(__nss_lookup_function): En/Decrypt cached function pointers.
[glibc/history.git] / nptl / tst-cond20.c
blob18918f32e6090ec8e6a767b97c53d70ec65e4554
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.
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.
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.
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. */
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #define N 10
27 #define ROUNDS 1000
28 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29 static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
30 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
31 static pthread_barrier_t b;
32 static int count;
34 static void *
35 tf (void *p)
37 int i;
38 for (i = 0; i < ROUNDS; ++i)
40 pthread_mutex_lock (&mut);
42 if (++count == N)
43 pthread_cond_signal (&cond2);
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
57 pthread_mutex_unlock (&mut);
59 int err = pthread_barrier_wait (&b);
60 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
62 puts ("child: barrier_wait failed");
63 exit (1);
66 err = pthread_barrier_wait (&b);
67 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
69 puts ("child: barrier_wait failed");
70 exit (1);
74 return NULL;
78 static int
79 do_test (void)
81 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
83 puts ("barrier_init failed");
84 return 1;
87 pthread_mutex_lock (&mut);
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)
94 printf ("cannot create thread %d: %s\n", i, strerror (err));
95 return 1;
98 for (i = 0; i < ROUNDS; ++i)
100 pthread_cond_wait (&cond2, &mut);
102 if (i & 1)
103 pthread_mutex_unlock (&mut);
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
112 for (j = 0; j < (i / 8) % N; ++j)
113 pthread_cond_signal (&cond);
114 pthread_cond_broadcast (&cond);
117 if ((i & 1) == 0)
118 pthread_mutex_unlock (&mut);
120 err = pthread_cond_destroy (&cond);
121 if (err)
123 printf ("pthread_cond_destroy failed: %s\n", strerror (err));
124 return 1;
127 /* Now clobber the cond variable which has been successfully
128 destroyed above. */
129 memset (&cond, (char) i, sizeof (cond));
131 err = pthread_barrier_wait (&b);
132 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
134 puts ("parent: barrier_wait failed");
135 return 1;
138 pthread_mutex_lock (&mut);
140 err = pthread_barrier_wait (&b);
141 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
143 puts ("parent: barrier_wait failed");
144 return 1;
147 count = 0;
148 err = pthread_cond_init (&cond, NULL);
149 if (err)
151 printf ("pthread_cond_init failed: %s\n", strerror (err));
152 return 1;
156 for (i = 0; i < N; ++i)
157 if ((err = pthread_join (th[i], NULL)) != 0)
159 printf ("failed to join thread %d: %s\n", i, strerror (err));
160 return 1;
163 puts ("done");
165 return 0;
169 #define TEST_FUNCTION do_test ()
170 #include "../test-skeleton.c"