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
38 static pthread_rwlock_t lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP;
42 writer_thread (void *nr)
45 struct timespec delay;
49 delay.tv_nsec = DELAY;
51 for (n = 0; n < WRITETRIES; ++n)
57 (void) gettimeofday (&tv, NULL);
58 TIMEVAL_TO_TIMESPEC (&tv, &ts);
60 ts.tv_nsec += 2 * TIMEOUT;
62 printf ("writer thread %ld tries again\n", (long int) nr);
64 e = pthread_rwlock_timedwrlock (&lock, &ts);
65 if (e != 0 && e != ETIMEDOUT)
67 puts ("timedwrlock failed");
71 while (e == ETIMEDOUT);
73 printf ("writer thread %ld succeeded\n", (long int) nr);
75 nanosleep (&delay, NULL);
77 if (pthread_rwlock_unlock (&lock) != 0)
79 puts ("unlock for writer failed");
83 printf ("writer thread %ld released\n", (long int) nr);
91 reader_thread (void *nr)
94 struct timespec delay;
98 delay.tv_nsec = DELAY;
100 for (n = 0; n < READTRIES; ++n)
106 (void) gettimeofday (&tv, NULL);
107 TIMEVAL_TO_TIMESPEC (&tv, &ts);
109 ts.tv_nsec += TIMEOUT;
111 printf ("reader thread %ld tries again\n", (long int) nr);
113 e = pthread_rwlock_timedrdlock (&lock, &ts);
114 if (e != 0 && e != ETIMEDOUT)
116 puts ("timedrdlock failed");
120 while (e == ETIMEDOUT);
122 printf ("reader thread %ld succeeded\n", (long int) nr);
124 nanosleep (&delay, NULL);
126 if (pthread_rwlock_unlock (&lock) != 0)
128 puts ("unlock for reader failed");
132 printf ("reader thread %ld released\n", (long int) nr);
142 pthread_t thwr[NWRITERS];
143 pthread_t thrd[NREADERS];
147 /* Make standard error the same as standard output. */
150 /* Make sure we see all message, even those on stdout. */
151 setvbuf (stdout, NULL, _IONBF, 0);
153 for (n = 0; n < NWRITERS; ++n)
154 if (pthread_create (&thwr[n], NULL, writer_thread,
155 (void *) (long int) n) != 0)
157 puts ("writer create failed");
161 for (n = 0; n < NREADERS; ++n)
162 if (pthread_create (&thrd[n], NULL, reader_thread,
163 (void *) (long int) n) != 0)
165 puts ("reader create failed");
169 /* Wait for all the threads. */
170 for (n = 0; n < NWRITERS; ++n)
171 if (pthread_join (thwr[n], &res) != 0)
173 puts ("writer join failed");
176 for (n = 0; n < NREADERS; ++n)
177 if (pthread_join (thrd[n], &res) != 0)
179 puts ("reader join failed");
188 #define TEST_FUNCTION do_test ()
189 #include "../test-skeleton.c"