Add 469782 to NEWS
[valgrind.git] / helgrind / tests / tc23_bogus_condwait.c
blobf2ccf3517fd45ad3a68d6a1eec337694d73a4a20
1 /* Expect 5 errors total (4 re cvs, 1 re exiting w/lock.).
2 Tests passing bogus mutexes to pthread_cond_wait. */
3 #define _GNU_SOURCE 1 /* needed by glibc <= 2.3 for pthread_rwlock_* */
4 #include <pthread.h>
5 #include <assert.h>
6 #include <unistd.h>
7 #include <semaphore.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #if defined(VGO_freebsd)
11 #include <sys/fcntl.h>
12 #endif
13 pthread_mutex_t mx[4];
14 pthread_cond_t cv; pthread_rwlock_t rwl;
15 sem_t* quit_now;
16 static sem_t* my_sem_init(char*, int, unsigned);
17 static int my_sem_destroy(sem_t*);
18 static int my_sem_wait(sem_t*); static int my_sem_post(sem_t*);
19 void* rescue_me ( void* uu )
21 #if !defined(VGO_freebsd)
22 /* wait for, and unblock, the first wait */
23 sleep(1);
24 pthread_cond_signal( &cv );
25 #endif
27 /* wait for, and unblock, the second wait */
28 sleep(1);
29 pthread_cond_signal( &cv );
31 /* wait for, and unblock, the third wait */
32 sleep(1);
33 pthread_cond_signal( &cv );
35 /* wait for, and unblock, the fourth wait */
36 sleep(1);
37 pthread_cond_signal( &cv );
39 my_sem_wait( quit_now );
40 return NULL;
43 void* grab_the_lock ( void* uu )
45 int r= pthread_mutex_lock( &mx[2] ); assert(!r);
46 my_sem_wait( quit_now );
47 r= pthread_mutex_unlock( &mx[2] ); assert(!r);
48 return NULL;
51 int main ( void )
53 int r;
54 pthread_t my_rescuer, grabber;
56 r= pthread_mutex_init(&mx[0], NULL); assert(!r);
57 r= pthread_mutex_init(&mx[1], NULL); assert(!r);
58 r= pthread_mutex_init(&mx[2], NULL); assert(!r);
59 r= pthread_mutex_init(&mx[3], NULL); assert(!r);
61 r= pthread_cond_init(&cv, NULL); assert(!r);
62 r= pthread_rwlock_init(&rwl, NULL); assert(!r);
64 quit_now = my_sem_init( "quit_now", 0,0 ); assert(quit_now);
66 r= pthread_create( &grabber, NULL, grab_the_lock, NULL ); assert(!r);
67 sleep(1); /* let the grabber get there first */
69 r= pthread_create( &my_rescuer, NULL, rescue_me, NULL ); assert(!r);
70 /* Do stupid things and hope that rescue_me gets us out of
71 trouble */
73 #if !defined(VGO_freebsd)
74 /* mx is bogus */
75 r= pthread_cond_wait(&cv, (pthread_mutex_t*)(4 + (char*)&mx[0]) );
76 #endif
78 /* mx is not locked */
79 r= pthread_cond_wait(&cv, &mx[3]);
81 /* wrong flavour of lock */
82 r= pthread_cond_wait(&cv, (pthread_mutex_t*)&rwl );
84 /* mx is held by someone else. */
85 r= pthread_cond_wait(&cv, &mx[2] );
87 r= my_sem_post( quit_now ); assert(!r);
88 r= my_sem_post( quit_now ); assert(!r);
90 r= pthread_join( my_rescuer, NULL ); assert(!r);
91 r= pthread_join( grabber, NULL ); assert(!r);
93 r= my_sem_destroy( quit_now ); assert(!r);
94 return 0;
104 static sem_t* my_sem_init (char* identity, int pshared, unsigned count)
106 sem_t* s;
108 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
109 s = malloc(sizeof(*s));
110 if (s) {
111 if (sem_init(s, pshared, count) < 0) {
112 perror("sem_init");
113 free(s);
114 s = NULL;
117 #elif defined(VGO_darwin)
118 char name[100];
119 sprintf(name, "anonsem_%s_pid%d", identity, (int)getpid());
120 name[ sizeof(name)-1 ] = 0;
121 if (0) printf("name = %s\n", name);
122 s = sem_open(name, O_CREAT | O_EXCL, 0600, count);
123 if (s == SEM_FAILED) {
124 perror("sem_open");
125 s = NULL;
127 #else
128 # error "Unsupported OS"
129 #endif
131 return s;
134 static int my_sem_destroy ( sem_t* s )
136 return sem_destroy(s);
139 static int my_sem_wait(sem_t* s)
141 return sem_wait(s);
144 static int my_sem_post(sem_t* s)
146 return sem_post(s);