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()
9
#include "../drd_clientreq.h"
12
// Local functions declarations.
14
static void* thread_func(void* thread_arg);
19
static pthread_mutex_t s_mutex;
20
static pthread_cond_t s_cond;
21
static int s_use_mutex = 0;
24
// Function definitions.
26
static void set_thread_name(const char* const name)
29
VALGRIND_DO_CLIENT_REQUEST(res, 0, VG_USERREQ__SET_THREAD_NAME,
33
int main(int argc, char** argv)
38
set_thread_name("main");
40
while ((optchar = getopt(argc, argv, "m")) != EOF)
52
pthread_cond_init(&s_cond, 0);
53
pthread_mutex_init(&s_mutex, 0);
54
pthread_mutex_lock(&s_mutex);
56
pthread_create(&threadid, 0, thread_func, 0);
58
pthread_cond_wait(&s_cond, &s_mutex);
59
pthread_mutex_unlock(&s_mutex);
61
pthread_join(threadid, 0);
63
pthread_mutex_destroy(&s_mutex);
64
pthread_cond_destroy(&s_cond);
69
static void* thread_func(void* thread_arg)
71
set_thread_name("thread_func");
73
// Wait until the main thread has entered pthread_cond_wait().
74
pthread_mutex_lock(&s_mutex);
75
pthread_mutex_unlock(&s_mutex);
77
// Signal the condition variable.
78
if (s_use_mutex) pthread_mutex_lock(&s_mutex);
79
pthread_cond_signal(&s_cond);
80
if (s_use_mutex) pthread_mutex_unlock(&s_mutex);