Commit 6ae2b000 authored by René J.V. Bertin's avatar René J.V. Bertin

make KGlobal reference counting thread safe.

REVIEW: 121134
parent a16d6f59
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "kglobal.h" #include "kglobal.h"
#include "kglobal_p.h" #include "kglobal_p.h"
#include <QThread> #include <QThread>
#include <QAtomicInt>
#include <config.h> #include <config.h>
...@@ -314,27 +315,30 @@ QString KGlobal::caption() ...@@ -314,27 +315,30 @@ QString KGlobal::caption()
* e.g. a file copy for a file manager, or 'compacting folders on exit' for a mail client, * e.g. a file copy for a file manager, or 'compacting folders on exit' for a mail client,
* the job progress widget with "keep open" checked, etc. * the job progress widget with "keep open" checked, etc.
*/ */
static int s_refCount = 0; static QBasicAtomicInt s_allowQuit = Q_BASIC_ATOMIC_INITIALIZER(false); // this is used a bool
static bool s_allowQuit = false; static QBasicAtomicInt s_refCount = Q_BASIC_ATOMIC_INITIALIZER(0);
void KGlobal::ref() void KGlobal::ref()
{ {
++s_refCount; s_refCount.fetchAndAddOrdered(1);
//kDebug() << "KGlobal::ref() : refCount = " << s_refCount;
} }
void KGlobal::deref() void KGlobal::deref()
{ {
--s_refCount; const int prevRefCount = s_refCount.fetchAndAddOrdered(-1);
//kDebug() << "KGlobal::deref() : refCount = " << s_refCount; if (prevRefCount <= 1 && int(s_allowQuit)) {
if (s_refCount <= 0 && s_allowQuit) {
QCoreApplication::instance()->quit(); QCoreApplication::instance()->quit();
} }
} }
void KGlobal::setAllowQuit(bool allowQuit) void KGlobal::setAllowQuit(bool allowQuit)
{ {
s_allowQuit = allowQuit; if (QThread::currentThread() == qApp->thread()) {
s_allowQuit.fetchAndStoreOrdered(int(allowQuit));
}
else {
qWarning() << "KGlobal::setAllowQuit may only be called from the main thread";
}
} }
#undef PRIVATE_DATA #undef PRIVATE_DATA
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment