From 935e52108d54f76fbcdc09479125ec5f7a3bb5b2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 20 Sep 2013 03:07:14 +0200 Subject: 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 --- src/concurrent/qtconcurrentiteratekernel.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/concurrent') 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; -- cgit v1.2.3