Fix semaphore destruction (bug 12674).
[platform/upstream/glibc.git] / nptl / tst-sem13.c
1 #include <errno.h>
2 #include <semaphore.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <pthread.h>
6 #include <internaltypes.h>
7
8
9 static int
10 do_test (void)
11 {
12   union
13   {
14     sem_t s;
15     struct new_sem ns;
16   } u;
17
18   if (sem_init (&u.s, 0, 0) != 0)
19     {
20       puts ("sem_init failed");
21       return 1;
22     }
23
24   struct timespec ts = { 0, 1000000001 };       /* Invalid.  */
25   errno = 0;
26   if (sem_timedwait (&u.s, &ts) >= 0)
27     {
28       puts ("sem_timedwait did not fail");
29       return 1;
30     }
31   if (errno != EINVAL)
32     {
33       perror ("sem_timedwait did not fail with EINVAL");
34       return 1;
35     }
36 #if __HAVE_64B_ATOMICS
37   unsigned int nwaiters = (u.ns.data >> SEM_NWAITERS_SHIFT);
38 #else
39   unsigned int nwaiters = u.ns.nwaiters;
40 #endif
41   if (nwaiters != 0)
42     {
43       printf ("sem_timedwait modified nwaiters: %d\n", nwaiters);
44       return 1;
45     }
46
47   ts.tv_sec = /* Invalid.  */ -2;
48   ts.tv_nsec = 0;
49   errno = 0;
50   if (sem_timedwait (&u.s, &ts) >= 0)
51     {
52       puts ("2nd sem_timedwait did not fail");
53       return 1;
54     }
55   if (errno != ETIMEDOUT)
56     {
57       perror ("2nd sem_timedwait did not fail with ETIMEDOUT");
58       return 1;
59     }
60 #if __HAVE_64B_ATOMICS
61   nwaiters = (u.ns.data >> SEM_NWAITERS_SHIFT);
62 #else
63   nwaiters = u.ns.nwaiters;
64 #endif
65   if (nwaiters != 0)
66     {
67       printf ("2nd sem_timedwait modified nwaiters: %d\n", nwaiters);
68       return 1;
69     }
70
71   return 0;
72 }
73
74 #define TEST_FUNCTION do_test ()
75 #include "../test-skeleton.c"