Upload Tizen:Base source
[external/eglibc.git] / nptl / tst-sem11.c
1 #include <semaphore.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 #include <internaltypes.h>
6
7 #ifndef SEM_WAIT
8 # define SEM_WAIT(s) sem_wait (s)
9 #endif
10
11 static void *
12 tf (void *arg)
13 {
14 #ifdef PREPARE
15   PREPARE
16 #endif
17   SEM_WAIT (arg);
18   return NULL;
19 }
20
21 int
22 main (void)
23 {
24   int tries = 5;
25   pthread_t th;
26   union
27   {
28     sem_t s;
29     struct new_sem ns;
30   } u;
31  again:
32   if (sem_init (&u.s, 0, 0) != 0)
33     {
34       puts ("sem_init failed");
35       return 1;
36     }
37
38   if (u.ns.nwaiters != 0)
39     {
40       puts ("nwaiters not initialized");
41       return 1;
42     }
43
44   if (pthread_create (&th, NULL, tf, &u.s) != 0)
45     {
46       puts ("pthread_create failed");
47       return 1;
48     }
49
50   sleep (1);
51
52   if (pthread_cancel (th) != 0)
53     {
54       puts ("pthread_cancel failed");
55       return 1;
56     }
57
58   void *r;
59   if (pthread_join (th, &r) != 0)
60     {
61       puts ("pthread_join failed");
62       return 1;
63     }
64   if (r != PTHREAD_CANCELED && --tries > 0)
65     {
66       /* Maybe we get the scheduling right the next time.  */
67       sem_destroy (&u.s);
68       goto again;
69     }
70
71   if (u.ns.nwaiters != 0)
72     {
73       puts ("nwaiters not reset");
74       return 1;
75     }
76
77   return 0;
78 }