manual: complete example in error message documentation
[glibc.git] / nptl / tst-sem11.c
blob1a2dbafd8930d6b324dad1c59e0f895bc24b31db
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 union
28 sem_t s;
29 struct new_sem ns;
30 } u;
31 again:
32 if (sem_init (&u.s, 0, 0) != 0)
34 puts ("sem_init failed");
35 return 1;
37 #if __HAVE_64B_ATOMICS
38 if ((u.ns.data >> SEM_NWAITERS_SHIFT) != 0)
39 #else
40 if (u.ns.nwaiters != 0)
41 #endif
43 puts ("nwaiters not initialized");
44 return 1;
47 if (pthread_create (&th, NULL, tf, &u.s) != 0)
49 puts ("pthread_create failed");
50 return 1;
53 sleep (1);
55 if (pthread_cancel (th) != 0)
57 puts ("pthread_cancel failed");
58 return 1;
61 void *r;
62 if (pthread_join (th, &r) != 0)
64 puts ("pthread_join failed");
65 return 1;
67 if (r != PTHREAD_CANCELED && --tries > 0)
69 /* Maybe we get the scheduling right the next time. */
70 sem_destroy (&u.s);
71 goto again;
74 #if __HAVE_64B_ATOMICS
75 if ((u.ns.data >> SEM_NWAITERS_SHIFT) != 0)
76 #else
77 if (u.ns.nwaiters != 0)
78 #endif
80 puts ("nwaiters not reset");
81 return 1;
84 return 0;