1 | /* Copyright (C) 2003 Free Software Foundation, Inc.
|
---|
2 | This file is part of the GNU C Library.
|
---|
3 | Contributed by Jakub Jelinek <jakub@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 <stdbool.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <time.h>
|
---|
27 | #include <sys/time.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | typedef struct
|
---|
31 | {
|
---|
32 | pthread_cond_t cond;
|
---|
33 | pthread_mutex_t lock;
|
---|
34 | pthread_t h;
|
---|
35 | } T;
|
---|
36 |
|
---|
37 |
|
---|
38 | static volatile bool done;
|
---|
39 |
|
---|
40 |
|
---|
41 | static void *
|
---|
42 | tf (void *arg)
|
---|
43 | {
|
---|
44 | puts ("child created");
|
---|
45 |
|
---|
46 | if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
|
---|
47 | || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
|
---|
48 | {
|
---|
49 | puts ("cannot set cancellation options");
|
---|
50 | exit (1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | T *t = (T *) arg;
|
---|
54 |
|
---|
55 | if (pthread_mutex_lock (&t->lock) != 0)
|
---|
56 | {
|
---|
57 | puts ("child: lock failed");
|
---|
58 | exit (1);
|
---|
59 | }
|
---|
60 |
|
---|
61 | done = true;
|
---|
62 |
|
---|
63 | if (pthread_cond_signal (&t->cond) != 0)
|
---|
64 | {
|
---|
65 | puts ("child: cond_signal failed");
|
---|
66 | exit (1);
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (pthread_cond_wait (&t->cond, &t->lock) != 0)
|
---|
70 | {
|
---|
71 | puts ("child: cond_wait failed");
|
---|
72 | exit (1);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (pthread_mutex_unlock (&t->lock) != 0)
|
---|
76 | {
|
---|
77 | puts ("child: unlock failed");
|
---|
78 | exit (1);
|
---|
79 | }
|
---|
80 |
|
---|
81 | return NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | static int
|
---|
86 | do_test (void)
|
---|
87 | {
|
---|
88 | int i;
|
---|
89 | #define N 100
|
---|
90 | T *t[N];
|
---|
91 | for (i = 0; i < N; ++i)
|
---|
92 | {
|
---|
93 | printf ("round %d\n", i);
|
---|
94 |
|
---|
95 | t[i] = (T *) malloc (sizeof (T));
|
---|
96 | if (t[i] == NULL)
|
---|
97 | {
|
---|
98 | puts ("out of memory");
|
---|
99 | exit (1);
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (pthread_mutex_init (&t[i]->lock, NULL) != 0
|
---|
103 | || pthread_cond_init (&t[i]->cond, NULL) != 0)
|
---|
104 | {
|
---|
105 | puts ("an _init function failed");
|
---|
106 | exit (1);
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (pthread_mutex_lock (&t[i]->lock) != 0)
|
---|
110 | {
|
---|
111 | puts ("initial mutex_lock failed");
|
---|
112 | exit (1);
|
---|
113 | }
|
---|
114 |
|
---|
115 | done = false;
|
---|
116 |
|
---|
117 | if (pthread_create (&t[i]->h, NULL, tf, t[i]) != 0)
|
---|
118 | {
|
---|
119 | puts ("pthread_create failed");
|
---|
120 | exit (1);
|
---|
121 | }
|
---|
122 |
|
---|
123 | do
|
---|
124 | if (pthread_cond_wait (&t[i]->cond, &t[i]->lock) != 0)
|
---|
125 | {
|
---|
126 | puts ("cond_wait failed");
|
---|
127 | exit (1);
|
---|
128 | }
|
---|
129 | while (! done);
|
---|
130 |
|
---|
131 | /* Release the lock since the cancel handler will get it. */
|
---|
132 | if (pthread_mutex_unlock (&t[i]->lock) != 0)
|
---|
133 | {
|
---|
134 | puts ("mutex_unlock failed");
|
---|
135 | exit (1);
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (pthread_cancel (t[i]->h) != 0)
|
---|
139 | {
|
---|
140 | puts ("cancel failed");
|
---|
141 | exit (1);
|
---|
142 | }
|
---|
143 |
|
---|
144 | puts ("parent: joining now");
|
---|
145 |
|
---|
146 | void *result;
|
---|
147 | if (pthread_join (t[i]->h, &result) != 0)
|
---|
148 | {
|
---|
149 | puts ("join failed");
|
---|
150 | exit (1);
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (result != PTHREAD_CANCELED)
|
---|
154 | {
|
---|
155 | puts ("result != PTHREAD_CANCELED");
|
---|
156 | exit (1);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | for (i = 0; i < N; ++i)
|
---|
161 | free (t[i]);
|
---|
162 |
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | #define TEST_FUNCTION do_test ()
|
---|
168 | #include "../test-skeleton.c"
|
---|