]>
Commit | Line | Data |
---|---|---|
26a526fa | 1 | /* Test program for timedout read/write lock functions. |
dff8da6b | 2 | Copyright (C) 2000-2024 Free Software Foundation, Inc. |
26a526fa UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public License as | |
6 | published by the Free Software Foundation; either version 2.1 of the | |
7 | License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; see the file COPYING.LIB. If |
5a82c748 | 16 | not, see <https://www.gnu.org/licenses/>. */ |
26a526fa UD |
17 | |
18 | #include <errno.h> | |
19 | #include <error.h> | |
20 | #include <pthread.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <time.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/time.h> | |
495514ee MC |
26 | #include <support/check.h> |
27 | #include <support/timespec.h> | |
e996fa72 | 28 | #include <support/xthread.h> |
26a526fa UD |
29 | |
30 | ||
31 | #define NWRITERS 15 | |
32 | #define WRITETRIES 10 | |
33 | #define NREADERS 15 | |
34 | #define READTRIES 15 | |
35 | ||
495514ee MC |
36 | static const struct timespec timeout = { 0,1000000 }; |
37 | static const struct timespec delay = { 0, 1000000 }; | |
26a526fa | 38 | |
cc25c8b4 TR |
39 | #ifndef KIND |
40 | # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP | |
d148ed25 UD |
41 | #endif |
42 | ||
e996fa72 MC |
43 | /* A bogus clock value that tells the tests to use pthread_rwlock_timedrdlock |
44 | and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and | |
45 | pthread_rwlock_clockwrlock. */ | |
46 | #define CLOCK_USE_TIMEDLOCK (-1) | |
47 | ||
cc25c8b4 | 48 | static pthread_rwlock_t lock; |
26a526fa | 49 | |
e996fa72 MC |
50 | struct thread_args |
51 | { | |
52 | int nr; | |
53 | clockid_t clockid; | |
54 | const char *fnname; | |
55 | }; | |
26a526fa UD |
56 | |
57 | static void * | |
e996fa72 | 58 | writer_thread (void *arg) |
26a526fa | 59 | { |
e996fa72 MC |
60 | struct thread_args *args = arg; |
61 | const int nr = args->nr; | |
62 | const clockid_t clockid = args->clockid; | |
63 | const clockid_t clockid_for_get = | |
64 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
65 | const char *fnname = args->fnname; | |
66 | ||
26a526fa | 67 | struct timespec ts; |
26a526fa UD |
68 | int n; |
69 | ||
26a526fa UD |
70 | for (n = 0; n < WRITETRIES; ++n) |
71 | { | |
72 | int e; | |
73 | do | |
74 | { | |
e996fa72 | 75 | xclock_gettime (clockid_for_get, &ts); |
26a526fa | 76 | |
495514ee MC |
77 | ts = timespec_add (ts, timeout); |
78 | ts = timespec_add (ts, timeout); | |
26a526fa | 79 | |
e996fa72 | 80 | printf ("writer thread %d tries again\n", nr); |
26a526fa | 81 | |
e996fa72 MC |
82 | e = (clockid == CLOCK_USE_TIMEDLOCK) |
83 | ? pthread_rwlock_timedwrlock (&lock, &ts) | |
84 | : pthread_rwlock_clockwrlock (&lock, clockid, &ts); | |
26a526fa | 85 | if (e != 0 && e != ETIMEDOUT) |
e996fa72 | 86 | FAIL_EXIT1 ("%swrlock failed", fnname); |
26a526fa UD |
87 | } |
88 | while (e == ETIMEDOUT); | |
89 | ||
e996fa72 | 90 | printf ("writer thread %d succeeded\n", nr); |
26a526fa UD |
91 | |
92 | nanosleep (&delay, NULL); | |
93 | ||
94 | if (pthread_rwlock_unlock (&lock) != 0) | |
495514ee | 95 | FAIL_EXIT1 ("unlock for writer failed"); |
26a526fa | 96 | |
e996fa72 | 97 | printf ("writer thread %d released\n", nr); |
26a526fa UD |
98 | } |
99 | ||
100 | return NULL; | |
101 | } | |
102 | ||
103 | ||
104 | static void * | |
e996fa72 | 105 | reader_thread (void *arg) |
26a526fa | 106 | { |
e996fa72 MC |
107 | struct thread_args *args = arg; |
108 | const int nr = args->nr; | |
109 | const clockid_t clockid = args->clockid; | |
110 | const clockid_t clockid_for_get = | |
111 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
112 | const char *fnname = args->fnname; | |
113 | ||
26a526fa | 114 | struct timespec ts; |
26a526fa UD |
115 | int n; |
116 | ||
26a526fa UD |
117 | for (n = 0; n < READTRIES; ++n) |
118 | { | |
119 | int e; | |
120 | do | |
121 | { | |
e996fa72 | 122 | xclock_gettime (clockid_for_get, &ts); |
26a526fa | 123 | |
495514ee | 124 | ts = timespec_add (ts, timeout); |
26a526fa | 125 | |
e996fa72 | 126 | printf ("reader thread %d tries again\n", nr); |
26a526fa | 127 | |
e996fa72 MC |
128 | if (clockid == CLOCK_USE_TIMEDLOCK) |
129 | e = pthread_rwlock_timedrdlock (&lock, &ts); | |
130 | else | |
131 | e = pthread_rwlock_clockrdlock (&lock, clockid, &ts); | |
26a526fa | 132 | if (e != 0 && e != ETIMEDOUT) |
e996fa72 | 133 | FAIL_EXIT1 ("%srdlock failed", fnname); |
26a526fa UD |
134 | } |
135 | while (e == ETIMEDOUT); | |
136 | ||
e996fa72 | 137 | printf ("reader thread %d succeeded\n", nr); |
26a526fa UD |
138 | |
139 | nanosleep (&delay, NULL); | |
140 | ||
141 | if (pthread_rwlock_unlock (&lock) != 0) | |
495514ee | 142 | FAIL_EXIT1 ("unlock for reader failed"); |
26a526fa | 143 | |
e996fa72 | 144 | printf ("reader thread %d released\n", nr); |
26a526fa UD |
145 | } |
146 | ||
147 | return NULL; | |
148 | } | |
149 | ||
150 | ||
151 | static int | |
e996fa72 | 152 | do_test_clock (clockid_t clockid, const char *fnname) |
26a526fa UD |
153 | { |
154 | pthread_t thwr[NWRITERS]; | |
155 | pthread_t thrd[NREADERS]; | |
156 | int n; | |
cc25c8b4 TR |
157 | pthread_rwlockattr_t a; |
158 | ||
159 | if (pthread_rwlockattr_init (&a) != 0) | |
495514ee | 160 | FAIL_EXIT1 ("rwlockattr_t failed"); |
cc25c8b4 TR |
161 | |
162 | if (pthread_rwlockattr_setkind_np (&a, KIND) != 0) | |
495514ee | 163 | FAIL_EXIT1 ("rwlockattr_setkind failed"); |
cc25c8b4 TR |
164 | |
165 | if (pthread_rwlock_init (&lock, &a) != 0) | |
495514ee | 166 | FAIL_EXIT1 ("rwlock_init failed"); |
26a526fa UD |
167 | |
168 | /* Make standard error the same as standard output. */ | |
169 | dup2 (1, 2); | |
170 | ||
171 | /* Make sure we see all message, even those on stdout. */ | |
172 | setvbuf (stdout, NULL, _IONBF, 0); | |
173 | ||
e996fa72 MC |
174 | struct thread_args wargs[NWRITERS]; |
175 | for (n = 0; n < NWRITERS; ++n) { | |
176 | wargs[n].nr = n; | |
177 | wargs[n].clockid = clockid; | |
178 | wargs[n].fnname = fnname; | |
179 | thwr[n] = xpthread_create (NULL, writer_thread, &wargs[n]); | |
180 | } | |
181 | ||
182 | struct thread_args rargs[NREADERS]; | |
183 | for (n = 0; n < NREADERS; ++n) { | |
184 | rargs[n].nr = n; | |
185 | rargs[n].clockid = clockid; | |
186 | rargs[n].fnname = fnname; | |
187 | thrd[n] = xpthread_create (NULL, reader_thread, &rargs[n]); | |
188 | } | |
26a526fa UD |
189 | |
190 | /* Wait for all the threads. */ | |
191 | for (n = 0; n < NWRITERS; ++n) | |
e996fa72 | 192 | xpthread_join (thwr[n]); |
26a526fa | 193 | for (n = 0; n < NREADERS; ++n) |
e996fa72 MC |
194 | xpthread_join (thrd[n]); |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
199 | static int | |
200 | do_test (void) | |
201 | { | |
202 | do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); | |
203 | do_test_clock (CLOCK_REALTIME, "clock(realtime)"); | |
204 | do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); | |
26a526fa UD |
205 | |
206 | return 0; | |
207 | } | |
208 | ||
26a526fa | 209 | #define TIMEOUT 30 |
495514ee | 210 | #include <support/test-driver.c> |