chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / linuxthreads / Examples / ex11.c
1 /* Test program for timedout read/write lock functions.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
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 License as
7    published by the Free Software Foundation; either version 2.1 of the
8    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; 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.  */
19
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
28
29 #define NWRITERS 15
30 #define WRITETRIES 10
31 #define NREADERS 15
32 #define READTRIES 15
33
34 #define TIMEOUT 1000000
35 #define DELAY   1000000
36
37 static pthread_rwlock_t lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP;
38
39
40 static void *
41 writer_thread (void *nr)
42 {
43   struct timespec ts;
44   struct timespec delay;
45   int n;
46
47   ts.tv_sec = 0;
48   ts.tv_nsec = TIMEOUT;
49
50   delay.tv_sec = 0;
51   delay.tv_nsec = DELAY;
52
53   for (n = 0; n < WRITETRIES; ++n)
54     {
55       do
56         {
57           clock_gettime (CLOCK_REALTIME, &ts);
58
59           ts.tv_nsec += 2 * TIMEOUT;
60
61           printf ("writer thread %ld tries again\n", (long int) nr);
62         }
63       //while (pthread_rwlock_wrlock (&lock), 0);
64       while (pthread_rwlock_timedwrlock (&lock, &ts) == ETIMEDOUT);
65
66       printf ("writer thread %ld succeeded\n", (long int) nr);
67
68       nanosleep (&delay, NULL);
69
70       pthread_rwlock_unlock (&lock);
71
72       printf ("writer thread %ld released\n", (long int) nr);
73     }
74
75   return NULL;
76 }
77
78
79 static void *
80 reader_thread (void *nr)
81 {
82   struct timespec ts;
83   struct timespec delay;
84   int n;
85
86   delay.tv_sec = 0;
87   delay.tv_nsec = DELAY;
88
89   for (n = 0; n < READTRIES; ++n)
90     {
91       do
92         {
93           clock_gettime (CLOCK_REALTIME, &ts);
94
95           ts.tv_nsec += TIMEOUT;
96
97           printf ("reader thread %ld tries again\n", (long int) nr);
98         }
99       //while (pthread_rwlock_rdlock (&lock), 0);
100       while (pthread_rwlock_timedrdlock (&lock, &ts) == ETIMEDOUT);
101
102       printf ("reader thread %ld succeeded\n", (long int) nr);
103
104       nanosleep (&delay, NULL);
105
106       pthread_rwlock_unlock (&lock);
107
108       printf ("reader thread %ld released\n", (long int) nr);
109     }
110
111   return NULL;
112 }
113
114
115 int
116 main (void)
117 {
118   pthread_t thwr[NWRITERS];
119   pthread_t thrd[NREADERS];
120   int n;
121   void *res;
122
123   /* Make standard error the same as standard output.  */
124   dup2 (1, 2);
125
126   /* Make sure we see all message, even those on stdout.  */
127   setvbuf (stdout, NULL, _IONBF, 0);
128
129   for (n = 0; n < NWRITERS; ++n)
130     {
131       int err = pthread_create (&thwr[n], NULL, writer_thread,
132                                 (void *) (long int) n);
133
134       if (err != 0)
135         error (EXIT_FAILURE, err, "cannot create writer thread");
136     }
137
138   for (n = 0; n < NREADERS; ++n)
139     {
140       int err = pthread_create (&thrd[n], NULL, reader_thread,
141                                 (void *) (long int) n);
142
143       if (err != 0)
144         error (EXIT_FAILURE, err, "cannot create reader thread");
145     }
146
147   /* Wait for all the threads.  */
148   for (n = 0; n < NWRITERS; ++n)
149     pthread_join (thwr[n], &res);
150   for (n = 0; n < NREADERS; ++n)
151     pthread_join (thrd[n], &res);
152
153   return 0;
154 }