* sysdeps/unix/sysv/linux/bits/resource.h: Define RUSAGE_THREAD and
[glibc.git] / nptl / tst-sem11.c
blob6633ddd1f33b95ccfbdeb6a483282fc821c97d76
1 #include <semaphore.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 #include <internaltypes.h>
7 #ifndef SEM_WAIT
8 # define SEM_WAIT(s) sem_wait (s)
9 #endif
11 static void *
12 tf (void *arg)
14 #ifdef PREPARE
15 PREPARE
16 #endif
17 SEM_WAIT (arg);
18 return NULL;
21 int
22 main (void)
24 int tries = 5;
25 pthread_t th;
26 sem_t s;
27 again:
28 if (sem_init (&s, 0, 0) != 0)
30 puts ("sem_init failed");
31 return 1;
34 struct new_sem *is = (struct new_sem *) &s;
36 if (is->nwaiters != 0)
38 puts ("nwaiters not initialized");
39 return 1;
42 if (pthread_create (&th, NULL, tf, &s) != 0)
44 puts ("pthread_create failed");
45 return 1;
48 sleep (1);
50 if (pthread_cancel (th) != 0)
52 puts ("pthread_cancel failed");
53 return 1;
56 void *r;
57 if (pthread_join (th, &r) != 0)
59 puts ("pthread_join failed");
60 return 1;
62 if (r != PTHREAD_CANCELED && --tries > 0)
64 /* Maybe we get the scheduling right the next time. */
65 sem_destroy (&s);
66 goto again;
69 if (is->nwaiters != 0)
71 puts ("nwaiters not reset");
72 return 1;
75 return 0;