QtConcurrent::IterateKernel: fix a race on a cache variable

getticks() can be called concurrently, so accessing a non-atomic static long,
even when the assignment will produce the same value in evey case, constitutes
a data race.

Fixed by making 'useThreadCpuTime' atomic.

Since atomic long's might not be supported on all platforms, use an
atomic int instead. To avoid a narrowing conversion, and since we're
not interested in the return value of sysconf(), only whether it
succeeded, convert any non-error return value to 0 prior to storing
in the atomic.

Change-Id: Ic285f7801327b30ddcd9c24bf1ccee3112a447b1
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
bb10
Marc Mutz 2013-09-20 03:07:14 +02:00 committed by The Qt Project
parent c66d42f972
commit 935e52108d
1 changed files with 6 additions and 3 deletions

View File

@ -90,10 +90,13 @@ static qint64 getticks()
# if (_POSIX_THREAD_CPUTIME-0 == 0)
// detect availablility of CLOCK_THREAD_CPUTIME_ID
static long useThreadCpuTime = -2;
static QBasicAtomicInt sUseThreadCpuTime = Q_BASIC_ATOMIC_INITIALIZER(-2);
int useThreadCpuTime = sUseThreadCpuTime.load();
if (useThreadCpuTime == -2) {
// sysconf() will return either -1 or _POSIX_VERSION (don't care about thread races here)
useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME);
// sysconf() will return either -1L or _POSIX_VERSION
// (don't care about sysconf's exact return value)
useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME) == -1L ? -1 : 0 ;
sUseThreadCpuTime.store(useThreadCpuTime); // might happen multiple times, but doesn't matter
}
if (useThreadCpuTime != -1)
clockId = CLOCK_THREAD_CPUTIME_ID;