1 | /* Copyright (C) 2003 Free Software Foundation, Inc.
|
---|
2 | This file is part of the GNU C Library.
|
---|
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
|
---|
4 |
|
---|
5 | The GNU C Library is free software; you can redistribute it and/or
|
---|
6 | modify it under the terms of the GNU Lesser General Public
|
---|
7 | License as published by the Free Software Foundation; either
|
---|
8 | version 2.1 of the License, or (at your option) any later version.
|
---|
9 |
|
---|
10 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | Lesser General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU Lesser General Public
|
---|
16 | License along with the GNU C Library; if not, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
20 | #include <errno.h>
|
---|
21 | #include <pthread.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <time.h>
|
---|
25 | #include <unistd.h>
|
---|
26 |
|
---|
27 |
|
---|
28 | static pthread_barrier_t b;
|
---|
29 |
|
---|
30 |
|
---|
31 | /* Cleanup handling test. */
|
---|
32 | static int cl_called;
|
---|
33 |
|
---|
34 | static void
|
---|
35 | cl (void *arg)
|
---|
36 | {
|
---|
37 | ++cl_called;
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | static void *
|
---|
42 | tf (void *arg)
|
---|
43 | {
|
---|
44 | int r = pthread_barrier_wait (&b);
|
---|
45 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
46 | {
|
---|
47 | puts ("barrier_wait failed");
|
---|
48 | exit (1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | pthread_cleanup_push (cl, NULL);
|
---|
52 |
|
---|
53 | struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
|
---|
54 | TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
|
---|
55 |
|
---|
56 | pthread_cleanup_pop (0);
|
---|
57 |
|
---|
58 | puts ("clock_nanosleep returned");
|
---|
59 |
|
---|
60 | exit (1);
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | static int
|
---|
65 | do_test (void)
|
---|
66 | {
|
---|
67 | if (pthread_barrier_init (&b, NULL, 2) != 0)
|
---|
68 | {
|
---|
69 | puts ("barrier_init failed");
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | pthread_t th;
|
---|
74 | if (pthread_create (&th, NULL, tf, NULL) != 0)
|
---|
75 | {
|
---|
76 | puts ("1st create failed");
|
---|
77 | return 1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int r = pthread_barrier_wait (&b);
|
---|
81 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
82 | {
|
---|
83 | puts ("barrier_wait failed");
|
---|
84 | exit (1);
|
---|
85 | }
|
---|
86 |
|
---|
87 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
|
---|
88 | while (nanosleep (&ts, &ts) != 0)
|
---|
89 | continue;
|
---|
90 |
|
---|
91 | puts ("going to cancel in-time");
|
---|
92 | if (pthread_cancel (th) != 0)
|
---|
93 | {
|
---|
94 | puts ("1st cancel failed");
|
---|
95 | return 1;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void *status;
|
---|
99 | if (pthread_join (th, &status) != 0)
|
---|
100 | {
|
---|
101 | puts ("1st join failed");
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 | if (status != PTHREAD_CANCELED)
|
---|
105 | {
|
---|
106 | puts ("1st thread not canceled");
|
---|
107 | return 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (cl_called == 0)
|
---|
111 | {
|
---|
112 | puts ("cleanup handler not called");
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 | if (cl_called > 1)
|
---|
116 | {
|
---|
117 | puts ("cleanup handler called more than once");
|
---|
118 | return 1;
|
---|
119 | }
|
---|
120 |
|
---|
121 | puts ("in-time cancellation succeeded");
|
---|
122 |
|
---|
123 |
|
---|
124 | cl_called = 0;
|
---|
125 |
|
---|
126 | if (pthread_create (&th, NULL, tf, NULL) != 0)
|
---|
127 | {
|
---|
128 | puts ("2nd create failed");
|
---|
129 | return 1;
|
---|
130 | }
|
---|
131 |
|
---|
132 | puts ("going to cancel early");
|
---|
133 | if (pthread_cancel (th) != 0)
|
---|
134 | {
|
---|
135 | puts ("2nd cancel failed");
|
---|
136 | return 1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | r = pthread_barrier_wait (&b);
|
---|
140 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
141 | {
|
---|
142 | puts ("barrier_wait failed");
|
---|
143 | exit (1);
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (pthread_join (th, &status) != 0)
|
---|
147 | {
|
---|
148 | puts ("2nd join failed");
|
---|
149 | return 1;
|
---|
150 | }
|
---|
151 | if (status != PTHREAD_CANCELED)
|
---|
152 | {
|
---|
153 | puts ("2nd thread not canceled");
|
---|
154 | return 1;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (cl_called == 0)
|
---|
158 | {
|
---|
159 | printf ("cleanup handler not called\n");
|
---|
160 | return 1;
|
---|
161 | }
|
---|
162 | if (cl_called > 1)
|
---|
163 | {
|
---|
164 | printf ("cleanup handler called more than once\n");
|
---|
165 | return 1;
|
---|
166 | }
|
---|
167 |
|
---|
168 | puts ("early cancellation succeeded");
|
---|
169 |
|
---|
170 | return 0;
|
---|
171 | }
|
---|
172 |
|
---|
173 | #define TEST_FUNCTION do_test ()
|
---|
174 | #include "../test-skeleton.c"
|
---|