summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2010-12-20 09:47:02 (GMT)
committerOlivier Goffart <olivier.goffart@nokia.com>2010-12-20 09:47:02 (GMT)
commit10c90522d6e3807baac3c4bfca4eedc51a4251f5 (patch)
tree92005b547e3b92eb9e640fae8c78fdcdc13b6dd9 /src/corelib/kernel
parent64e1f888586f2c988b08bcc93579990e970b7206 (diff)
parent97b039438bc31bb420138d72549372adb1244dc8 (diff)
downloadQt-10c90522d6e3807baac3c4bfca4eedc51a4251f5.zip
Qt-10c90522d6e3807baac3c4bfca4eedc51a4251f5.tar.gz
Qt-10c90522d6e3807baac3c4bfca4eedc51a4251f5.tar.bz2
Merge remote branch 'origin/4.7' into qt-master-from-4.7
Conflicts: demos/declarative/minehunt/minehunt.pro src/opengl/gl2paintengineex/qtextureglyphcache_gl.cpp src/plugins/phonon/mmf/mmf.pro src/s60installs/s60installs.pro tests/auto/qapplication/test/test.pro tests/auto/qgraphicsview/tst_qgraphicsview.cpp
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r--src/corelib/kernel/qabstracteventdispatcher.cpp52
-rw-r--r--src/corelib/kernel/qeventdispatcher_unix.cpp1
2 files changed, 44 insertions, 9 deletions
diff --git a/src/corelib/kernel/qabstracteventdispatcher.cpp b/src/corelib/kernel/qabstracteventdispatcher.cpp
index bcf4477..bf675a7 100644
--- a/src/corelib/kernel/qabstracteventdispatcher.cpp
+++ b/src/corelib/kernel/qabstracteventdispatcher.cpp
@@ -77,10 +77,14 @@ Q_DESTRUCTOR_FUNCTION(timerIdsDestructorFunction)
static QBasicAtomicInt nextFreeTimerId = Q_BASIC_ATOMIC_INITIALIZER(1);
+static const int TimerIdMask = 0x00ffffff;
+static const int TimerSerialMask = ~TimerIdMask & ~0x80000000;
+static const int TimerSerialCounter = TimerIdMask + 1;
+
// avoid the ABA-problem by using 7 of the top 8 bits of the timerId as a serial number
static inline int prepareNewValueWithSerialNumber(int oldId, int newId)
{
- return (newId & 0x00FFFFFF) | ((oldId + 0x01000000) & 0x7f000000);
+ return (newId & TimerIdMask) | ((oldId + TimerSerialCounter) & TimerSerialMask);
}
static inline int bucketOffset(int timerId)
@@ -120,17 +124,32 @@ void QAbstractEventDispatcherPrivate::init()
}
}
+// Timer IDs are implemented using a free-list;
+// there's a vector initialized with:
+// X[i] = i + 1
+// and nextFreeTimerId starts with 1.
+//
+// Allocating a timer ID involves taking the ID from
+// X[nextFreeTimerId]
+// updating nextFreeTimerId to this value and returning the old value
+//
+// When the timer ID is allocated, its cell in the vector is unused (it's a
+// free list). As an added protection, we use the cell to store an invalid
+// (negative) value that we can later check for integrity.
+//
+// (continues below).
int QAbstractEventDispatcherPrivate::allocateTimerId()
{
int timerId, newTimerId;
+ int at, *b;
do {
- timerId = nextFreeTimerId;
+ timerId = nextFreeTimerId; //.loadAcquire(); // ### FIXME Proper memory ordering semantics
// which bucket are we looking in?
- int which = timerId & 0x00ffffff;
+ int which = timerId & TimerIdMask;
int bucket = bucketOffset(which);
- int at = bucketIndex(bucket, which);
- int *b = timerIds[bucket];
+ at = bucketIndex(bucket, which);
+ b = timerIds[bucket];
if (!b) {
// allocate a new bucket
@@ -142,23 +161,38 @@ int QAbstractEventDispatcherPrivate::allocateTimerId()
}
}
- newTimerId = b[at];
+ newTimerId = prepareNewValueWithSerialNumber(timerId, b[at]);
} while (!nextFreeTimerId.testAndSetRelaxed(timerId, newTimerId));
+ b[at] = -timerId;
+
return timerId;
}
+// Releasing a timer ID requires putting the current ID back in the vector;
+// we do it by setting:
+// X[timerId] = nextFreeTimerId;
+// then we update nextFreeTimerId to the timer we've just released
+//
+// The extra code in allocateTimerId and releaseTimerId are ABA prevention
+// and bucket memory. The buckets are simply to make sure we allocate only
+// the necessary number of timers. See above.
+//
+// ABA prevention simply adds a value to 7 of the top 8 bits when resetting
+// nextFreeTimerId.
void QAbstractEventDispatcherPrivate::releaseTimerId(int timerId)
{
- int which = timerId & 0x00ffffff;
+ int which = timerId & TimerIdMask;
int bucket = bucketOffset(which);
int at = bucketIndex(bucket, which);
int *b = timerIds[bucket];
+ Q_ASSERT(b[at] == -timerId);
+
int freeId, newTimerId;
do {
- freeId = nextFreeTimerId;
- b[at] = freeId & 0x00ffffff;
+ freeId = nextFreeTimerId;//.loadAcquire(); // ### FIXME Proper memory ordering semantics
+ b[at] = freeId & TimerIdMask;
newTimerId = prepareNewValueWithSerialNumber(freeId, timerId);
} while (!nextFreeTimerId.testAndSetRelease(freeId, newTimerId));
diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp
index b2b9806..234c164 100644
--- a/src/corelib/kernel/qeventdispatcher_unix.cpp
+++ b/src/corelib/kernel/qeventdispatcher_unix.cpp
@@ -509,6 +509,7 @@ bool QTimerInfoList::unregisterTimer(int timerId)
}
}
// id not found
+ qWarning("Application asked to unregister timer 0x%x which is not registered in this thread. Fix application.", timerId);
return false;
}