Debian Codegraph @

lib/glthread/lock.c

raw
1 /* Locking in multithreaded situations.
2 Copyright (C) 2005-2024 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]>, 2005.
18 Based on GCC's gthr-posix.h, gthr-posix95.h. */
19
20 #include <config.h>
21
22 #include "glthread/lock.h"
23
24 /* ========================================================================= */
25
26 #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
27
28 /* -------------------------- gl_lock_t datatype -------------------------- */
29
30 int
31 glthread_lock_init (gl_lock_t *lock)
32 {
33 if (mtx_init (&lock->mutex, mtx_plain) != thrd_success)
34 return ENOMEM;
35 lock->init_needed = 0;
36 return 0;
37 }
38
39 int
40 glthread_lock_lock (gl_lock_t *lock)
41 {
42 if (lock->init_needed)
43 call_once (&lock->init_once, lock->init_func);
44 if (mtx_lock (&lock->mutex) != thrd_success)
45 return EAGAIN;
46 return 0;
47 }
48
49 int
50 glthread_lock_unlock (gl_lock_t *lock)
51 {
52 if (lock->init_needed)
53 call_once (&lock->init_once, lock->init_func);
54 if (mtx_unlock (&lock->mutex) != thrd_success)
55 return EINVAL;
56 return 0;
57 }
58
59 int
60 glthread_lock_destroy (gl_lock_t *lock)
61 {
62 if (lock->init_needed)
63 call_once (&lock->init_once, lock->init_func);
64 mtx_destroy (&lock->mutex);
65 return 0;
66 }
67
68 /* ------------------------- gl_rwlock_t datatype ------------------------- */
69
70 int
71 glthread_rwlock_init (gl_rwlock_t *lock)
72 {
73 if (mtx_init (&lock->lock, mtx_plain) != thrd_success
74 || cnd_init (&lock->waiting_readers) != thrd_success
75 || cnd_init (&lock->waiting_writers) != thrd_success)
76 return ENOMEM;
77 lock->waiting_writers_count = 0;
78 lock->runcount = 0;
79 lock->init_needed = 0;
80 return 0;
81 }
82
83 int
84 glthread_rwlock_rdlock (gl_rwlock_t *lock)
85 {
86 if (lock->init_needed)
87 call_once (&lock->init_once, lock->init_func);
88 if (mtx_lock (&lock->lock) != thrd_success)
89 return EAGAIN;
90 /* Test whether only readers are currently running, and whether the runcount
91 field will not overflow, and whether no writer is waiting. The latter
92 condition is because POSIX recommends that "write locks shall take
93 precedence over read locks", to avoid "writer starvation". */
94 while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
95 {
96 /* This thread has to wait for a while. Enqueue it among the
97 waiting_readers. */
98 if (cnd_wait (&lock->waiting_readers, &lock->lock) != thrd_success)
99 {
100 mtx_unlock (&lock->lock);
101 return EINVAL;
102 }
103 }
104 lock->runcount++;
105 if (mtx_unlock (&lock->lock) != thrd_success)
106 return EINVAL;
107 return 0;
108 }
109
110 int
111 glthread_rwlock_wrlock (gl_rwlock_t *lock)
112 {
113 if (lock->init_needed)
114 call_once (&lock->init_once, lock->init_func);
115 if (mtx_lock (&lock->lock) != thrd_success)
116 return EAGAIN;
117 /* Test whether no readers or writers are currently running. */
118 while (!(lock->runcount == 0))
119 {
120 /* This thread has to wait for a while. Enqueue it among the
121 waiting_writers. */
122 lock->waiting_writers_count++;
123 if (cnd_wait (&lock->waiting_writers, &lock->lock) != thrd_success)
124 {
125 lock->waiting_writers_count--;
126 mtx_unlock (&lock->lock);
127 return EINVAL;
128 }
129 lock->waiting_writers_count--;
130 }
131 lock->runcount--; /* runcount becomes -1 */
132 if (mtx_unlock (&lock->lock) != thrd_success)
133 return EINVAL;
134 return 0;
135 }
136
137 int
138 glthread_rwlock_unlock (gl_rwlock_t *lock)
139 {
140 if (lock->init_needed)
141 call_once (&lock->init_once, lock->init_func);
142 if (mtx_lock (&lock->lock) != thrd_success)
143 return EAGAIN;
144 if (lock->runcount < 0)
145 {
146 /* Drop a writer lock. */
147 if (!(lock->runcount == -1))
148 {
149 mtx_unlock (&lock->lock);
150 return EINVAL;
151 }
152 lock->runcount = 0;
153 }
154 else
155 {
156 /* Drop a reader lock. */
157 if (!(lock->runcount > 0))
158 {
159 mtx_unlock (&lock->lock);
160 return EINVAL;
161 }
162 lock->runcount--;
163 }
164 if (lock->runcount == 0)
165 {
166 /* POSIX recommends that "write locks shall take precedence over read
167 locks", to avoid "writer starvation". */
168 if (lock->waiting_writers_count > 0)
169 {
170 /* Wake up one of the waiting writers. */
171 if (cnd_signal (&lock->waiting_writers) != thrd_success)
172 {
173 mtx_unlock (&lock->lock);
174 return EINVAL;
175 }
176 }
177 else
178 {
179 /* Wake up all waiting readers. */
180 if (cnd_broadcast (&lock->waiting_readers) != thrd_success)
181 {
182 mtx_unlock (&lock->lock);
183 return EINVAL;
184 }
185 }
186 }
187 if (mtx_unlock (&lock->lock) != thrd_success)
188 return EINVAL;
189 return 0;
190 }
191
192 int
193 glthread_rwlock_destroy (gl_rwlock_t *lock)
194 {
195 if (lock->init_needed)
196 call_once (&lock->init_once, lock->init_func);
197 mtx_destroy (&lock->lock);
198 cnd_destroy (&lock->waiting_readers);
199 cnd_destroy (&lock->waiting_writers);
200 return 0;
201 }
202
203 /* --------------------- gl_recursive_lock_t datatype --------------------- */
204
205 int
206 glthread_recursive_lock_init (gl_recursive_lock_t *lock)
207 {
208 if (mtx_init (&lock->mutex, mtx_plain | mtx_recursive) != thrd_success)
209 return ENOMEM;
210 lock->init_needed = 0;
211 return 0;
212 }
213
214 int
215 glthread_recursive_lock_lock (gl_recursive_lock_t *lock)
216 {
217 if (lock->init_needed)
218 call_once (&lock->init_once, lock->init_func);
219 if (mtx_lock (&lock->mutex) != thrd_success)
220 return EAGAIN;
221 return 0;
222 }
223
224 int
225 glthread_recursive_lock_unlock (gl_recursive_lock_t *lock)
226 {
227 if (lock->init_needed)
228 call_once (&lock->init_once, lock->init_func);
229 if (mtx_unlock (&lock->mutex) != thrd_success)
230 return EINVAL;
231 return 0;
232 }
233
234 int
235 glthread_recursive_lock_destroy (gl_recursive_lock_t *lock)
236 {
237 if (lock->init_needed)
238 call_once (&lock->init_once, lock->init_func);
239 mtx_destroy (&lock->mutex);
240 return 0;
241 }
242
243 #endif
244
245 /* ========================================================================= */
246
247 #if USE_POSIX_THREADS
248
249 /* -------------------------- gl_lock_t datatype -------------------------- */
250
251 /* ------------------------- gl_rwlock_t datatype ------------------------- */
252
253 # if HAVE_PTHREAD_RWLOCK && (HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER || (defined PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP && (__GNU_LIBRARY__ > 1)))
254
255 # if defined PTHREAD_RWLOCK_INITIALIZER || defined PTHREAD_RWLOCK_INITIALIZER_NP
256
257 # if !HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER
258 /* glibc with bug https://sourceware.org/bugzilla/show_bug.cgi?id=13701 */
259
260 int
261 glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock)
262 {
263 pthread_rwlockattr_t attributes;
264 int err;
265
266 err = pthread_rwlockattr_init (&attributes);
267 if (err != 0)
268 return err;
269 /* Note: PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP is the only value that
270 causes the writer to be preferred. PTHREAD_RWLOCK_PREFER_WRITER_NP does not
271 do this; see
272 http://man7.org/linux/man-pages/man3/pthread_rwlockattr_setkind_np.3.html */
273 err = pthread_rwlockattr_setkind_np (&attributes,
274 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
275 if (err == 0)
276 err = pthread_rwlock_init(lock, &attributes);
277 /* pthread_rwlockattr_destroy always returns 0. It cannot influence the
278 return value. */
279 pthread_rwlockattr_destroy (&attributes);
280 return err;
281 }
282
283 # endif
284 # else
285
286 int
287 glthread_rwlock_init_multithreaded (gl_rwlock_t *lock)
288 {
289 int err;
290
291 err = pthread_rwlock_init (&lock->rwlock, NULL);
292 if (err != 0)
293 return err;
294 lock->initialized = 1;
295 return 0;
296 }
297
298 int
299 glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock)
300 {
301 if (!lock->initialized)
302 {
303 int err;
304
305 err = pthread_mutex_lock (&lock->guard);
306 if (err != 0)
307 return err;
308 if (!lock->initialized)
309 {
310 err = glthread_rwlock_init_multithreaded (lock);
311 if (err != 0)
312 {
313 pthread_mutex_unlock (&lock->guard);
314 return err;
315 }
316 }
317 err = pthread_mutex_unlock (&lock->guard);
318 if (err != 0)
319 return err;
320 }
321 return pthread_rwlock_rdlock (&lock->rwlock);
322 }
323
324 int
325 glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock)
326 {
327 if (!lock->initialized)
328 {
329 int err;
330
331 err = pthread_mutex_lock (&lock->guard);
332 if (err != 0)
333 return err;
334 if (!lock->initialized)
335 {
336 err = glthread_rwlock_init_multithreaded (lock);
337 if (err != 0)
338 {
339 pthread_mutex_unlock (&lock->guard);
340 return err;
341 }
342 }
343 err = pthread_mutex_unlock (&lock->guard);
344 if (err != 0)
345 return err;
346 }
347 return pthread_rwlock_wrlock (&lock->rwlock);
348 }
349
350 int
351 glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock)
352 {
353 if (!lock->initialized)
354 return EINVAL;
355 return pthread_rwlock_unlock (&lock->rwlock);
356 }
357
358 int
359 glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock)
360 {
361 int err;
362
363 if (!lock->initialized)
364 return EINVAL;
365 err = pthread_rwlock_destroy (&lock->rwlock);
366 if (err != 0)
367 return err;
368 lock->initialized = 0;
369 return 0;
370 }
371
372 # endif
373
374 # else
375
376 int
377 glthread_rwlock_init_multithreaded (gl_rwlock_t *lock)
378 {
379 int err;
380
381 err = pthread_mutex_init (&lock->lock, NULL);
382 if (err != 0)
383 return err;
384 err = pthread_cond_init (&lock->waiting_readers, NULL);
385 if (err != 0)
386 return err;
387 err = pthread_cond_init (&lock->waiting_writers, NULL);
388 if (err != 0)
389 return err;
390 lock->waiting_writers_count = 0;
391 lock->runcount = 0;
392 return 0;
393 }
394
395 int
396 glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock)
397 {
398 int err;
399
400 err = pthread_mutex_lock (&lock->lock);
401 if (err != 0)
402 return err;
403 /* Test whether only readers are currently running, and whether the runcount
404 field will not overflow, and whether no writer is waiting. The latter
405 condition is because POSIX recommends that "write locks shall take
406 precedence over read locks", to avoid "writer starvation". */
407 while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
408 {
409 /* This thread has to wait for a while. Enqueue it among the
410 waiting_readers. */
411 err = pthread_cond_wait (&lock->waiting_readers, &lock->lock);
412 if (err != 0)
413 {
414 pthread_mutex_unlock (&lock->lock);
415 return err;
416 }
417 }
418 lock->runcount++;
419 return pthread_mutex_unlock (&lock->lock);
420 }
421
422 int
423 glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock)
424 {
425 int err;
426
427 err = pthread_mutex_lock (&lock->lock);
428 if (err != 0)
429 return err;
430 /* Test whether no readers or writers are currently running. */
431 while (!(lock->runcount == 0))
432 {
433 /* This thread has to wait for a while. Enqueue it among the
434 waiting_writers. */
435 lock->waiting_writers_count++;
436 err = pthread_cond_wait (&lock->waiting_writers, &lock->lock);
437 if (err != 0)
438 {
439 lock->waiting_writers_count--;
440 pthread_mutex_unlock (&lock->lock);
441 return err;
442 }
443 lock->waiting_writers_count--;
444 }
445 lock->runcount--; /* runcount becomes -1 */
446 return pthread_mutex_unlock (&lock->lock);
447 }
448
449 int
450 glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock)
451 {
452 int err;
453
454 err = pthread_mutex_lock (&lock->lock);
455 if (err != 0)
456 return err;
457 if (lock->runcount < 0)
458 {
459 /* Drop a writer lock. */
460 if (!(lock->runcount == -1))
461 {
462 pthread_mutex_unlock (&lock->lock);
463 return EINVAL;
464 }
465 lock->runcount = 0;
466 }
467 else
468 {
469 /* Drop a reader lock. */
470 if (!(lock->runcount > 0))
471 {
472 pthread_mutex_unlock (&lock->lock);
473 return EINVAL;
474 }
475 lock->runcount--;
476 }
477 if (lock->runcount == 0)
478 {
479 /* POSIX recommends that "write locks shall take precedence over read
480 locks", to avoid "writer starvation". */
481 if (lock->waiting_writers_count > 0)
482 {
483 /* Wake up one of the waiting writers. */
484 err = pthread_cond_signal (&lock->waiting_writers);
485 if (err != 0)
486 {
487 pthread_mutex_unlock (&lock->lock);
488 return err;
489 }
490 }
491 else
492 {
493 /* Wake up all waiting readers. */
494 err = pthread_cond_broadcast (&lock->waiting_readers);
495 if (err != 0)
496 {
497 pthread_mutex_unlock (&lock->lock);
498 return err;
499 }
500 }
501 }
502 return pthread_mutex_unlock (&lock->lock);
503 }
504
505 int
506 glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock)
507 {
508 int err;
509
510 err = pthread_mutex_destroy (&lock->lock);
511 if (err != 0)
512 return err;
513 err = pthread_cond_destroy (&lock->waiting_readers);
514 if (err != 0)
515 return err;
516 err = pthread_cond_destroy (&lock->waiting_writers);
517 if (err != 0)
518 return err;
519 return 0;
520 }
521
522 # endif
523
524 /* --------------------- gl_recursive_lock_t datatype --------------------- */
525
526 # if HAVE_PTHREAD_MUTEX_RECURSIVE
527
528 # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
529
530 int
531 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
532 {
533 pthread_mutexattr_t attributes;
534 int err;
535
536 err = pthread_mutexattr_init (&attributes);
537 if (err != 0)
538 return err;
539 err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE);
540 if (err != 0)
541 {
542 pthread_mutexattr_destroy (&attributes);
543 return err;
544 }
545 err = pthread_mutex_init (lock, &attributes);
546 if (err != 0)
547 {
548 pthread_mutexattr_destroy (&attributes);
549 return err;
550 }
551 err = pthread_mutexattr_destroy (&attributes);
552 if (err != 0)
553 return err;
554 return 0;
555 }
556
557 # else
558
559 int
560 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
561 {
562 pthread_mutexattr_t attributes;
563 int err;
564
565 err = pthread_mutexattr_init (&attributes);
566 if (err != 0)
567 return err;
568 err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE);
569 if (err != 0)
570 {
571 pthread_mutexattr_destroy (&attributes);
572 return err;
573 }
574 err = pthread_mutex_init (&lock->recmutex, &attributes);
575 if (err != 0)
576 {
577 pthread_mutexattr_destroy (&attributes);
578 return err;
579 }
580 err = pthread_mutexattr_destroy (&attributes);
581 if (err != 0)
582 return err;
583 lock->initialized = 1;
584 return 0;
585 }
586
587 int
588 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
589 {
590 if (!lock->initialized)
591 {
592 int err;
593
594 err = pthread_mutex_lock (&lock->guard);
595 if (err != 0)
596 return err;
597 if (!lock->initialized)
598 {
599 err = glthread_recursive_lock_init_multithreaded (lock);
600 if (err != 0)
601 {
602 pthread_mutex_unlock (&lock->guard);
603 return err;
604 }
605 }
606 err = pthread_mutex_unlock (&lock->guard);
607 if (err != 0)
608 return err;
609 }
610 return pthread_mutex_lock (&lock->recmutex);
611 }
612
613 int
614 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
615 {
616 if (!lock->initialized)
617 return EINVAL;
618 return pthread_mutex_unlock (&lock->recmutex);
619 }
620
621 int
622 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
623 {
624 int err;
625
626 if (!lock->initialized)
627 return EINVAL;
628 err = pthread_mutex_destroy (&lock->recmutex);
629 if (err != 0)
630 return err;
631 lock->initialized = 0;
632 return 0;
633 }
634
635 # endif
636
637 # else
638
639 int
640 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
641 {
642 int err;
643
644 err = pthread_mutex_init (&lock->mutex, NULL);
645 if (err != 0)
646 return err;
647 lock->owner = (pthread_t) 0;
648 lock->depth = 0;
649 return 0;
650 }
651
652 int
653 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
654 {
655 pthread_t self = pthread_self ();
656 if (lock->owner != self)
657 {
658 int err;
659
660 err = pthread_mutex_lock (&lock->mutex);
661 if (err != 0)
662 return err;
663 lock->owner = self;
664 }
665 if (++(lock->depth) == 0) /* wraparound? */
666 {
667 lock->depth--;
668 return EAGAIN;
669 }
670 return 0;
671 }
672
673 int
674 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
675 {
676 if (lock->owner != pthread_self ())
677 return EPERM;
678 if (lock->depth == 0)
679 return EINVAL;
680 if (--(lock->depth) == 0)
681 {
682 lock->owner = (pthread_t) 0;
683 return pthread_mutex_unlock (&lock->mutex);
684 }
685 else
686 return 0;
687 }
688
689 int
690 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
691 {
692 if (lock->owner != (pthread_t) 0)
693 return EBUSY;
694 return pthread_mutex_destroy (&lock->mutex);
695 }
696
697 # endif
698
699 #endif
700
701 /* ========================================================================= */
702
703 #if USE_WINDOWS_THREADS
704
705 #endif
706
707 /* ========================================================================= */