1 /* Check that Helgrind does not complain about semaphores with a
2 nonzero initial value, when said semaphores are correctly used.
3 Also useful for generating VCG of simple semaphore activity, for
15 static sem_t
* my_sem_init(char*, int, unsigned);
16 static int my_sem_destroy(sem_t
*);
17 static int my_sem_wait(sem_t
*); //static int my_sem_post(sem_t*);
19 void* child_fn ( void* semV
) {
21 sem_t
* sem
= (sem_t
*)semV
;
22 r
= my_sem_wait(sem
); assert(!r
);
30 pthread_t child
[N_THREADS
];
32 sem
= my_sem_init("sem1", 0, N_THREADS
); assert(sem
);
34 for (i
= 0; i
< N_THREADS
; i
++) {
35 r
= pthread_create( &child
[i
], NULL
, child_fn
, sem
);
39 for (i
= 0; i
< N_THREADS
; i
++) {
40 r
= pthread_join( child
[i
], NULL
);
44 r
= my_sem_destroy(sem
); assert(!r
);
49 static sem_t
* my_sem_init (char* identity
, int pshared
, unsigned count
)
53 #if defined(VGO_linux) || defined(VGO_solaris)
54 s
= malloc(sizeof(*s
));
56 if (sem_init(s
, pshared
, count
) < 0) {
62 #elif defined(VGO_darwin)
64 sprintf(name
, "anonsem_%s_pid%d", identity
, (int)getpid());
65 name
[ sizeof(name
)-1 ] = 0;
66 if (0) printf("name = %s\n", name
);
67 s
= sem_open(name
, O_CREAT
| O_EXCL
, 0600, count
);
68 if (s
== SEM_FAILED
) {
73 # error "Unsupported OS"
79 static int my_sem_destroy ( sem_t
* s
)
81 return sem_destroy(s
);
84 static int my_sem_wait(sem_t
* s
)
90 static int my_sem_post(sem_t
* s
)