blob: 6ea5ad90be08ace1a44859d005f53901d68fba91 [file] [log] [blame]
sewardjb4112022007-11-09 22:49:28 +00001/* 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>
sewardje84c7f42009-07-26 19:53:42 +00008#include <stdio.h>
sewardjb2513992011-03-16 10:44:13 +00009#include <stdlib.h>
bart4fa04642011-03-17 10:18:22 +000010pthread_mutex_t mx[4];
11pthread_cond_t cv; pthread_rwlock_t rwl;
bart310c8f22011-03-05 15:43:39 +000012sem_t* quit_now;
13static sem_t* my_sem_init(char*, int, unsigned);
sewardje84c7f42009-07-26 19:53:42 +000014static int my_sem_destroy(sem_t*);
15static int my_sem_wait(sem_t*); static int my_sem_post(sem_t*);
sewardjb4112022007-11-09 22:49:28 +000016void* rescue_me ( void* uu )
17{
18 /* wait for, and unblock, the first wait */
19 sleep(1);
20 pthread_cond_signal( &cv );
21
22 /* wait for, and unblock, the second wait */
23 sleep(1);
24 pthread_cond_signal( &cv );
25
26 /* wait for, and unblock, the third wait */
27 sleep(1);
28 pthread_cond_signal( &cv );
29
30 /* wait for, and unblock, the fourth wait */
31 sleep(1);
32 pthread_cond_signal( &cv );
33
bart310c8f22011-03-05 15:43:39 +000034 my_sem_wait( quit_now );
sewardjb4112022007-11-09 22:49:28 +000035 return NULL;
36}
bart4fa04642011-03-17 10:18:22 +000037
sewardjb4112022007-11-09 22:49:28 +000038void* grab_the_lock ( void* uu )
39{
40 int r= pthread_mutex_lock( &mx[2] ); assert(!r);
bart310c8f22011-03-05 15:43:39 +000041 my_sem_wait( quit_now );
sewardjb4112022007-11-09 22:49:28 +000042 r= pthread_mutex_unlock( &mx[2] ); assert(!r);
43 return NULL;
44}
45
46int main ( void )
47{
48 int r;
49 pthread_t my_rescuer, grabber;
50
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);
55
56 r= pthread_cond_init(&cv, NULL); assert(!r);
57 r= pthread_rwlock_init(&rwl, NULL); assert(!r);
58
bart310c8f22011-03-05 15:43:39 +000059 quit_now = my_sem_init( "quit_now", 0,0 ); assert(quit_now);
sewardjb4112022007-11-09 22:49:28 +000060
61 r= pthread_create( &grabber, NULL, grab_the_lock, NULL ); assert(!r);
62 sleep(1); /* let the grabber get there first */
63
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
66 trouble */
67
68 /* mx is bogus */
florian86aabd82011-09-18 00:11:12 +000069 r= pthread_cond_wait(&cv, (pthread_mutex_t*)(4 + (char*)&mx[0]) );
sewardjb4112022007-11-09 22:49:28 +000070
71 /* mx is not locked */
Elliott Hughesed398002017-06-21 14:41:24 -070072 r= pthread_cond_wait(&cv, &mx[3]);
sewardjb4112022007-11-09 22:49:28 +000073
74 /* wrong flavour of lock */
75 r= pthread_cond_wait(&cv, (pthread_mutex_t*)&rwl );
76
77 /* mx is held by someone else. */
78 r= pthread_cond_wait(&cv, &mx[2] );
79
bart310c8f22011-03-05 15:43:39 +000080 r= my_sem_post( quit_now ); assert(!r);
81 r= my_sem_post( quit_now ); assert(!r);
sewardjb4112022007-11-09 22:49:28 +000082
83 r= pthread_join( my_rescuer, NULL ); assert(!r);
84 r= pthread_join( grabber, NULL ); assert(!r);
sewardje84c7f42009-07-26 19:53:42 +000085
bart310c8f22011-03-05 15:43:39 +000086 r= my_sem_destroy( quit_now ); assert(!r);
sewardjb4112022007-11-09 22:49:28 +000087 return 0;
88}
sewardje84c7f42009-07-26 19:53:42 +000089
90
91
92
93
94
95
96
bart310c8f22011-03-05 15:43:39 +000097static sem_t* my_sem_init (char* identity, int pshared, unsigned count)
sewardje84c7f42009-07-26 19:53:42 +000098{
bart310c8f22011-03-05 15:43:39 +000099 sem_t* s;
100
sewardj8eb8bab2015-07-21 14:44:28 +0000101#if defined(VGO_linux) || defined(VGO_solaris)
bart310c8f22011-03-05 15:43:39 +0000102 s = malloc(sizeof(*s));
103 if (s) {
104 if (sem_init(s, pshared, count) < 0) {
105 perror("sem_init");
106 free(s);
107 s = NULL;
108 }
109 }
sewardje84c7f42009-07-26 19:53:42 +0000110#elif defined(VGO_darwin)
111 char name[100];
sewardje84c7f42009-07-26 19:53:42 +0000112 sprintf(name, "anonsem_%s_pid%d", identity, (int)getpid());
113 name[ sizeof(name)-1 ] = 0;
114 if (0) printf("name = %s\n", name);
bart310c8f22011-03-05 15:43:39 +0000115 s = sem_open(name, O_CREAT | O_EXCL, 0600, count);
116 if (s == SEM_FAILED) {
117 perror("sem_open");
118 s = NULL;
119 }
sewardje84c7f42009-07-26 19:53:42 +0000120#else
121# error "Unsupported OS"
122#endif
bart310c8f22011-03-05 15:43:39 +0000123
124 return s;
sewardje84c7f42009-07-26 19:53:42 +0000125}
126
127static int my_sem_destroy ( sem_t* s )
128{
sewardje84c7f42009-07-26 19:53:42 +0000129 return sem_destroy(s);
sewardje84c7f42009-07-26 19:53:42 +0000130}
131
132static int my_sem_wait(sem_t* s)
133{
sewardje84c7f42009-07-26 19:53:42 +0000134 return sem_wait(s);
sewardje84c7f42009-07-26 19:53:42 +0000135}
136
137static int my_sem_post(sem_t* s)
138{
sewardje84c7f42009-07-26 19:53:42 +0000139 return sem_post(s);
sewardje84c7f42009-07-26 19:53:42 +0000140}