diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2013-09-20 03:07:14 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-26 06:31:32 +0200 |
commit | 935e52108d54f76fbcdc09479125ec5f7a3bb5b2 (patch) | |
tree | cd465021251e5941c7ded64f70494d6b6231c4c2 /src/concurrent/qtconcurrentiteratekernel.cpp | |
parent | c66d42f97299c19fab40a7f59be961e6928f43ed (diff) |
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>
Diffstat (limited to 'src/concurrent/qtconcurrentiteratekernel.cpp')
-rw-r--r-- | src/concurrent/qtconcurrentiteratekernel.cpp | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/concurrent/qtconcurrentiteratekernel.cpp b/src/concurrent/qtconcurrentiteratekernel.cpp index c3b35afe3f..268d85cb01 100644 --- a/src/concurrent/qtconcurrentiteratekernel.cpp +++ b/src/concurrent/qtconcurrentiteratekernel.cpp @@ -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; |