regtest: turn off exec stack warning
[valgrind.git] / helgrind / tests / tc24_nonzero_sem.c
blob541fa7ddb8816a1a77aa98b00d046aa656c64b71
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
4 inspection. */
6 #include <stdio.h>
7 #include <pthread.h>
8 #include <semaphore.h>
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #if defined(VGO_freebsd)
13 # include <sys/fcntl.h>
14 #endif
16 #define N_THREADS 3
18 static sem_t* my_sem_init(char*, int, unsigned);
19 static int my_sem_destroy(sem_t*);
20 static int my_sem_wait(sem_t*); //static int my_sem_post(sem_t*);
22 void* child_fn ( void* semV ) {
23 int r;
24 sem_t* sem = (sem_t*)semV;
25 r= my_sem_wait(sem); assert(!r);
26 return NULL;
29 int main ( void )
31 int r, i;
32 sem_t* sem;
33 pthread_t child[N_THREADS];
35 sem= my_sem_init("sem1", 0, N_THREADS); assert(sem);
37 for (i = 0; i < N_THREADS; i++) {
38 r= pthread_create( &child[i], NULL, child_fn, sem );
39 assert(!r);
42 for (i = 0; i < N_THREADS; i++) {
43 r= pthread_join( child[i], NULL );
44 assert(!r);
47 r= my_sem_destroy(sem); assert(!r);
48 return 0;
52 static sem_t* my_sem_init (char* identity, int pshared, unsigned count)
54 sem_t* s;
56 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
57 s = malloc(sizeof(*s));
58 if (s) {
59 if (sem_init(s, pshared, count) < 0) {
60 perror("sem_init");
61 free(s);
62 s = NULL;
65 #elif defined(VGO_darwin)
66 char name[100];
67 sprintf(name, "anonsem_%s_pid%d", identity, (int)getpid());
68 name[ sizeof(name)-1 ] = 0;
69 if (0) printf("name = %s\n", name);
70 s = sem_open(name, O_CREAT | O_EXCL, 0600, count);
71 if (s == SEM_FAILED) {
72 perror("sem_open");
73 s = NULL;
75 #else
76 # error "Unsupported OS"
77 #endif
79 return s;
82 static int my_sem_destroy ( sem_t* s )
84 return sem_destroy(s);
87 static int my_sem_wait(sem_t* s)
89 return sem_wait(s);
92 #if 0
93 static int my_sem_post(sem_t* s)
95 return sem_post(s);
97 #endif