[waffle] [PATCH v3] wcore_error_unittest: Fix race condition leading to forever loop
Pauli Nieminen
pauli.nieminen at linux.intel.com
Thu May 31 09:49:25 PDT 2012
Reading non-volatile value in loop without function calls allows C
compiler to generate forever loop without checking the memory value. To
avoid compiler optimization causing trouble take lock when reading the
number of threads.
Signed-off-by: Pauli Nieminen <pauli.nieminen at linux.intel.com>
---
If Mac doesn't have barries then we have to stick simulating barriers. Even
tough barrier in linux can be implemented with single atomic int and futex.
We could probably do a pseudo barrier with same princible using mutex and
atomic int. But who cares as long as test is quarenteed to work correctly.
tests/unittests/waffle/core/wcore_error_unittest.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/tests/unittests/waffle/core/wcore_error_unittest.c b/tests/unittests/waffle/core/wcore_error_unittest.c
index 06ebb78..e9fdb94 100644
--- a/tests/unittests/waffle/core/wcore_error_unittest.c
+++ b/tests/unittests/waffle/core/wcore_error_unittest.c
@@ -191,6 +191,7 @@ TEST(wcore_error, thread_local)
pthread_mutex_t mutex;
pthread_cond_t cond;
int num_threads_waiting = 0;
+ int thread_count;
pthread_t threads[NUM_THREADS];
struct thread_arg thread_args[NUM_THREADS];
@@ -213,8 +214,13 @@ TEST(wcore_error, thread_local)
// Wait for all threads to set their error codes, thus giving
// the threads opportunity to clobber each other's codes.
- while (num_threads_waiting < NUM_THREADS)
- ;;
+ do {
+ pthread_mutex_lock(&mutex); {
+ thread_count = num_threads_waiting;
+ pthread_mutex_unlock(&mutex);
+ }
+ } while (thread_count < NUM_THREADS);
+
pthread_mutex_lock(&mutex); {
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
--
1.7.9.5
More information about the waffle
mailing list