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 #if defined(VGO_freebsd)
11 #include <sys/fcntl.h>
13 pthread_mutex_t mx
[4];
14 pthread_cond_t cv
; pthread_rwlock_t rwl
;
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 */
24 pthread_cond_signal( &cv
);
27 /* wait for, and unblock, the second wait */
29 pthread_cond_signal( &cv
);
31 /* wait for, and unblock, the third wait */
33 pthread_cond_signal( &cv
);
35 /* wait for, and unblock, the fourth wait */
37 pthread_cond_signal( &cv
);
39 my_sem_wait( quit_now
);
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
);
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
73 #if !defined(VGO_freebsd)
75 r
= pthread_cond_wait(&cv
, (pthread_mutex_t
*)(4 + (char*)&mx
[0]) );
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
);
104 static sem_t
* my_sem_init (char* identity
, int pshared
, unsigned count
)
108 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
109 s
= malloc(sizeof(*s
));
111 if (sem_init(s
, pshared
, count
) < 0) {
117 #elif defined(VGO_darwin)
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
) {
128 # error "Unsupported OS"
134 static int my_sem_destroy ( sem_t
* s
)
136 return sem_destroy(s
);
139 static int my_sem_wait(sem_t
* s
)
144 static int my_sem_post(sem_t
* s
)