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_* */
10 pthread_mutex_t mx[4];
11 pthread_cond_t cv; pthread_rwlock_t rwl;
13 static sem_t* my_sem_init(char*, int, unsigned);
14 static int my_sem_destroy(sem_t*);
15 static int my_sem_wait(sem_t*); static int my_sem_post(sem_t*);
16 void* rescue_me ( void* uu )
18 /* wait for, and unblock, the first wait */
20 pthread_cond_signal( &cv );
22 /* wait for, and unblock, the second wait */
24 pthread_cond_signal( &cv );
26 /* wait for, and unblock, the third wait */
28 pthread_cond_signal( &cv );
30 /* wait for, and unblock, the fourth wait */
32 pthread_cond_signal( &cv );
34 my_sem_wait( quit_now );
38 void* grab_the_lock ( void* uu )
40 int r= pthread_mutex_lock( &mx[2] ); assert(!r);
41 my_sem_wait( quit_now );
42 r= pthread_mutex_unlock( &mx[2] ); assert(!r);
49 pthread_t my_rescuer, grabber;
51 r= pthread_mutex_init(&mx[0], NULL); assert(!r);
52 r= pthread_mutex_init(&mx[1], NULL); assert(!r);
53 r= pthread_mutex_init(&mx[2], NULL); assert(!r);
54 r= pthread_mutex_init(&mx[3], NULL); assert(!r);
56 r= pthread_cond_init(&cv, NULL); assert(!r);
57 r= pthread_rwlock_init(&rwl, NULL); assert(!r);
59 quit_now = my_sem_init( "quit_now", 0,0 ); assert(quit_now);
61 r= pthread_create( &grabber, NULL, grab_the_lock, NULL ); assert(!r);
62 sleep(1); /* let the grabber get there first */
64 r= pthread_create( &my_rescuer, NULL, rescue_me, NULL ); assert(!r);
65 /* Do stupid things and hope that rescue_me gets us out of
69 r= pthread_cond_wait(&cv, (pthread_mutex_t*)(1 + (char*)&mx[0]) );
71 /* mx is not locked */
72 r= pthread_cond_wait(&cv, &mx[0]);
74 /* wrong flavour of lock */
75 r= pthread_cond_wait(&cv, (pthread_mutex_t*)&rwl );
77 /* mx is held by someone else. */
78 r= pthread_cond_wait(&cv, &mx[2] );
80 r= my_sem_post( quit_now ); assert(!r);
81 r= my_sem_post( quit_now ); assert(!r);
83 r= pthread_join( my_rescuer, NULL ); assert(!r);
84 r= pthread_join( grabber, NULL ); assert(!r);
86 r= my_sem_destroy( quit_now ); assert(!r);
97 static sem_t* my_sem_init (char* identity, int pshared, unsigned count)
101 #if defined(VGO_linux)
102 s = malloc(sizeof(*s));
104 if (sem_init(s, pshared, count) < 0) {
110 #elif defined(VGO_darwin)
112 sprintf(name, "anonsem_%s_pid%d", identity, (int)getpid());
113 name[ sizeof(name)-1 ] = 0;
114 if (0) printf("name = %s\n", name);
115 s = sem_open(name, O_CREAT | O_EXCL, 0600, count);
116 if (s == SEM_FAILED) {
121 # error "Unsupported OS"
127 static int my_sem_destroy ( sem_t* s )
129 return sem_destroy(s);
132 static int my_sem_wait(sem_t* s)
137 static int my_sem_post(sem_t* s)