diff options
author | David Faure <faure+bluesystems@kde.org> | 2013-01-14 12:58:31 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-15 14:58:35 +0100 |
commit | d147285d644157bc12487584578b5389b56eea31 (patch) | |
tree | b035b6ca02197e8f0a2df6c10a89f51ecf1f50e0 /src/corelib/kernel/qcoreapplication.cpp | |
parent | 3a1f1aecf9b4ef05fd162cd60e06214dc28922b6 (diff) |
Add Q_COREAPP_STARTUP_FUNCTION macro.
This is necessary for initializing things in a library, which require
a QCoreApplication instance (unlike Q_CONSTRUCTOR_FUNCTION, which runs
before that). Example use cases: KCrash (segv handler), and KCheckAccelerators
(debugging tool triggered by magic key combination).
Change-Id: I5f4c4699dd4d21aea72b007989ba57467e86ed10
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qcoreapplication.cpp')
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 8ede244145..c9f973b52f 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -194,8 +194,32 @@ extern "C" void Q_CORE_EXPORT qt_startup_hook() { } +typedef QList<QtStartUpFunction> QStartUpFuncList; +Q_GLOBAL_STATIC(QStartUpFuncList, preRList) typedef QList<QtCleanUpFunction> QVFuncList; Q_GLOBAL_STATIC(QVFuncList, postRList) +static QBasicMutex globalPreRoutinesMutex; + +/*! + \internal + + Adds a global routine that will be called from the QCoreApplication + constructor. The public API is Q_COREAPP_STARTUP_FUNCTION. +*/ +void qAddPreRoutine(QtStartUpFunction p) +{ + QStartUpFuncList *list = preRList(); + if (!list) + return; + // Due to C++11 parallel dynamic initialization, this can be called + // from multiple threads. +#ifndef QT_NO_THREAD + QMutexLocker locker(&globalPreRoutinesMutex); +#endif + if (QCoreApplication::instance()) + p(); + list->prepend(p); // in case QCoreApplication is re-created, see qt_call_pre_routines +} void qAddPostRoutine(QtCleanUpFunction p) { @@ -213,6 +237,21 @@ void qRemovePostRoutine(QtCleanUpFunction p) list->removeAll(p); } +static void qt_call_pre_routines() +{ + QStartUpFuncList *list = preRList(); + if (!list) + return; +#ifndef QT_NO_THREAD + QMutexLocker locker(&globalPreRoutinesMutex); +#endif + // Unlike qt_call_post_routines, we don't empty the list, because + // Q_COREAPP_STARTUP_FUNCTION is a macro, so the user expects + // the function to be executed every time QCoreApplication is created. + for (int i = 0; i < list->count(); ++i) + list->at(i)(); +} + void Q_CORE_EXPORT qt_call_post_routines() { QVFuncList *list = 0; @@ -637,6 +676,7 @@ void QCoreApplication::init() d->processCommandLineArguments(); + qt_call_pre_routines(); qt_startup_hook(); } @@ -2325,6 +2365,31 @@ void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatc } /*! + \macro Q_COREAPP_STARTUP_FUNCTION(QtStartUpFunction ptr) + \since 5.1 + \relates QCoreApplication + \reentrant + + Adds a global function that will be called from the QCoreApplication + constructor. This macro is normally used to initialize libraries + for program-wide functionality, without requiring the application to + call into the library for initialization. + + The function specified by \a ptr should take no arguments and should + return nothing. For example: + + \snippet code/src_corelib_kernel_qcoreapplication.cpp 3 + + Note that the startup function will run at the end of the QCoreApplication constructor, + before any GUI initialization. If GUI code is required in the function, + use a timer (or a queued invocation) to perform the initialization later on, + from the event loop. + + If QCoreApplication is deleted and another QCoreApplication is created, + the startup function will be invoked again. +*/ + +/*! \fn void qAddPostRoutine(QtCleanUpFunction ptr) \relates QCoreApplication |