1 /* Unit test for drd that triggers a race on the use of a POSIX condition
2 variable. By Bart Van Assche.
6 #include <stdio.h> // printf()
8 #include <unistd.h> // usleep()
11 // Local functions declarations.
13 static void* thread_func(void* thread_arg
);
18 static pthread_mutex_t s_mutex
;
19 static pthread_cond_t s_cond
;
20 static int s_use_mutex
= 0;
23 // Function definitions.
25 int main(int argc
, char** argv
)
30 while ((optchar
= getopt(argc
, argv
, "m")) != EOF
)
42 pthread_cond_init(&s_cond
, 0);
43 pthread_mutex_init(&s_mutex
, 0);
44 pthread_mutex_lock(&s_mutex
);
46 pthread_create(&threadid
, 0, thread_func
, 0);
48 pthread_cond_wait(&s_cond
, &s_mutex
);
49 pthread_mutex_unlock(&s_mutex
);
51 pthread_join(threadid
, 0);
53 pthread_mutex_destroy(&s_mutex
);
54 pthread_cond_destroy(&s_cond
);
59 static void* thread_func(void* thread_arg
)
61 // Wait until the main thread has entered pthread_cond_wait().
62 pthread_mutex_lock(&s_mutex
);
63 pthread_mutex_unlock(&s_mutex
);
65 // Signal the condition variable.
66 if (s_use_mutex
) pthread_mutex_lock(&s_mutex
);
67 pthread_cond_signal(&s_cond
);
68 if (s_use_mutex
) pthread_mutex_unlock(&s_mutex
);