Debian Codegraph @

gnulib-tests/pthread-rwlock.c

raw
1 /* POSIX read-write locks.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 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 This file 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
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2019. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include <pthread.h>
23
24 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
25 # include "windows-timedrwlock.h"
26 #else
27 # include <errno.h>
28 # include <limits.h>
29 # include <sys/time.h>
30 # include <time.h>
31 #endif
32
33 #ifndef MIN
34 # define MIN(a, b) ((a) < (b) ? (a) : (b))
35 #endif
36
37 #if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
38
39 int
40 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
41 {
42 *attr = 0;
43 return 0;
44 }
45
46 int
47 pthread_rwlockattr_destroy (_GL_UNUSED pthread_rwlockattr_t *attr)
48 {
49 return 0;
50 }
51
52 #elif PTHREAD_RWLOCK_BAD_WAITQUEUE
53
54 /* Override pthread_rwlockattr_init, to use the kind PREFER_WRITER_NONRECURSIVE
55 (or possibly PREFER_WRITER) instead of the kind DEFAULT. */
56 int
57 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
58 # undef pthread_rwlockattr_init
59 {
60 int err;
61
62 err = pthread_rwlockattr_init (attr);
63 if (err != 0)
64 return err;
65 err = pthread_rwlockattr_setkind_np (attr,
66 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
67 if (err != 0)
68 {
69 pthread_rwlockattr_destroy (attr);
70 return err;
71 }
72 return 0;
73 }
74
75 #endif
76
77 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
78 /* Use Windows threads. */
79
80 int
81 pthread_rwlock_init (pthread_rwlock_t *lock,
82 _GL_UNUSED const pthread_rwlockattr_t *attr)
83 {
84 glwthread_timedrwlock_init (lock);
85 return 0;
86 }
87
88 int
89 pthread_rwlock_rdlock (pthread_rwlock_t *lock)
90 {
91 return glwthread_timedrwlock_rdlock (lock);
92 }
93
94 int
95 pthread_rwlock_wrlock (pthread_rwlock_t *lock)
96 {
97 return glwthread_timedrwlock_wrlock (lock);
98 }
99
100 int
101 pthread_rwlock_tryrdlock (pthread_rwlock_t *lock)
102 {
103 return glwthread_timedrwlock_tryrdlock (lock);
104 }
105
106 int
107 pthread_rwlock_trywrlock (pthread_rwlock_t *lock)
108 {
109 return glwthread_timedrwlock_trywrlock (lock);
110 }
111
112 int
113 pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
114 const struct timespec *abstime)
115 {
116 return glwthread_timedrwlock_timedrdlock (lock, abstime);
117 }
118
119 int
120 pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
121 const struct timespec *abstime)
122 {
123 return glwthread_timedrwlock_timedwrlock (lock, abstime);
124 }
125
126 int
127 pthread_rwlock_unlock (pthread_rwlock_t *lock)
128 {
129 return glwthread_timedrwlock_unlock (lock);
130 }
131
132 int
133 pthread_rwlock_destroy (pthread_rwlock_t *lock)
134 {
135 return glwthread_timedrwlock_destroy (lock);
136 }
137
138 #elif HAVE_PTHREAD_H
139 /* Provide workarounds for POSIX threads. */
140
141 # if PTHREAD_RWLOCK_UNIMPLEMENTED
142
143 int
144 pthread_rwlock_init (pthread_rwlock_t *lock,
145 _GL_UNUSED const pthread_rwlockattr_t *attr)
146 {
147 int err;
148
149 err = pthread_mutex_init (&lock->lock, NULL);
150 if (err != 0)
151 return err;
152 err = pthread_cond_init (&lock->waiting_readers, NULL);
153 if (err != 0)
154 return err;
155 err = pthread_cond_init (&lock->waiting_writers, NULL);
156 if (err != 0)
157 return err;
158 lock->waiting_writers_count = 0;
159 lock->runcount = 0;
160 return 0;
161 }
162
163 int
164 pthread_rwlock_rdlock (pthread_rwlock_t *lock)
165 {
166 int err;
167
168 err = pthread_mutex_lock (&lock->lock);
169 if (err != 0)
170 return err;
171 /* Test whether only readers are currently running, and whether the runcount
172 field will not overflow, and whether no writer is waiting. The latter
173 condition is because POSIX recommends that "write locks shall take
174 precedence over read locks", to avoid "writer starvation". */
175 while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
176 {
177 /* This thread has to wait for a while. Enqueue it among the
178 waiting_readers. */
179 err = pthread_cond_wait (&lock->waiting_readers, &lock->lock);
180 if (err != 0)
181 {
182 pthread_mutex_unlock (&lock->lock);
183 return err;
184 }
185 }
186 lock->runcount++;
187 return pthread_mutex_unlock (&lock->lock);
188 }
189
190 int
191 pthread_rwlock_wrlock (pthread_rwlock_t *lock)
192 {
193 int err;
194
195 err = pthread_mutex_lock (&lock->lock);
196 if (err != 0)
197 return err;
198 /* Test whether no readers or writers are currently running. */
199 while (!(lock->runcount == 0))
200 {
201 /* This thread has to wait for a while. Enqueue it among the
202 waiting_writers. */
203 lock->waiting_writers_count++;
204 err = pthread_cond_wait (&lock->waiting_writers, &lock->lock);
205 if (err != 0)
206 {
207 lock->waiting_writers_count--;
208 pthread_mutex_unlock (&lock->lock);
209 return err;
210 }
211 lock->waiting_writers_count--;
212 }
213 lock->runcount--; /* runcount becomes -1 */
214 return pthread_mutex_unlock (&lock->lock);
215 }
216
217 int
218 pthread_rwlock_tryrdlock (pthread_rwlock_t *lock)
219 {
220 int err;
221
222 err = pthread_mutex_lock (&lock->lock);
223 if (err != 0)
224 return err;
225 /* Test whether only readers are currently running, and whether the runcount
226 field will not overflow, and whether no writer is waiting. The latter
227 condition is because POSIX recommends that "write locks shall take
228 precedence over read locks", to avoid "writer starvation". */
229 if (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
230 {
231 /* This thread would have to wait for a while. Return instead. */
232 pthread_mutex_unlock (&lock->lock);
233 return EBUSY;
234 }
235 lock->runcount++;
236 return pthread_mutex_unlock (&lock->lock);
237 }
238
239 int
240 pthread_rwlock_trywrlock (pthread_rwlock_t *lock)
241 {
242 int err;
243
244 err = pthread_mutex_lock (&lock->lock);
245 if (err != 0)
246 return err;
247 /* Test whether no readers or writers are currently running. */
248 if (!(lock->runcount == 0))
249 {
250 /* This thread would have to wait for a while. Return instead. */
251 pthread_mutex_unlock (&lock->lock);
252 return EBUSY;
253 }
254 lock->runcount--; /* runcount becomes -1 */
255 return pthread_mutex_unlock (&lock->lock);
256 }
257
258 int
259 pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
260 const struct timespec *abstime)
261 {
262 int err;
263
264 err = pthread_mutex_lock (&lock->lock);
265 if (err != 0)
266 return err;
267 /* Test whether only readers are currently running, and whether the runcount
268 field will not overflow, and whether no writer is waiting. The latter
269 condition is because POSIX recommends that "write locks shall take
270 precedence over read locks", to avoid "writer starvation". */
271 while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
272 {
273 /* This thread has to wait for a while. Enqueue it among the
274 waiting_readers. */
275 err = pthread_cond_timedwait (&lock->waiting_readers, &lock->lock,
276 abstime);
277 if (err != 0)
278 {
279 pthread_mutex_unlock (&lock->lock);
280 return err;
281 }
282 }
283 lock->runcount++;
284 return pthread_mutex_unlock (&lock->lock);
285 }
286
287 int
288 pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
289 const struct timespec *abstime)
290 {
291 int err;
292
293 err = pthread_mutex_lock (&lock->lock);
294 if (err != 0)
295 return err;
296 /* Test whether no readers or writers are currently running. */
297 while (!(lock->runcount == 0))
298 {
299 /* This thread has to wait for a while. Enqueue it among the
300 waiting_writers. */
301 lock->waiting_writers_count++;
302 err = pthread_cond_timedwait (&lock->waiting_writers, &lock->lock,
303 abstime);
304 if (err != 0)
305 {
306 lock->waiting_writers_count--;
307 pthread_mutex_unlock (&lock->lock);
308 return err;
309 }
310 lock->waiting_writers_count--;
311 }
312 lock->runcount--; /* runcount becomes -1 */
313 return pthread_mutex_unlock (&lock->lock);
314 }
315
316 int
317 pthread_rwlock_unlock (pthread_rwlock_t *lock)
318 {
319 int err;
320
321 err = pthread_mutex_lock (&lock->lock);
322 if (err != 0)
323 return err;
324 if (lock->runcount < 0)
325 {
326 /* Drop a writer lock. */
327 if (!(lock->runcount == -1))
328 {
329 pthread_mutex_unlock (&lock->lock);
330 return EINVAL;
331 }
332 lock->runcount = 0;
333 }
334 else
335 {
336 /* Drop a reader lock. */
337 if (!(lock->runcount > 0))
338 {
339 pthread_mutex_unlock (&lock->lock);
340 return EINVAL;
341 }
342 lock->runcount--;
343 }
344 if (lock->runcount == 0)
345 {
346 /* POSIX recommends that "write locks shall take precedence over read
347 locks", to avoid "writer starvation". */
348 if (lock->waiting_writers_count > 0)
349 {
350 /* Wake up one of the waiting writers. */
351 err = pthread_cond_signal (&lock->waiting_writers);
352 if (err != 0)
353 {
354 pthread_mutex_unlock (&lock->lock);
355 return err;
356 }
357 }
358 else
359 {
360 /* Wake up all waiting readers. */
361 err = pthread_cond_broadcast (&lock->waiting_readers);
362 if (err != 0)
363 {
364 pthread_mutex_unlock (&lock->lock);
365 return err;
366 }
367 }
368 }
369 return pthread_mutex_unlock (&lock->lock);
370 }
371
372 int
373 pthread_rwlock_destroy (pthread_rwlock_t *lock)
374 {
375 int err;
376
377 err = pthread_mutex_destroy (&lock->lock);
378 if (err != 0)
379 return err;
380 err = pthread_cond_destroy (&lock->waiting_readers);
381 if (err != 0)
382 return err;
383 err = pthread_cond_destroy (&lock->waiting_writers);
384 if (err != 0)
385 return err;
386 return 0;
387 }
388
389 # else
390
391 # if PTHREAD_RWLOCK_BAD_WAITQUEUE
392
393 /* Override pthread_rwlock_init, to use the kind PREFER_WRITER_NONRECURSIVE
394 (or possibly PREFER_WRITER) instead of the default, when no
395 pthread_rwlockattr_t object is specified. */
396 int
397 pthread_rwlock_init (pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr)
398 # undef pthread_rwlock_init
399 {
400 int err;
401
402 if (attr != NULL)
403 err = pthread_rwlock_init (lock, attr);
404 else
405 {
406 pthread_rwlockattr_t replacement_attr;
407
408 err = pthread_rwlockattr_init (&replacement_attr);
409 if (err != 0)
410 return err;
411 err = pthread_rwlockattr_setkind_np (&replacement_attr,
412 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
413 if (err != 0)
414 {
415 pthread_rwlockattr_destroy (&replacement_attr);
416 return err;
417 }
418 err = pthread_rwlock_init (lock, &replacement_attr);
419 pthread_rwlockattr_destroy (&replacement_attr);
420 }
421 return err;
422 }
423
424 # endif
425
426 # if PTHREAD_RWLOCK_LACKS_TIMEOUT
427
428 int
429 pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
430 const struct timespec *abstime)
431 {
432 /* Poll the lock's state in regular intervals. Ugh. */
433 for (;;)
434 {
435 int err;
436 struct timeval currtime;
437 unsigned long remaining;
438
439 err = pthread_rwlock_tryrdlock (lock);
440 if (err != EBUSY)
441 return err;
442
443 gettimeofday (&currtime, NULL);
444
445 if (currtime.tv_sec > abstime->tv_sec)
446 remaining = 0;
447 else
448 {
449 unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
450 remaining = seconds * 1000000000;
451 if (remaining / 1000000000 != seconds) /* overflow? */
452 remaining = ULONG_MAX;
453 else
454 {
455 long nanoseconds =
456 abstime->tv_nsec - currtime.tv_usec * 1000;
457 if (nanoseconds >= 0)
458 {
459 remaining += nanoseconds;
460 if (remaining < nanoseconds) /* overflow? */
461 remaining = ULONG_MAX;
462 }
463 else
464 {
465 if (remaining >= - nanoseconds)
466 remaining -= (- nanoseconds);
467 else
468 remaining = 0;
469 }
470 }
471 }
472 if (remaining == 0)
473 return ETIMEDOUT;
474
475 /* Sleep 1 ms. */
476 struct timespec duration =
477 {
478 .tv_sec = 0,
479 .tv_nsec = MIN (1000000, remaining)
480 };
481 nanosleep (&duration, NULL);
482 }
483 }
484
485 int
486 pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
487 const struct timespec *abstime)
488 {
489 /* Poll the lock's state in regular intervals. Ugh. */
490 for (;;)
491 {
492 int err;
493 struct timeval currtime;
494 unsigned long remaining;
495
496 err = pthread_rwlock_trywrlock (lock);
497 if (err != EBUSY)
498 return err;
499
500 gettimeofday (&currtime, NULL);
501
502 if (currtime.tv_sec > abstime->tv_sec)
503 remaining = 0;
504 else
505 {
506 unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
507 remaining = seconds * 1000000000;
508 if (remaining / 1000000000 != seconds) /* overflow? */
509 remaining = ULONG_MAX;
510 else
511 {
512 long nanoseconds =
513 abstime->tv_nsec - currtime.tv_usec * 1000;
514 if (nanoseconds >= 0)
515 {
516 remaining += nanoseconds;
517 if (remaining < nanoseconds) /* overflow? */
518 remaining = ULONG_MAX;
519 }
520 else
521 {
522 if (remaining >= - nanoseconds)
523 remaining -= (- nanoseconds);
524 else
525 remaining = 0;
526 }
527 }
528 }
529 if (remaining == 0)
530 return ETIMEDOUT;
531
532 /* Sleep 1 ms. */
533 struct timespec duration =
534 {
535 .tv_sec = 0,
536 .tv_nsec = MIN (1000000, remaining)
537 };
538 nanosleep (&duration, NULL);
539 }
540 }
541
542 # endif
543
544 # endif
545
546 #else
547 /* Provide a dummy implementation for single-threaded applications. */
548
549 /* The pthread_rwlock_t is an 'int', representing the number of readers running,
550 or -1 when a writer runs. */
551
552 int
553 pthread_rwlock_init (pthread_rwlock_t *lock,
554 _GL_UNUSED const pthread_rwlockattr_t *attr)
555 {
556 *lock = 0;
557 return 0;
558 }
559
560 int
561 pthread_rwlock_rdlock (pthread_rwlock_t *lock)
562 {
563 if (*lock < 0)
564 return EDEADLK;
565 (*lock)++;
566 return 0;
567 }
568
569 int
570 pthread_rwlock_wrlock (pthread_rwlock_t *lock)
571 {
572 if (*lock != 0)
573 return EDEADLK;
574 *lock = -1;
575 return 0;
576 }
577
578 int
579 pthread_rwlock_tryrdlock (pthread_rwlock_t *lock)
580 {
581 return pthread_rwlock_rdlock (lock);
582 }
583
584 int
585 pthread_rwlock_trywrlock (pthread_rwlock_t *lock)
586 {
587 return pthread_rwlock_wrlock (lock);
588 }
589
590 int
591 pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
592 _GL_UNUSED const struct timespec *abstime)
593 {
594 return pthread_rwlock_rdlock (lock);
595 }
596
597 int
598 pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
599 _GL_UNUSED const struct timespec *abstime)
600 {
601 return pthread_rwlock_wrlock (lock);
602 }
603
604 int
605 pthread_rwlock_unlock (pthread_rwlock_t *lock)
606 {
607 if (*lock == 0)
608 return EPERM;
609 if (*lock < 0)
610 *lock = 0;
611 else /* *lock > 0 */
612 (*lock)--;
613 return 0;
614 }
615
616 int
617 pthread_rwlock_destroy (pthread_rwlock_t *lock)
618 {
619 if (*lock)
620 return EBUSY;
621 return 0;
622 }
623
624 #endif