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.
|
---|
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 DELAY 1000000
|
---|
35 |
|
---|
36 | #ifndef INIT
|
---|
37 | # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | static pthread_rwlock_t lock = INIT;
|
---|
41 |
|
---|
42 |
|
---|
43 | static void *
|
---|
44 | writer_thread (void *nr)
|
---|
45 | {
|
---|
46 | struct timespec delay;
|
---|
47 | int n;
|
---|
48 |
|
---|
49 | delay.tv_sec = 0;
|
---|
50 | delay.tv_nsec = DELAY;
|
---|
51 |
|
---|
52 | for (n = 0; n < WRITETRIES; ++n)
|
---|
53 | {
|
---|
54 | printf ("writer thread %ld tries again\n", (long int) nr);
|
---|
55 |
|
---|
56 | if (pthread_rwlock_wrlock (&lock) != 0)
|
---|
57 | {
|
---|
58 | puts ("wrlock failed");
|
---|
59 | exit (1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | printf ("writer thread %ld succeeded\n", (long int) nr);
|
---|
63 |
|
---|
64 | nanosleep (&delay, NULL);
|
---|
65 |
|
---|
66 | if (pthread_rwlock_unlock (&lock) != 0)
|
---|
67 | {
|
---|
68 | puts ("unlock for writer failed");
|
---|
69 | exit (1);
|
---|
70 | }
|
---|
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 delay;
|
---|
83 | int n;
|
---|
84 |
|
---|
85 | delay.tv_sec = 0;
|
---|
86 | delay.tv_nsec = DELAY;
|
---|
87 |
|
---|
88 | for (n = 0; n < READTRIES; ++n)
|
---|
89 | {
|
---|
90 | printf ("reader thread %ld tries again\n", (long int) nr);
|
---|
91 |
|
---|
92 | if (pthread_rwlock_rdlock (&lock) != 0)
|
---|
93 | {
|
---|
94 | puts ("rdlock failed");
|
---|
95 | exit (1);
|
---|
96 | }
|
---|
97 |
|
---|
98 | printf ("reader thread %ld succeeded\n", (long int) nr);
|
---|
99 |
|
---|
100 | nanosleep (&delay, NULL);
|
---|
101 |
|
---|
102 | if (pthread_rwlock_unlock (&lock) != 0)
|
---|
103 | {
|
---|
104 | puts ("unlock for reader failed");
|
---|
105 | exit (1);
|
---|
106 | }
|
---|
107 |
|
---|
108 | printf ("reader thread %ld released\n", (long int) nr);
|
---|
109 | }
|
---|
110 |
|
---|
111 | return NULL;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | static int
|
---|
116 | do_test (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 | if (pthread_create (&thwr[n], NULL, writer_thread,
|
---|
131 | (void *) (long int) n) != 0)
|
---|
132 | {
|
---|
133 | puts ("writer create failed");
|
---|
134 | exit (1);
|
---|
135 | }
|
---|
136 |
|
---|
137 | for (n = 0; n < NREADERS; ++n)
|
---|
138 | if (pthread_create (&thrd[n], NULL, reader_thread,
|
---|
139 | (void *) (long int) n) != 0)
|
---|
140 | {
|
---|
141 | puts ("reader create failed");
|
---|
142 | exit (1);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* Wait for all the threads. */
|
---|
146 | for (n = 0; n < NWRITERS; ++n)
|
---|
147 | if (pthread_join (thwr[n], &res) != 0)
|
---|
148 | {
|
---|
149 | puts ("writer join failed");
|
---|
150 | exit (1);
|
---|
151 | }
|
---|
152 | for (n = 0; n < NREADERS; ++n)
|
---|
153 | if (pthread_join (thrd[n], &res) != 0)
|
---|
154 | {
|
---|
155 | puts ("reader join failed");
|
---|
156 | exit (1);
|
---|
157 | }
|
---|
158 |
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | #define TIMEOUT 30
|
---|
163 | #define TEST_FUNCTION do_test ()
|
---|
164 | #include "../test-skeleton.c"
|
---|