source: vendor/glibc-tests/2005-06-14/nptl/tst-cond20.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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 <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#define N 10
27#define ROUNDS 1000
28static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
30static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
31static pthread_barrier_t b;
32static int count;
33
34static void *
35tf (void *p)
36{
37 int i;
38 for (i = 0; i < ROUNDS; ++i)
39 {
40 pthread_mutex_lock (&mut);
41
42 if (++count == N)
43 pthread_cond_signal (&cond2);
44
45#ifdef TIMED
46 struct timeval tv;
47 gettimeofday (&tv, NULL);
48 struct timespec ts;
49 /* Wait three seconds. */
50 ts.tv_sec = tv.tv_sec + 3;
51 ts.tv_nsec = tv.tv_usec * 1000;
52 pthread_cond_timedwait (&cond, &mut, &ts);
53#else
54 pthread_cond_wait (&cond, &mut);
55#endif
56
57 pthread_mutex_unlock (&mut);
58
59 int err = pthread_barrier_wait (&b);
60 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
61 {
62 puts ("child: barrier_wait failed");
63 exit (1);
64 }
65
66 err = pthread_barrier_wait (&b);
67 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
68 {
69 puts ("child: barrier_wait failed");
70 exit (1);
71 }
72 }
73
74 return NULL;
75}
76
77
78static int
79do_test (void)
80{
81 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
82 {
83 puts ("barrier_init failed");
84 return 1;
85 }
86
87 pthread_mutex_lock (&mut);
88
89 int i, j, err;
90 pthread_t th[N];
91 for (i = 0; i < N; ++i)
92 if ((err = pthread_create (&th[i], NULL, tf, NULL)) != 0)
93 {
94 printf ("cannot create thread %d: %s\n", i, strerror (err));
95 return 1;
96 }
97
98 for (i = 0; i < ROUNDS; ++i)
99 {
100 pthread_cond_wait (&cond2, &mut);
101
102 if (i & 1)
103 pthread_mutex_unlock (&mut);
104
105 if (i & 2)
106 pthread_cond_broadcast (&cond);
107 else if (i & 4)
108 for (j = 0; j < N; ++j)
109 pthread_cond_signal (&cond);
110 else
111 {
112 for (j = 0; j < (i / 8) % N; ++j)
113 pthread_cond_signal (&cond);
114 pthread_cond_broadcast (&cond);
115 }
116
117 if ((i & 1) == 0)
118 pthread_mutex_unlock (&mut);
119
120 err = pthread_cond_destroy (&cond);
121 if (err)
122 {
123 printf ("pthread_cond_destroy failed: %s\n", strerror (err));
124 return 1;
125 }
126
127 /* Now clobber the cond variable which has been successfully
128 destroyed above. */
129 memset (&cond, (char) i, sizeof (cond));
130
131 err = pthread_barrier_wait (&b);
132 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
133 {
134 puts ("parent: barrier_wait failed");
135 return 1;
136 }
137
138 pthread_mutex_lock (&mut);
139
140 err = pthread_barrier_wait (&b);
141 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
142 {
143 puts ("parent: barrier_wait failed");
144 return 1;
145 }
146
147 count = 0;
148 err = pthread_cond_init (&cond, NULL);
149 if (err)
150 {
151 printf ("pthread_cond_init failed: %s\n", strerror (err));
152 return 1;
153 }
154 }
155
156 for (i = 0; i < N; ++i)
157 if ((err = pthread_join (th[i], NULL)) != 0)
158 {
159 printf ("failed to join thread %d: %s\n", i, strerror (err));
160 return 1;
161 }
162
163 puts ("done");
164
165 return 0;
166}
167
168
169#define TEST_FUNCTION do_test ()
170#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.