• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <errno.h>
4 // test program from johan.walles (bug 295590)
5 // This test verifies that helgrind detects (and does not crash) when
6 // the guest application wrongly destroys a cond var being waited
7 // upon.
8 pthread_mutex_t mutex;
9 pthread_cond_t cond;
10 pthread_t thread;
11 int ready = 0;
12 
ThreadFunction(void * ptr)13 void *ThreadFunction(void *ptr)
14 {
15    pthread_mutex_lock(&mutex);
16    ready = 1;
17    pthread_cond_signal(&cond);
18    pthread_cond_destroy(&cond); // ERROR!!!
19    pthread_mutex_unlock(&mutex);
20    return NULL;
21 }
22 
main()23 int main()
24 {
25    pthread_mutex_init(&mutex, NULL);
26    pthread_cond_init(&cond, NULL);
27 
28    pthread_mutex_lock(&mutex);
29    pthread_create(&thread, NULL, ThreadFunction, (void*) NULL);
30    while (!ready) { // to insure ourselves against spurious wakeups
31       pthread_cond_wait(&cond, &mutex);
32    }
33    pthread_mutex_unlock(&mutex);
34 
35    pthread_join(thread, NULL);
36    pthread_mutex_destroy(&mutex);
37    printf("finished\n");
38    return 0;
39 }
40