Warning: That file was not part of the compilation database. It may have many parsing errors.

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

Warning: That file was not part of the compilation database. It may have many parsing errors.