blob: 6633ddd1f33b95ccfbdeb6a483282fc821c97d76 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001#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
11static void *
12tf (void *arg)
13{
14#ifdef PREPARE
15 PREPARE
16#endif
17 SEM_WAIT (arg);
18 return NULL;
19}
20
21int
22main (void)
23{
24 int tries = 5;
25 pthread_t th;
26 sem_t s;
27 again:
28 if (sem_init (&s, 0, 0) != 0)
29 {
30 puts ("sem_init failed");
31 return 1;
32 }
33
34 struct new_sem *is = (struct new_sem *) &s;
35
36 if (is->nwaiters != 0)
37 {
38 puts ("nwaiters not initialized");
39 return 1;
40 }
41
42 if (pthread_create (&th, NULL, tf, &s) != 0)
43 {
44 puts ("pthread_create failed");
45 return 1;
46 }
47
48 sleep (1);
49
50 if (pthread_cancel (th) != 0)
51 {
52 puts ("pthread_cancel failed");
53 return 1;
54 }
55
56 void *r;
57 if (pthread_join (th, &r) != 0)
58 {
59 puts ("pthread_join failed");
60 return 1;
61 }
62 if (r != PTHREAD_CANCELED && --tries > 0)
63 {
64 /* Maybe we get the scheduling right the next time. */
65 sem_destroy (&s);
66 goto again;
67 }
68
69 if (is->nwaiters != 0)
70 {
71 puts ("nwaiters not reset");
72 return 1;
73 }
74
75 return 0;
76}