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. */
35 #define TIMEOUT 1000000
39 # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
42 static pthread_rwlock_t lock = INIT;
46 writer_thread (void *nr)
49 struct timespec delay;
53 delay.tv_nsec = DELAY;
55 for (n = 0; n < WRITETRIES; ++n)
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;
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");
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");
92 printf ("writer thread %ld released\n", (long int) nr);
100 reader_thread (void *nr)
103 struct timespec delay;
107 delay.tv_nsec = DELAY;
109 for (n = 0; n < READTRIES; ++n)
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;
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");
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");
146 printf ("reader thread %ld released\n", (long int) nr);
156 pthread_t thwr[NWRITERS];
157 pthread_t thrd[NREADERS];
161 /* Make standard error the same as standard output. */
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");
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");
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");
190 for (n = 0; n < NREADERS; ++n)
191 if (pthread_join (thrd[n], &res) != 0)
193 puts ("reader join failed");
202 #define TEST_FUNCTION do_test ()
203 #include "../test-skeleton.c"