]>
Commit | Line | Data |
---|---|---|
c37cae9e UD |
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 <error.h> | |
21 | #include <pthread.h> | |
22 | #include <stdbool.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | ||
27 | #define N 10 | |
28 | #define ROUNDS 100 | |
29 | ||
30 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | |
31 | static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; | |
32 | static pthread_barrier_t bN1; | |
33 | static pthread_barrier_t b2; | |
34 | ||
35 | ||
36 | static void * | |
37 | tf (void *p) | |
38 | { | |
39 | if (pthread_mutex_lock (&mut) != 0) | |
40 | { | |
41 | puts ("child: 1st mutex_lock failed"); | |
42 | exit (1); | |
43 | } | |
44 | ||
45 | int e = pthread_barrier_wait (&b2); | |
46 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | |
47 | { | |
48 | puts ("child: 1st barrier_wait failed"); | |
49 | exit (1); | |
50 | } | |
51 | ||
52 | if (pthread_cond_wait (&cond, &mut) != 0) | |
53 | { | |
54 | puts ("child: cond_wait failed"); | |
55 | exit (1); | |
56 | } | |
57 | ||
58 | if (pthread_mutex_unlock (&mut) != 0) | |
59 | { | |
60 | puts ("child: mutex_unlock failed"); | |
61 | exit (1); | |
62 | } | |
63 | ||
64 | e = pthread_barrier_wait (&bN1); | |
65 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | |
66 | { | |
67 | puts ("child: 2nd barrier_wait failed"); | |
68 | exit (1); | |
69 | } | |
70 | ||
71 | return NULL; | |
72 | } | |
73 | ||
74 | ||
75 | static int | |
76 | do_test (void) | |
77 | { | |
78 | if (pthread_barrier_init (&bN1, NULL, N + 1) != 0) | |
79 | { | |
80 | puts ("barrier_init failed"); | |
81 | exit (1); | |
82 | } | |
83 | ||
84 | if (pthread_barrier_init (&b2, NULL, 2) != 0) | |
85 | { | |
86 | puts ("barrier_init failed"); | |
87 | exit (1); | |
88 | } | |
89 | ||
90 | int r; | |
91 | for (r = 0; r < ROUNDS; ++r) | |
92 | { | |
93 | printf ("round %d\n", r + 1); | |
94 | ||
95 | int i; | |
96 | pthread_t th[N]; | |
97 | for (i = 0; i < N; ++i) | |
98 | { | |
99 | if (pthread_create (&th[i], NULL, tf, NULL) != 0) | |
100 | { | |
101 | puts ("create failed"); | |
102 | exit (1); | |
103 | } | |
104 | ||
105 | int e = pthread_barrier_wait (&b2); | |
106 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | |
107 | { | |
108 | puts ("parent: 1st barrier_wait failed"); | |
109 | exit (1); | |
110 | } | |
111 | } | |
112 | ||
113 | if (pthread_mutex_lock (&mut) != 0) | |
114 | { | |
115 | puts ("parent: mutex_lock failed"); | |
116 | exit (1); | |
117 | } | |
118 | if (pthread_mutex_unlock (&mut) != 0) | |
119 | { | |
120 | puts ("parent: mutex_unlock failed"); | |
121 | exit (1); | |
122 | } | |
123 | ||
124 | /* N single signal calls. Without locking. This tests that no | |
125 | signal gets lost. */ | |
126 | for (i = 0; i < N; ++i) | |
127 | if (pthread_cond_signal (&cond) != 0) | |
128 | { | |
129 | puts ("cond_signal failed"); | |
130 | exit (1); | |
131 | } | |
132 | ||
133 | int e = pthread_barrier_wait (&bN1); | |
134 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | |
135 | { | |
136 | puts ("parent: 2nd barrier_wait failed"); | |
137 | exit (1); | |
138 | } | |
139 | ||
140 | for (i = 0; i < N; ++i) | |
141 | if (pthread_join (th[i], NULL) != 0) | |
142 | { | |
143 | puts ("join failed"); | |
144 | exit (1); | |
145 | } | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | ||
152 | #define TEST_FUNCTION do_test () | |
153 | #include "../test-skeleton.c" |