]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-cond7.c
Update.
[thirdparty/glibc.git] / nptl / tst-cond7.c
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 T *t = (T *) arg;
47
48 if (pthread_mutex_lock (&t->lock) != 0)
49 {
50 puts ("child: lock failed");
51 exit (1);
52 }
53
54 done = true;
55
56 if (pthread_cond_signal (&t->cond) != 0)
57 {
58 puts ("child: cond_signal failed");
59 exit (1);
60 }
61
62 if (pthread_cond_wait (&t->cond, &t->lock) != 0)
63 {
64 puts ("child: cond_wait failed");
65 exit (1);
66 }
67
68 if (pthread_mutex_unlock (&t->lock) != 0)
69 {
70 puts ("child: unlock failed");
71 exit (1);
72 }
73
74 return NULL;
75 }
76
77
78 static int
79 do_test (void)
80 {
81 int i;
82 #define N 100
83 T *t[N];
84 for (i = 0; i < N; ++i)
85 {
86 printf ("round %d\n", i);
87
88 t[i] = (T *) malloc (sizeof (T));
89 if (t[i] == NULL)
90 {
91 puts ("out of memory");
92 exit (1);
93 }
94
95 if (pthread_mutex_init (&t[i]->lock, NULL) != 0
96 || pthread_cond_init (&t[i]->cond, NULL) != 0)
97 {
98 puts ("an _init function failed");
99 exit (1);
100 }
101
102 if (pthread_mutex_lock (&t[i]->lock) != 0)
103 {
104 puts ("initial mutex_lock failed");
105 exit (1);
106 }
107
108 done = false;
109
110 if (pthread_create (&t[i]->h, NULL, tf, t[i]) != 0)
111 {
112 puts ("pthread_create failed");
113 exit (1);
114 }
115
116 do
117 if (pthread_cond_wait (&t[i]->cond, &t[i]->lock) != 0)
118 {
119 puts ("cond_wait failed");
120 exit (1);
121 }
122 while (! done);
123
124 if (pthread_cancel (t[i]->h) != 0)
125 {
126 puts ("cancel failed");
127 exit (1);
128 }
129
130 puts ("parent: joining now");
131
132 void *result;
133 if (pthread_join (t[i]->h, &result) != 0)
134 {
135 puts ("join failed");
136 exit (1);
137 }
138
139 if (result != PTHREAD_CANCELED)
140 {
141 puts ("result != PTHREAD_CANCELED");
142 exit (1);
143 }
144 }
145
146 for (i = 0; i < N; ++i)
147 free (t[i]);
148
149 return 0;
150 }
151
152
153 #define TEST_FUNCTION do_test ()
154 #include "../test-skeleton.c"