file tst-cputimer2.c was initially added on branch fedora-branch.
[glibc/history.git] / nptl / tst-rwlock9.c
bloba5522ce48410d4b9c43f2ce768cd3475d7296057
1 /* Test program for timedout read/write lock functions.
2 Copyright (C) 2000, 2003 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
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 License as
7 published by the Free Software Foundation; either version 2.1 of the
8 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; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/time.h>
30 #define NWRITERS 15
31 #define WRITETRIES 10
32 #define NREADERS 15
33 #define READTRIES 15
35 #define TIMEOUT 1000000
36 #define DELAY 1000000
38 #ifndef INIT
39 # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
40 #endif
42 static pthread_rwlock_t lock = INIT;
45 static void *
46 writer_thread (void *nr)
48 struct timespec ts;
49 struct timespec delay;
50 int n;
52 delay.tv_sec = 0;
53 delay.tv_nsec = DELAY;
55 for (n = 0; n < WRITETRIES; ++n)
57 int e;
60 struct timeval tv;
61 (void) gettimeofday (&tv, NULL);
62 TIMEVAL_TO_TIMESPEC (&tv, &ts);
64 ts.tv_nsec += 2 * TIMEOUT;
65 if (ts.tv_nsec >= 1000000000)
67 ts.tv_nsec -= 1000000000;
68 ++ts.tv_sec;
71 printf ("writer thread %ld tries again\n", (long int) nr);
73 e = pthread_rwlock_timedwrlock (&lock, &ts);
74 if (e != 0 && e != ETIMEDOUT)
76 puts ("timedwrlock failed");
77 exit (1);
80 while (e == ETIMEDOUT);
82 printf ("writer thread %ld succeeded\n", (long int) nr);
84 nanosleep (&delay, NULL);
86 if (pthread_rwlock_unlock (&lock) != 0)
88 puts ("unlock for writer failed");
89 exit (1);
92 printf ("writer thread %ld released\n", (long int) nr);
95 return NULL;
99 static void *
100 reader_thread (void *nr)
102 struct timespec ts;
103 struct timespec delay;
104 int n;
106 delay.tv_sec = 0;
107 delay.tv_nsec = DELAY;
109 for (n = 0; n < READTRIES; ++n)
111 int e;
114 struct timeval tv;
115 (void) gettimeofday (&tv, NULL);
116 TIMEVAL_TO_TIMESPEC (&tv, &ts);
118 ts.tv_nsec += TIMEOUT;
119 if (ts.tv_nsec >= 1000000000)
121 ts.tv_nsec -= 1000000000;
122 ++ts.tv_sec;
125 printf ("reader thread %ld tries again\n", (long int) nr);
127 e = pthread_rwlock_timedrdlock (&lock, &ts);
128 if (e != 0 && e != ETIMEDOUT)
130 puts ("timedrdlock failed");
131 exit (1);
134 while (e == ETIMEDOUT);
136 printf ("reader thread %ld succeeded\n", (long int) nr);
138 nanosleep (&delay, NULL);
140 if (pthread_rwlock_unlock (&lock) != 0)
142 puts ("unlock for reader failed");
143 exit (1);
146 printf ("reader thread %ld released\n", (long int) nr);
149 return NULL;
153 static int
154 do_test (void)
156 pthread_t thwr[NWRITERS];
157 pthread_t thrd[NREADERS];
158 int n;
159 void *res;
161 /* Make standard error the same as standard output. */
162 dup2 (1, 2);
164 /* Make sure we see all message, even those on stdout. */
165 setvbuf (stdout, NULL, _IONBF, 0);
167 for (n = 0; n < NWRITERS; ++n)
168 if (pthread_create (&thwr[n], NULL, writer_thread,
169 (void *) (long int) n) != 0)
171 puts ("writer create failed");
172 exit (1);
175 for (n = 0; n < NREADERS; ++n)
176 if (pthread_create (&thrd[n], NULL, reader_thread,
177 (void *) (long int) n) != 0)
179 puts ("reader create failed");
180 exit (1);
183 /* Wait for all the threads. */
184 for (n = 0; n < NWRITERS; ++n)
185 if (pthread_join (thwr[n], &res) != 0)
187 puts ("writer join failed");
188 exit (1);
190 for (n = 0; n < NREADERS; ++n)
191 if (pthread_join (thrd[n], &res) != 0)
193 puts ("reader join failed");
194 exit (1);
197 return 0;
200 #undef TIMEOUT
201 #define TIMEOUT 30
202 #define TEST_FUNCTION do_test ()
203 #include "../test-skeleton.c"