Fix some spelling errors found by Lintian. Patch from Alessandro Ghedini <ghedo...
[valgrind.git] / drd / tests / pth_cond_race.c
blob303a5d84fe35dd315632d5532799c24d468744a5
1 /* Unit test for drd that triggers a race on the use of a POSIX condition
2 variable. By Bart Van Assche.
3 */
5 #include <assert.h>
6 #include <stdio.h> // printf()
7 #include <pthread.h>
8 #include <unistd.h> // usleep()
11 // Local functions declarations.
13 static void* thread_func(void* thread_arg);
16 // Local variables.
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)
27 int optchar;
28 pthread_t threadid;
30 while ((optchar = getopt(argc, argv, "m")) != EOF)
32 switch (optchar)
34 case 'm':
35 s_use_mutex = 1;
36 break;
37 default:
38 assert(0);
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);
56 return 0;
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);
70 return 0;