Debian Codegraph @

lib/glthread/lock.c

raw
1 /* Locking in multithreaded situations.
2 Copyright (C) 2005-2026 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/PR13701 */
259
260 int
261 glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock)
262 {
263 pthread_rwlockattr_t attributes;
264 int err = pthread_rwlockattr_init (&attributes);
265 if (err != 0)
266 return err;
267 /* Note: PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP is the only value that
268 causes the writer to be preferred. PTHREAD_RWLOCK_PREFER_WRITER_NP does not
269 do this; see
270 https://man7.org/linux/man-pages/man3/pthread_rwlockattr_setkind_np.3.html */
271 err = pthread_rwlockattr_setkind_np (&attributes,
272 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
273 if (err == 0)
274 err = pthread_rwlock_init(lock, &attributes);
275 /* pthread_rwlockattr_destroy always returns 0. It cannot influence the
276 return value. */
277 pthread_rwlockattr_destroy (&attributes);
278 return err;
279 }
280
281 # endif
282 # else
283
284 int
285 glthread_rwlock_init_multithreaded (gl_rwlock_t *lock)
286 {
287 int err = pthread_rwlock_init (&lock->rwlock, NULL);
288 if (err != 0)
289 return err;
290 lock->initialized = 1;
291 return 0;
292 }
293
294 int
295 glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock)
296 {
297 if (!lock->initialized)
298 {
299 int err = pthread_mutex_lock (&lock->guard);
300 if (err != 0)
301 return err;
302 if (!lock->initialized)
303 {
304 err = glthread_rwlock_init_multithreaded (lock);
305 if (err != 0)
306 {
307 pthread_mutex_unlock (&lock->guard);
308 return err;
309 }
310 }
311 err = pthread_mutex_unlock (&lock->guard);
312 if (err != 0)
313 return err;
314 }
315 return pthread_rwlock_rdlock (&lock->rwlock);
316 }
317
318 int
319 glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock)
320 {
321 if (!lock->initialized)
322 {
323 int err = pthread_mutex_lock (&lock->guard);
324 if (err != 0)
325 return err;
326 if (!lock->initialized)
327 {
328 err = glthread_rwlock_init_multithreaded (lock);
329 if (err != 0)
330 {
331 pthread_mutex_unlock (&lock->guard);
332 return err;
333 }
334 }
335 err = pthread_mutex_unlock (&lock->guard);
336 if (err != 0)
337 return err;
338 }
339 return pthread_rwlock_wrlock (&lock->rwlock);
340 }
341
342 int
343 glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock)
344 {
345 if (!lock->initialized)
346 return EINVAL;
347 return pthread_rwlock_unlock (&lock->rwlock);
348 }
349
350 int
351 glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock)
352 {
353 if (!lock->initialized)
354 return EINVAL;
355 int err = pthread_rwlock_destroy (&lock->rwlock);
356 if (err != 0)
357 return err;
358 lock->initialized = 0;
359 return 0;
360 }
361
362 # endif
363
364 # else
365
366 int
367 glthread_rwlock_init_multithreaded (gl_rwlock_t *lock)
368 {
369 int err = pthread_mutex_init (&lock->lock, NULL);
370 if (err != 0)
371 return err;
372 err = pthread_cond_init (&lock->waiting_readers, NULL);
373 if (err != 0)
374 return err;
375 err = pthread_cond_init (&lock->waiting_writers, NULL);
376 if (err != 0)
377 return err;
378 lock->waiting_writers_count = 0;
379 lock->runcount = 0;
380 return 0;
381 }
382
383 int
384 glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock)
385 {
386 int err = pthread_mutex_lock (&lock->lock);
387 if (err != 0)
388 return err;
389 /* Test whether only readers are currently running, and whether the runcount
390 field will not overflow, and whether no writer is waiting. The latter
391 condition is because POSIX recommends that "write locks shall take
392 precedence over read locks", to avoid "writer starvation". */
393 while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
394 {
395 /* This thread has to wait for a while. Enqueue it among the
396 waiting_readers. */
397 err = pthread_cond_wait (&lock->waiting_readers, &lock->lock);
398 if (err != 0)
399 {
400 pthread_mutex_unlock (&lock->lock);
401 return err;
402 }
403 }
404 lock->runcount++;
405 return pthread_mutex_unlock (&lock->lock);
406 }
407
408 int
409 glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock)
410 {
411 int err = pthread_mutex_lock (&lock->lock);
412 if (err != 0)
413 return err;
414 /* Test whether no readers or writers are currently running. */
415 while (!(lock->runcount == 0))
416 {
417 /* This thread has to wait for a while. Enqueue it among the
418 waiting_writers. */
419 lock->waiting_writers_count++;
420 err = pthread_cond_wait (&lock->waiting_writers, &lock->lock);
421 if (err != 0)
422 {
423 lock->waiting_writers_count--;
424 pthread_mutex_unlock (&lock->lock);
425 return err;
426 }
427 lock->waiting_writers_count--;
428 }
429 lock->runcount--; /* runcount becomes -1 */
430 return pthread_mutex_unlock (&lock->lock);
431 }
432
433 int
434 glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock)
435 {
436 int err = pthread_mutex_lock (&lock->lock);
437 if (err != 0)
438 return err;
439 if (lock->runcount < 0)
440 {
441 /* Drop a writer lock. */
442 if (!(lock->runcount == -1))
443 {
444 pthread_mutex_unlock (&lock->lock);
445 return EINVAL;
446 }
447 lock->runcount = 0;
448 }
449 else
450 {
451 /* Drop a reader lock. */
452 if (!(lock->runcount > 0))
453 {
454 pthread_mutex_unlock (&lock->lock);
455 return EINVAL;
456 }
457 lock->runcount--;
458 }
459 if (lock->runcount == 0)
460 {
461 /* POSIX recommends that "write locks shall take precedence over read
462 locks", to avoid "writer starvation". */
463 if (lock->waiting_writers_count > 0)
464 {
465 /* Wake up one of the waiting writers. */
466 err = pthread_cond_signal (&lock->waiting_writers);
467 if (err != 0)
468 {
469 pthread_mutex_unlock (&lock->lock);
470 return err;
471 }
472 }
473 else
474 {
475 /* Wake up all waiting readers. */
476 err = pthread_cond_broadcast (&lock->waiting_readers);
477 if (err != 0)
478 {
479 pthread_mutex_unlock (&lock->lock);
480 return err;
481 }
482 }
483 }
484 return pthread_mutex_unlock (&lock->lock);
485 }
486
487 int
488 glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock)
489 {
490 int err = pthread_mutex_destroy (&lock->lock);
491 if (err != 0)
492 return err;
493 err = pthread_cond_destroy (&lock->waiting_readers);
494 if (err != 0)
495 return err;
496 err = pthread_cond_destroy (&lock->waiting_writers);
497 if (err != 0)
498 return err;
499 return 0;
500 }
501
502 # endif
503
504 /* --------------------- gl_recursive_lock_t datatype --------------------- */
505
506 # if HAVE_PTHREAD_MUTEX_RECURSIVE
507
508 # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
509
510 int
511 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
512 {
513 pthread_mutexattr_t attributes;
514 int err = pthread_mutexattr_init (&attributes);
515 if (err != 0)
516 return err;
517 err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE);
518 if (err != 0)
519 {
520 pthread_mutexattr_destroy (&attributes);
521 return err;
522 }
523 err = pthread_mutex_init (lock, &attributes);
524 if (err != 0)
525 {
526 pthread_mutexattr_destroy (&attributes);
527 return err;
528 }
529 err = pthread_mutexattr_destroy (&attributes);
530 if (err != 0)
531 return err;
532 return 0;
533 }
534
535 # else
536
537 int
538 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
539 {
540 pthread_mutexattr_t attributes;
541 int err = pthread_mutexattr_init (&attributes);
542 if (err != 0)
543 return err;
544 err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE);
545 if (err != 0)
546 {
547 pthread_mutexattr_destroy (&attributes);
548 return err;
549 }
550 err = pthread_mutex_init (&lock->recmutex, &attributes);
551 if (err != 0)
552 {
553 pthread_mutexattr_destroy (&attributes);
554 return err;
555 }
556 err = pthread_mutexattr_destroy (&attributes);
557 if (err != 0)
558 return err;
559 lock->initialized = 1;
560 return 0;
561 }
562
563 int
564 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
565 {
566 if (!lock->initialized)
567 {
568 int err = pthread_mutex_lock (&lock->guard);
569 if (err != 0)
570 return err;
571 if (!lock->initialized)
572 {
573 err = glthread_recursive_lock_init_multithreaded (lock);
574 if (err != 0)
575 {
576 pthread_mutex_unlock (&lock->guard);
577 return err;
578 }
579 }
580 err = pthread_mutex_unlock (&lock->guard);
581 if (err != 0)
582 return err;
583 }
584 return pthread_mutex_lock (&lock->recmutex);
585 }
586
587 int
588 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
589 {
590 if (!lock->initialized)
591 return EINVAL;
592 return pthread_mutex_unlock (&lock->recmutex);
593 }
594
595 int
596 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
597 {
598 if (!lock->initialized)
599 return EINVAL;
600 int err = pthread_mutex_destroy (&lock->recmutex);
601 if (err != 0)
602 return err;
603 lock->initialized = 0;
604 return 0;
605 }
606
607 # endif
608
609 # else
610
611 int
612 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
613 {
614 int err = pthread_mutex_init (&lock->mutex, NULL);
615 if (err != 0)
616 return err;
617 lock->owner = (pthread_t) 0;
618 lock->depth = 0;
619 return 0;
620 }
621
622 int
623 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
624 {
625 pthread_t self = pthread_self ();
626 if (lock->owner != self)
627 {
628 int err = pthread_mutex_lock (&lock->mutex);
629 if (err != 0)
630 return err;
631 lock->owner = self;
632 }
633 if (++(lock->depth) == 0) /* wraparound? */
634 {
635 lock->depth--;
636 return EAGAIN;
637 }
638 return 0;
639 }
640
641 int
642 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
643 {
644 if (lock->owner != pthread_self ())
645 return EPERM;
646 if (lock->depth == 0)
647 return EINVAL;
648 if (--(lock->depth) == 0)
649 {
650 lock->owner = (pthread_t) 0;
651 return pthread_mutex_unlock (&lock->mutex);
652 }
653 else
654 return 0;
655 }
656
657 int
658 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
659 {
660 if (lock->owner != (pthread_t) 0)
661 return EBUSY;
662 return pthread_mutex_destroy (&lock->mutex);
663 }
664
665 # endif
666
667 #endif
668
669 /* ========================================================================= */
670
671 #if USE_WINDOWS_THREADS
672
673 #endif
674
675 /* ========================================================================= */