diff options
Diffstat (limited to 'gl/glthread')
| -rw-r--r-- | gl/glthread/lock.c | 112 | ||||
| -rw-r--r-- | gl/glthread/lock.h | 155 | ||||
| -rw-r--r-- | gl/glthread/once.c | 80 | ||||
| -rw-r--r-- | gl/glthread/once.h | 272 | ||||
| -rw-r--r-- | gl/glthread/threadlib.c | 2 |
5 files changed, 378 insertions, 243 deletions
diff --git a/gl/glthread/lock.c b/gl/glthread/lock.c index 6661ad6a..cfb9393c 100644 --- a/gl/glthread/lock.c +++ b/gl/glthread/lock.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* Locking in multithreaded situations. | 1 | /* Locking in multithreaded situations. |
| 2 | Copyright (C) 2005-2024 Free Software Foundation, Inc. | 2 | Copyright (C) 2005-2026 Free Software Foundation, Inc. |
| 3 | 3 | ||
| 4 | This file is free software: you can redistribute it and/or modify | 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 | 5 | it under the terms of the GNU Lesser General Public License as |
| @@ -240,8 +240,6 @@ glthread_recursive_lock_destroy (gl_recursive_lock_t *lock) | |||
| 240 | return 0; | 240 | return 0; |
| 241 | } | 241 | } |
| 242 | 242 | ||
| 243 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 244 | |||
| 245 | #endif | 243 | #endif |
| 246 | 244 | ||
| 247 | /* ========================================================================= */ | 245 | /* ========================================================================= */ |
| @@ -257,21 +255,19 @@ glthread_recursive_lock_destroy (gl_recursive_lock_t *lock) | |||
| 257 | # if defined PTHREAD_RWLOCK_INITIALIZER || defined PTHREAD_RWLOCK_INITIALIZER_NP | 255 | # if defined PTHREAD_RWLOCK_INITIALIZER || defined PTHREAD_RWLOCK_INITIALIZER_NP |
| 258 | 256 | ||
| 259 | # if !HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER | 257 | # if !HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER |
| 260 | /* glibc with bug https://sourceware.org/bugzilla/show_bug.cgi?id=13701 */ | 258 | /* glibc with bug https://sourceware.org/PR13701 */ |
| 261 | 259 | ||
| 262 | int | 260 | int |
| 263 | glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock) | 261 | glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock) |
| 264 | { | 262 | { |
| 265 | pthread_rwlockattr_t attributes; | 263 | pthread_rwlockattr_t attributes; |
| 266 | int err; | 264 | int err = pthread_rwlockattr_init (&attributes); |
| 267 | |||
| 268 | err = pthread_rwlockattr_init (&attributes); | ||
| 269 | if (err != 0) | 265 | if (err != 0) |
| 270 | return err; | 266 | return err; |
| 271 | /* Note: PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP is the only value that | 267 | /* Note: PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP is the only value that |
| 272 | causes the writer to be preferred. PTHREAD_RWLOCK_PREFER_WRITER_NP does not | 268 | causes the writer to be preferred. PTHREAD_RWLOCK_PREFER_WRITER_NP does not |
| 273 | do this; see | 269 | do this; see |
| 274 | http://man7.org/linux/man-pages/man3/pthread_rwlockattr_setkind_np.3.html */ | 270 | https://man7.org/linux/man-pages/man3/pthread_rwlockattr_setkind_np.3.html */ |
| 275 | err = pthread_rwlockattr_setkind_np (&attributes, | 271 | err = pthread_rwlockattr_setkind_np (&attributes, |
| 276 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); | 272 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 277 | if (err == 0) | 273 | if (err == 0) |
| @@ -288,9 +284,7 @@ glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock) | |||
| 288 | int | 284 | int |
| 289 | glthread_rwlock_init_multithreaded (gl_rwlock_t *lock) | 285 | glthread_rwlock_init_multithreaded (gl_rwlock_t *lock) |
| 290 | { | 286 | { |
| 291 | int err; | 287 | int err = pthread_rwlock_init (&lock->rwlock, NULL); |
| 292 | |||
| 293 | err = pthread_rwlock_init (&lock->rwlock, NULL); | ||
| 294 | if (err != 0) | 288 | if (err != 0) |
| 295 | return err; | 289 | return err; |
| 296 | lock->initialized = 1; | 290 | lock->initialized = 1; |
| @@ -302,9 +296,7 @@ glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock) | |||
| 302 | { | 296 | { |
| 303 | if (!lock->initialized) | 297 | if (!lock->initialized) |
| 304 | { | 298 | { |
| 305 | int err; | 299 | int err = pthread_mutex_lock (&lock->guard); |
| 306 | |||
| 307 | err = pthread_mutex_lock (&lock->guard); | ||
| 308 | if (err != 0) | 300 | if (err != 0) |
| 309 | return err; | 301 | return err; |
| 310 | if (!lock->initialized) | 302 | if (!lock->initialized) |
| @@ -328,9 +320,7 @@ glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock) | |||
| 328 | { | 320 | { |
| 329 | if (!lock->initialized) | 321 | if (!lock->initialized) |
| 330 | { | 322 | { |
| 331 | int err; | 323 | int err = pthread_mutex_lock (&lock->guard); |
| 332 | |||
| 333 | err = pthread_mutex_lock (&lock->guard); | ||
| 334 | if (err != 0) | 324 | if (err != 0) |
| 335 | return err; | 325 | return err; |
| 336 | if (!lock->initialized) | 326 | if (!lock->initialized) |
| @@ -360,11 +350,9 @@ glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock) | |||
| 360 | int | 350 | int |
| 361 | glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock) | 351 | glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock) |
| 362 | { | 352 | { |
| 363 | int err; | ||
| 364 | |||
| 365 | if (!lock->initialized) | 353 | if (!lock->initialized) |
| 366 | return EINVAL; | 354 | return EINVAL; |
| 367 | err = pthread_rwlock_destroy (&lock->rwlock); | 355 | int err = pthread_rwlock_destroy (&lock->rwlock); |
| 368 | if (err != 0) | 356 | if (err != 0) |
| 369 | return err; | 357 | return err; |
| 370 | lock->initialized = 0; | 358 | lock->initialized = 0; |
| @@ -378,9 +366,7 @@ glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock) | |||
| 378 | int | 366 | int |
| 379 | glthread_rwlock_init_multithreaded (gl_rwlock_t *lock) | 367 | glthread_rwlock_init_multithreaded (gl_rwlock_t *lock) |
| 380 | { | 368 | { |
| 381 | int err; | 369 | int err = pthread_mutex_init (&lock->lock, NULL); |
| 382 | |||
| 383 | err = pthread_mutex_init (&lock->lock, NULL); | ||
| 384 | if (err != 0) | 370 | if (err != 0) |
| 385 | return err; | 371 | return err; |
| 386 | err = pthread_cond_init (&lock->waiting_readers, NULL); | 372 | err = pthread_cond_init (&lock->waiting_readers, NULL); |
| @@ -397,9 +383,7 @@ glthread_rwlock_init_multithreaded (gl_rwlock_t *lock) | |||
| 397 | int | 383 | int |
| 398 | glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock) | 384 | glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock) |
| 399 | { | 385 | { |
| 400 | int err; | 386 | int err = pthread_mutex_lock (&lock->lock); |
| 401 | |||
| 402 | err = pthread_mutex_lock (&lock->lock); | ||
| 403 | if (err != 0) | 387 | if (err != 0) |
| 404 | return err; | 388 | return err; |
| 405 | /* Test whether only readers are currently running, and whether the runcount | 389 | /* Test whether only readers are currently running, and whether the runcount |
| @@ -424,9 +408,7 @@ glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock) | |||
| 424 | int | 408 | int |
| 425 | glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock) | 409 | glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock) |
| 426 | { | 410 | { |
| 427 | int err; | 411 | int err = pthread_mutex_lock (&lock->lock); |
| 428 | |||
| 429 | err = pthread_mutex_lock (&lock->lock); | ||
| 430 | if (err != 0) | 412 | if (err != 0) |
| 431 | return err; | 413 | return err; |
| 432 | /* Test whether no readers or writers are currently running. */ | 414 | /* Test whether no readers or writers are currently running. */ |
| @@ -451,9 +433,7 @@ glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock) | |||
| 451 | int | 433 | int |
| 452 | glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock) | 434 | glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock) |
| 453 | { | 435 | { |
| 454 | int err; | 436 | int err = pthread_mutex_lock (&lock->lock); |
| 455 | |||
| 456 | err = pthread_mutex_lock (&lock->lock); | ||
| 457 | if (err != 0) | 437 | if (err != 0) |
| 458 | return err; | 438 | return err; |
| 459 | if (lock->runcount < 0) | 439 | if (lock->runcount < 0) |
| @@ -507,9 +487,7 @@ glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock) | |||
| 507 | int | 487 | int |
| 508 | glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock) | 488 | glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock) |
| 509 | { | 489 | { |
| 510 | int err; | 490 | int err = pthread_mutex_destroy (&lock->lock); |
| 511 | |||
| 512 | err = pthread_mutex_destroy (&lock->lock); | ||
| 513 | if (err != 0) | 491 | if (err != 0) |
| 514 | return err; | 492 | return err; |
| 515 | err = pthread_cond_destroy (&lock->waiting_readers); | 493 | err = pthread_cond_destroy (&lock->waiting_readers); |
| @@ -533,9 +511,7 @@ int | |||
| 533 | glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock) | 511 | glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock) |
| 534 | { | 512 | { |
| 535 | pthread_mutexattr_t attributes; | 513 | pthread_mutexattr_t attributes; |
| 536 | int err; | 514 | int err = pthread_mutexattr_init (&attributes); |
| 537 | |||
| 538 | err = pthread_mutexattr_init (&attributes); | ||
| 539 | if (err != 0) | 515 | if (err != 0) |
| 540 | return err; | 516 | return err; |
| 541 | err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE); | 517 | err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE); |
| @@ -562,9 +538,7 @@ int | |||
| 562 | glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock) | 538 | glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock) |
| 563 | { | 539 | { |
| 564 | pthread_mutexattr_t attributes; | 540 | pthread_mutexattr_t attributes; |
| 565 | int err; | 541 | int err = pthread_mutexattr_init (&attributes); |
| 566 | |||
| 567 | err = pthread_mutexattr_init (&attributes); | ||
| 568 | if (err != 0) | 542 | if (err != 0) |
| 569 | return err; | 543 | return err; |
| 570 | err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE); | 544 | err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE); |
| @@ -591,9 +565,7 @@ glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock) | |||
| 591 | { | 565 | { |
| 592 | if (!lock->initialized) | 566 | if (!lock->initialized) |
| 593 | { | 567 | { |
| 594 | int err; | 568 | int err = pthread_mutex_lock (&lock->guard); |
| 595 | |||
| 596 | err = pthread_mutex_lock (&lock->guard); | ||
| 597 | if (err != 0) | 569 | if (err != 0) |
| 598 | return err; | 570 | return err; |
| 599 | if (!lock->initialized) | 571 | if (!lock->initialized) |
| @@ -623,11 +595,9 @@ glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock) | |||
| 623 | int | 595 | int |
| 624 | glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock) | 596 | glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock) |
| 625 | { | 597 | { |
| 626 | int err; | ||
| 627 | |||
| 628 | if (!lock->initialized) | 598 | if (!lock->initialized) |
| 629 | return EINVAL; | 599 | return EINVAL; |
| 630 | err = pthread_mutex_destroy (&lock->recmutex); | 600 | int err = pthread_mutex_destroy (&lock->recmutex); |
| 631 | if (err != 0) | 601 | if (err != 0) |
| 632 | return err; | 602 | return err; |
| 633 | lock->initialized = 0; | 603 | lock->initialized = 0; |
| @@ -641,9 +611,7 @@ glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock) | |||
| 641 | int | 611 | int |
| 642 | glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock) | 612 | glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock) |
| 643 | { | 613 | { |
| 644 | int err; | 614 | int err = pthread_mutex_init (&lock->mutex, NULL); |
| 645 | |||
| 646 | err = pthread_mutex_init (&lock->mutex, NULL); | ||
| 647 | if (err != 0) | 615 | if (err != 0) |
| 648 | return err; | 616 | return err; |
| 649 | lock->owner = (pthread_t) 0; | 617 | lock->owner = (pthread_t) 0; |
| @@ -657,9 +625,7 @@ glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock) | |||
| 657 | pthread_t self = pthread_self (); | 625 | pthread_t self = pthread_self (); |
| 658 | if (lock->owner != self) | 626 | if (lock->owner != self) |
| 659 | { | 627 | { |
| 660 | int err; | 628 | int err = pthread_mutex_lock (&lock->mutex); |
| 661 | |||
| 662 | err = pthread_mutex_lock (&lock->mutex); | ||
| 663 | if (err != 0) | 629 | if (err != 0) |
| 664 | return err; | 630 | return err; |
| 665 | lock->owner = self; | 631 | lock->owner = self; |
| @@ -698,46 +664,6 @@ glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock) | |||
| 698 | 664 | ||
| 699 | # endif | 665 | # endif |
| 700 | 666 | ||
| 701 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 702 | |||
| 703 | static const pthread_once_t fresh_once = PTHREAD_ONCE_INIT; | ||
| 704 | |||
| 705 | int | ||
| 706 | glthread_once_singlethreaded (pthread_once_t *once_control) | ||
| 707 | { | ||
| 708 | /* We don't know whether pthread_once_t is an integer type, a floating-point | ||
| 709 | type, a pointer type, or a structure type. */ | ||
| 710 | char *firstbyte = (char *)once_control; | ||
| 711 | if (*firstbyte == *(const char *)&fresh_once) | ||
| 712 | { | ||
| 713 | /* First time use of once_control. Invert the first byte. */ | ||
| 714 | *firstbyte = ~ *(const char *)&fresh_once; | ||
| 715 | return 1; | ||
| 716 | } | ||
| 717 | else | ||
| 718 | return 0; | ||
| 719 | } | ||
| 720 | |||
| 721 | # if !(PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK) | ||
| 722 | |||
| 723 | int | ||
| 724 | glthread_once_multithreaded (pthread_once_t *once_control, | ||
| 725 | void (*init_function) (void)) | ||
| 726 | { | ||
| 727 | int err = pthread_once (once_control, init_function); | ||
| 728 | if (err == ENOSYS) | ||
| 729 | { | ||
| 730 | /* This happens on FreeBSD 11: The pthread_once function in libc returns | ||
| 731 | ENOSYS. */ | ||
| 732 | if (glthread_once_singlethreaded (once_control)) | ||
| 733 | init_function (); | ||
| 734 | return 0; | ||
| 735 | } | ||
| 736 | return err; | ||
| 737 | } | ||
| 738 | |||
| 739 | # endif | ||
| 740 | |||
| 741 | #endif | 667 | #endif |
| 742 | 668 | ||
| 743 | /* ========================================================================= */ | 669 | /* ========================================================================= */ |
diff --git a/gl/glthread/lock.h b/gl/glthread/lock.h index 2d5cb320..d8190b6c 100644 --- a/gl/glthread/lock.h +++ b/gl/glthread/lock.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* Locking in multithreaded situations. | 1 | /* Locking in multithreaded situations. |
| 2 | Copyright (C) 2005-2024 Free Software Foundation, Inc. | 2 | Copyright (C) 2005-2026 Free Software Foundation, Inc. |
| 3 | 3 | ||
| 4 | This file is free software: you can redistribute it and/or modify | 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 | 5 | it under the terms of the GNU Lesser General Public License as |
| @@ -64,13 +64,6 @@ | |||
| 64 | Taking the lock: err = glthread_recursive_lock_lock (&name); | 64 | Taking the lock: err = glthread_recursive_lock_lock (&name); |
| 65 | Releasing the lock: err = glthread_recursive_lock_unlock (&name); | 65 | Releasing the lock: err = glthread_recursive_lock_unlock (&name); |
| 66 | De-initialization: err = glthread_recursive_lock_destroy (&name); | 66 | De-initialization: err = glthread_recursive_lock_destroy (&name); |
| 67 | |||
| 68 | Once-only execution: | ||
| 69 | Type: gl_once_t | ||
| 70 | Initializer: gl_once_define(extern, name) | ||
| 71 | Execution: gl_once (name, initfunction); | ||
| 72 | Equivalent functions with control of error handling: | ||
| 73 | Execution: err = glthread_once (&name, initfunction); | ||
| 74 | */ | 67 | */ |
| 75 | 68 | ||
| 76 | 69 | ||
| @@ -88,17 +81,9 @@ | |||
| 88 | #include <errno.h> | 81 | #include <errno.h> |
| 89 | #include <stdlib.h> | 82 | #include <stdlib.h> |
| 90 | 83 | ||
| 91 | #if !defined c11_threads_in_use | 84 | #include "glthread/once.h" |
| 92 | # if HAVE_THREADS_H && USE_POSIX_THREADS_FROM_LIBC | 85 | |
| 93 | # define c11_threads_in_use() 1 | 86 | /* c11_threads_in_use() is defined in glthread/once.h. */ |
| 94 | # elif HAVE_THREADS_H && USE_POSIX_THREADS_WEAK | ||
| 95 | # include <threads.h> | ||
| 96 | # pragma weak thrd_exit | ||
| 97 | # define c11_threads_in_use() (thrd_exit != NULL) | ||
| 98 | # else | ||
| 99 | # define c11_threads_in_use() 0 | ||
| 100 | # endif | ||
| 101 | #endif | ||
| 102 | 87 | ||
| 103 | /* ========================================================================= */ | 88 | /* ========================================================================= */ |
| 104 | 89 | ||
| @@ -195,14 +180,6 @@ extern int glthread_recursive_lock_lock (gl_recursive_lock_t *lock); | |||
| 195 | extern int glthread_recursive_lock_unlock (gl_recursive_lock_t *lock); | 180 | extern int glthread_recursive_lock_unlock (gl_recursive_lock_t *lock); |
| 196 | extern int glthread_recursive_lock_destroy (gl_recursive_lock_t *lock); | 181 | extern int glthread_recursive_lock_destroy (gl_recursive_lock_t *lock); |
| 197 | 182 | ||
| 198 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 199 | |||
| 200 | typedef once_flag gl_once_t; | ||
| 201 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 202 | STORAGECLASS once_flag NAME = ONCE_FLAG_INIT; | ||
| 203 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 204 | (call_once (ONCE_CONTROL, INITFUNCTION), 0) | ||
| 205 | |||
| 206 | # ifdef __cplusplus | 183 | # ifdef __cplusplus |
| 207 | } | 184 | } |
| 208 | # endif | 185 | # endif |
| @@ -221,80 +198,7 @@ typedef once_flag gl_once_t; | |||
| 221 | extern "C" { | 198 | extern "C" { |
| 222 | # endif | 199 | # endif |
| 223 | 200 | ||
| 224 | # if PTHREAD_IN_USE_DETECTION_HARD | 201 | /* pthread_in_use() is defined in glthread/once.h. */ |
| 225 | |||
| 226 | /* The pthread_in_use() detection needs to be done at runtime. */ | ||
| 227 | # define pthread_in_use() \ | ||
| 228 | glthread_in_use () | ||
| 229 | extern int glthread_in_use (void); | ||
| 230 | |||
| 231 | # endif | ||
| 232 | |||
| 233 | # if USE_POSIX_THREADS_WEAK | ||
| 234 | |||
| 235 | /* Use weak references to the POSIX threads library. */ | ||
| 236 | |||
| 237 | /* Weak references avoid dragging in external libraries if the other parts | ||
| 238 | of the program don't use them. Here we use them, because we don't want | ||
| 239 | every program that uses libintl to depend on libpthread. This assumes | ||
| 240 | that libpthread would not be loaded after libintl; i.e. if libintl is | ||
| 241 | loaded first, by an executable that does not depend on libpthread, and | ||
| 242 | then a module is dynamically loaded that depends on libpthread, libintl | ||
| 243 | will not be multithread-safe. */ | ||
| 244 | |||
| 245 | /* The way to test at runtime whether libpthread is present is to test | ||
| 246 | whether a function pointer's value, such as &pthread_mutex_init, is | ||
| 247 | non-NULL. However, some versions of GCC have a bug through which, in | ||
| 248 | PIC mode, &foo != NULL always evaluates to true if there is a direct | ||
| 249 | call to foo(...) in the same function. To avoid this, we test the | ||
| 250 | address of a function in libpthread that we don't use. */ | ||
| 251 | |||
| 252 | # pragma weak pthread_mutex_init | ||
| 253 | # pragma weak pthread_mutex_lock | ||
| 254 | # pragma weak pthread_mutex_unlock | ||
| 255 | # pragma weak pthread_mutex_destroy | ||
| 256 | # pragma weak pthread_rwlock_init | ||
| 257 | # pragma weak pthread_rwlock_rdlock | ||
| 258 | # pragma weak pthread_rwlock_wrlock | ||
| 259 | # pragma weak pthread_rwlock_unlock | ||
| 260 | # pragma weak pthread_rwlock_destroy | ||
| 261 | # pragma weak pthread_once | ||
| 262 | # pragma weak pthread_cond_init | ||
| 263 | # pragma weak pthread_cond_wait | ||
| 264 | # pragma weak pthread_cond_signal | ||
| 265 | # pragma weak pthread_cond_broadcast | ||
| 266 | # pragma weak pthread_cond_destroy | ||
| 267 | # pragma weak pthread_mutexattr_init | ||
| 268 | # pragma weak pthread_mutexattr_settype | ||
| 269 | # pragma weak pthread_mutexattr_destroy | ||
| 270 | # pragma weak pthread_rwlockattr_init | ||
| 271 | # if __GNU_LIBRARY__ > 1 | ||
| 272 | # pragma weak pthread_rwlockattr_setkind_np | ||
| 273 | # endif | ||
| 274 | # pragma weak pthread_rwlockattr_destroy | ||
| 275 | # ifndef pthread_self | ||
| 276 | # pragma weak pthread_self | ||
| 277 | # endif | ||
| 278 | |||
| 279 | # if !PTHREAD_IN_USE_DETECTION_HARD | ||
| 280 | /* Considering all platforms with USE_POSIX_THREADS_WEAK, only few symbols | ||
| 281 | can be used to determine whether libpthread is in use. These are: | ||
| 282 | pthread_mutexattr_gettype | ||
| 283 | pthread_rwlockattr_destroy | ||
| 284 | pthread_rwlockattr_init | ||
| 285 | */ | ||
| 286 | # pragma weak pthread_mutexattr_gettype | ||
| 287 | # define pthread_in_use() \ | ||
| 288 | (pthread_mutexattr_gettype != NULL || c11_threads_in_use ()) | ||
| 289 | # endif | ||
| 290 | |||
| 291 | # else | ||
| 292 | |||
| 293 | # if !PTHREAD_IN_USE_DETECTION_HARD | ||
| 294 | # define pthread_in_use() 1 | ||
| 295 | # endif | ||
| 296 | |||
| 297 | # endif | ||
| 298 | 202 | ||
| 299 | /* -------------------------- gl_lock_t datatype -------------------------- */ | 203 | /* -------------------------- gl_lock_t datatype -------------------------- */ |
| 300 | 204 | ||
| @@ -335,7 +239,7 @@ typedef pthread_rwlock_t gl_rwlock_t; | |||
| 335 | # endif | 239 | # endif |
| 336 | # define glthread_rwlock_init(LOCK) \ | 240 | # define glthread_rwlock_init(LOCK) \ |
| 337 | (pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0) | 241 | (pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0) |
| 338 | # else /* glibc with bug https://sourceware.org/bugzilla/show_bug.cgi?id=13701 */ | 242 | # else /* glibc with bug https://sourceware.org/PR13701 */ |
| 339 | # define gl_rwlock_initializer \ | 243 | # define gl_rwlock_initializer \ |
| 340 | PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP | 244 | PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP |
| 341 | # define glthread_rwlock_init(LOCK) \ | 245 | # define glthread_rwlock_init(LOCK) \ |
| @@ -510,26 +414,6 @@ extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *l | |||
| 510 | 414 | ||
| 511 | # endif | 415 | # endif |
| 512 | 416 | ||
| 513 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 514 | |||
| 515 | typedef pthread_once_t gl_once_t; | ||
| 516 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 517 | STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT; | ||
| 518 | # if PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK | ||
| 519 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 520 | (pthread_in_use () \ | ||
| 521 | ? pthread_once (ONCE_CONTROL, INITFUNCTION) \ | ||
| 522 | : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0)) | ||
| 523 | # else | ||
| 524 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 525 | (pthread_in_use () \ | ||
| 526 | ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \ | ||
| 527 | : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0)) | ||
| 528 | extern int glthread_once_multithreaded (pthread_once_t *once_control, | ||
| 529 | void (*init_function) (void)); | ||
| 530 | # endif | ||
| 531 | extern int glthread_once_singlethreaded (pthread_once_t *once_control); | ||
| 532 | |||
| 533 | # ifdef __cplusplus | 417 | # ifdef __cplusplus |
| 534 | } | 418 | } |
| 535 | # endif | 419 | # endif |
| @@ -546,7 +430,6 @@ extern int glthread_once_singlethreaded (pthread_once_t *once_control); | |||
| 546 | # include "windows-mutex.h" | 430 | # include "windows-mutex.h" |
| 547 | # include "windows-rwlock.h" | 431 | # include "windows-rwlock.h" |
| 548 | # include "windows-recmutex.h" | 432 | # include "windows-recmutex.h" |
| 549 | # include "windows-once.h" | ||
| 550 | 433 | ||
| 551 | # ifdef __cplusplus | 434 | # ifdef __cplusplus |
| 552 | extern "C" { | 435 | extern "C" { |
| @@ -619,14 +502,6 @@ typedef glwthread_recmutex_t gl_recursive_lock_t; | |||
| 619 | # define glthread_recursive_lock_destroy(LOCK) \ | 502 | # define glthread_recursive_lock_destroy(LOCK) \ |
| 620 | glwthread_recmutex_destroy (LOCK) | 503 | glwthread_recmutex_destroy (LOCK) |
| 621 | 504 | ||
| 622 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 623 | |||
| 624 | typedef glwthread_once_t gl_once_t; | ||
| 625 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 626 | STORAGECLASS gl_once_t NAME = GLWTHREAD_ONCE_INIT; | ||
| 627 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 628 | (glwthread_once (ONCE_CONTROL, INITFUNCTION), 0) | ||
| 629 | |||
| 630 | # ifdef __cplusplus | 505 | # ifdef __cplusplus |
| 631 | } | 506 | } |
| 632 | # endif | 507 | # endif |
| @@ -670,14 +545,6 @@ typedef int gl_recursive_lock_t; | |||
| 670 | # define glthread_recursive_lock_unlock(NAME) 0 | 545 | # define glthread_recursive_lock_unlock(NAME) 0 |
| 671 | # define glthread_recursive_lock_destroy(NAME) 0 | 546 | # define glthread_recursive_lock_destroy(NAME) 0 |
| 672 | 547 | ||
| 673 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 674 | |||
| 675 | typedef int gl_once_t; | ||
| 676 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 677 | STORAGECLASS gl_once_t NAME = 0; | ||
| 678 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 679 | (*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0) | ||
| 680 | |||
| 681 | #endif | 548 | #endif |
| 682 | 549 | ||
| 683 | /* ========================================================================= */ | 550 | /* ========================================================================= */ |
| @@ -784,16 +651,6 @@ typedef int gl_once_t; | |||
| 784 | } \ | 651 | } \ |
| 785 | while (0) | 652 | while (0) |
| 786 | 653 | ||
| 787 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 788 | |||
| 789 | #define gl_once(NAME, INITFUNCTION) \ | ||
| 790 | do \ | ||
| 791 | { \ | ||
| 792 | if (glthread_once (&NAME, INITFUNCTION)) \ | ||
| 793 | abort (); \ | ||
| 794 | } \ | ||
| 795 | while (0) | ||
| 796 | |||
| 797 | /* ========================================================================= */ | 654 | /* ========================================================================= */ |
| 798 | 655 | ||
| 799 | #endif /* _LOCK_H */ | 656 | #endif /* _LOCK_H */ |
diff --git a/gl/glthread/once.c b/gl/glthread/once.c new file mode 100644 index 00000000..b72a4a20 --- /dev/null +++ b/gl/glthread/once.c | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | /* Once-only initialization 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 <bruno@clisp.org>, 2005. | ||
| 18 | Based on GCC's gthr-posix.h, gthr-posix95.h. */ | ||
| 19 | |||
| 20 | #include <config.h> | ||
| 21 | |||
| 22 | #include "glthread/once.h" | ||
| 23 | |||
| 24 | /* ========================================================================= */ | ||
| 25 | |||
| 26 | #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS | ||
| 27 | |||
| 28 | #endif | ||
| 29 | |||
| 30 | /* ========================================================================= */ | ||
| 31 | |||
| 32 | #if USE_POSIX_THREADS | ||
| 33 | |||
| 34 | static const pthread_once_t fresh_once = PTHREAD_ONCE_INIT; | ||
| 35 | |||
| 36 | int | ||
| 37 | glthread_once_singlethreaded (pthread_once_t *once_control) | ||
| 38 | { | ||
| 39 | /* We don't know whether pthread_once_t is an integer type, a floating-point | ||
| 40 | type, a pointer type, or a structure type. */ | ||
| 41 | char *firstbyte = (char *)once_control; | ||
| 42 | if (*firstbyte == *(const char *)&fresh_once) | ||
| 43 | { | ||
| 44 | /* First time use of once_control. Invert the first byte. */ | ||
| 45 | *firstbyte = ~ *(const char *)&fresh_once; | ||
| 46 | return 1; | ||
| 47 | } | ||
| 48 | else | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | # if !(PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK) | ||
| 53 | |||
| 54 | int | ||
| 55 | glthread_once_multithreaded (pthread_once_t *once_control, | ||
| 56 | void (*init_function) (void)) | ||
| 57 | { | ||
| 58 | int err = pthread_once (once_control, init_function); | ||
| 59 | if (err == ENOSYS) | ||
| 60 | { | ||
| 61 | /* This happens on FreeBSD 11: The pthread_once function in libc returns | ||
| 62 | ENOSYS. */ | ||
| 63 | if (glthread_once_singlethreaded (once_control)) | ||
| 64 | init_function (); | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | return err; | ||
| 68 | } | ||
| 69 | |||
| 70 | # endif | ||
| 71 | |||
| 72 | #endif | ||
| 73 | |||
| 74 | /* ========================================================================= */ | ||
| 75 | |||
| 76 | #if USE_WINDOWS_THREADS | ||
| 77 | |||
| 78 | #endif | ||
| 79 | |||
| 80 | /* ========================================================================= */ | ||
diff --git a/gl/glthread/once.h b/gl/glthread/once.h new file mode 100644 index 00000000..b6d9ab40 --- /dev/null +++ b/gl/glthread/once.h | |||
| @@ -0,0 +1,272 @@ | |||
| 1 | /* Once-only initialization 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 <bruno@clisp.org>, 2005. | ||
| 18 | Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-win32.h. */ | ||
| 19 | |||
| 20 | /* This file contains once-only initialization primitives for use with a given | ||
| 21 | thread library. | ||
| 22 | It does not contain primitives for creating threads or for other | ||
| 23 | synchronization primitives. | ||
| 24 | |||
| 25 | Once-only execution: | ||
| 26 | Type: gl_once_t | ||
| 27 | Initializer: gl_once_define(extern, name) | ||
| 28 | Execution: gl_once (name, initfunction); | ||
| 29 | Equivalent functions with control of error handling: | ||
| 30 | Execution: err = glthread_once (&name, initfunction); | ||
| 31 | */ | ||
| 32 | |||
| 33 | |||
| 34 | #ifndef _ONCE_H | ||
| 35 | #define _ONCE_H | ||
| 36 | |||
| 37 | /* This file uses HAVE_THREADS_H. */ | ||
| 38 | #if !_GL_CONFIG_H_INCLUDED | ||
| 39 | #error "Please include config.h first." | ||
| 40 | #endif | ||
| 41 | |||
| 42 | #include <errno.h> | ||
| 43 | #include <stdlib.h> | ||
| 44 | |||
| 45 | #if !defined c11_threads_in_use | ||
| 46 | # if HAVE_THREADS_H && USE_POSIX_THREADS_FROM_LIBC | ||
| 47 | # define c11_threads_in_use() 1 | ||
| 48 | # elif HAVE_THREADS_H && USE_POSIX_THREADS_WEAK | ||
| 49 | # include <threads.h> | ||
| 50 | # pragma weak thrd_exit | ||
| 51 | # define c11_threads_in_use() (thrd_exit != NULL) | ||
| 52 | # else | ||
| 53 | # define c11_threads_in_use() 0 | ||
| 54 | # endif | ||
| 55 | #endif | ||
| 56 | |||
| 57 | /* ========================================================================= */ | ||
| 58 | |||
| 59 | #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS | ||
| 60 | |||
| 61 | /* Use the ISO C threads library. */ | ||
| 62 | |||
| 63 | # include <threads.h> | ||
| 64 | |||
| 65 | # ifdef __cplusplus | ||
| 66 | extern "C" { | ||
| 67 | # endif | ||
| 68 | |||
| 69 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 70 | |||
| 71 | typedef once_flag gl_once_t; | ||
| 72 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 73 | STORAGECLASS once_flag NAME = ONCE_FLAG_INIT; | ||
| 74 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 75 | (call_once (ONCE_CONTROL, INITFUNCTION), 0) | ||
| 76 | |||
| 77 | # ifdef __cplusplus | ||
| 78 | } | ||
| 79 | # endif | ||
| 80 | |||
| 81 | #endif | ||
| 82 | |||
| 83 | /* ========================================================================= */ | ||
| 84 | |||
| 85 | #if USE_POSIX_THREADS | ||
| 86 | |||
| 87 | /* Use the POSIX threads library. */ | ||
| 88 | |||
| 89 | # include <pthread.h> | ||
| 90 | |||
| 91 | # ifdef __cplusplus | ||
| 92 | extern "C" { | ||
| 93 | # endif | ||
| 94 | |||
| 95 | # if PTHREAD_IN_USE_DETECTION_HARD | ||
| 96 | |||
| 97 | /* The pthread_in_use() detection needs to be done at runtime. */ | ||
| 98 | # define pthread_in_use() \ | ||
| 99 | glthread_in_use () | ||
| 100 | extern int glthread_in_use (void); | ||
| 101 | |||
| 102 | # endif | ||
| 103 | |||
| 104 | # if USE_POSIX_THREADS_WEAK | ||
| 105 | |||
| 106 | /* Use weak references to the POSIX threads library. */ | ||
| 107 | |||
| 108 | /* Weak references avoid dragging in external libraries if the other parts | ||
| 109 | of the program don't use them. Here we use them, because we don't want | ||
| 110 | every program that uses libintl to depend on libpthread. This assumes | ||
| 111 | that libpthread would not be loaded after libintl; i.e. if libintl is | ||
| 112 | loaded first, by an executable that does not depend on libpthread, and | ||
| 113 | then a module is dynamically loaded that depends on libpthread, libintl | ||
| 114 | will not be multithread-safe. */ | ||
| 115 | |||
| 116 | /* The way to test at runtime whether libpthread is present is to test | ||
| 117 | whether a function pointer's value, such as &pthread_mutex_init, is | ||
| 118 | non-NULL. However, some versions of GCC have a bug through which, in | ||
| 119 | PIC mode, &foo != NULL always evaluates to true if there is a direct | ||
| 120 | call to foo(...) in the same function. To avoid this, we test the | ||
| 121 | address of a function in libpthread that we don't use. */ | ||
| 122 | |||
| 123 | # pragma weak pthread_mutex_init | ||
| 124 | # pragma weak pthread_mutex_lock | ||
| 125 | # pragma weak pthread_mutex_unlock | ||
| 126 | # pragma weak pthread_mutex_destroy | ||
| 127 | /* Work around clang bug <https://github.com/llvm/llvm-project/issues/104670> */ | ||
| 128 | # ifndef pthread_rwlock_init | ||
| 129 | # pragma weak pthread_rwlock_init | ||
| 130 | # endif | ||
| 131 | # pragma weak pthread_rwlock_rdlock | ||
| 132 | # pragma weak pthread_rwlock_wrlock | ||
| 133 | # pragma weak pthread_rwlock_unlock | ||
| 134 | # pragma weak pthread_rwlock_destroy | ||
| 135 | # pragma weak pthread_once | ||
| 136 | # pragma weak pthread_cond_init | ||
| 137 | # pragma weak pthread_cond_wait | ||
| 138 | # pragma weak pthread_cond_signal | ||
| 139 | # pragma weak pthread_cond_broadcast | ||
| 140 | # pragma weak pthread_cond_destroy | ||
| 141 | # pragma weak pthread_mutexattr_init | ||
| 142 | # pragma weak pthread_mutexattr_settype | ||
| 143 | # pragma weak pthread_mutexattr_destroy | ||
| 144 | /* Work around clang bug <https://github.com/llvm/llvm-project/issues/104670> */ | ||
| 145 | # ifndef pthread_rwlockattr_init | ||
| 146 | # pragma weak pthread_rwlockattr_init | ||
| 147 | # endif | ||
| 148 | # if __GNU_LIBRARY__ > 1 | ||
| 149 | # pragma weak pthread_rwlockattr_setkind_np | ||
| 150 | # endif | ||
| 151 | # pragma weak pthread_rwlockattr_destroy | ||
| 152 | # ifndef pthread_self | ||
| 153 | # pragma weak pthread_self | ||
| 154 | # endif | ||
| 155 | |||
| 156 | # if !PTHREAD_IN_USE_DETECTION_HARD | ||
| 157 | /* Considering all platforms with USE_POSIX_THREADS_WEAK, only few symbols | ||
| 158 | can be used to determine whether libpthread is in use. These are: | ||
| 159 | pthread_mutexattr_gettype | ||
| 160 | pthread_rwlockattr_destroy | ||
| 161 | pthread_rwlockattr_init | ||
| 162 | */ | ||
| 163 | # pragma weak pthread_mutexattr_gettype | ||
| 164 | # define pthread_in_use() \ | ||
| 165 | (pthread_mutexattr_gettype != NULL || c11_threads_in_use ()) | ||
| 166 | # endif | ||
| 167 | |||
| 168 | # else | ||
| 169 | |||
| 170 | # if !PTHREAD_IN_USE_DETECTION_HARD | ||
| 171 | # define pthread_in_use() 1 | ||
| 172 | # endif | ||
| 173 | |||
| 174 | # endif | ||
| 175 | |||
| 176 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 177 | |||
| 178 | typedef pthread_once_t gl_once_t; | ||
| 179 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 180 | STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT; | ||
| 181 | # if PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK | ||
| 182 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 183 | (pthread_in_use () \ | ||
| 184 | ? pthread_once (ONCE_CONTROL, INITFUNCTION) \ | ||
| 185 | : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0)) | ||
| 186 | # else | ||
| 187 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 188 | (pthread_in_use () \ | ||
| 189 | ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \ | ||
| 190 | : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0)) | ||
| 191 | extern int glthread_once_multithreaded (pthread_once_t *once_control, | ||
| 192 | void (*init_function) (void)); | ||
| 193 | # endif | ||
| 194 | extern int glthread_once_singlethreaded (pthread_once_t *once_control); | ||
| 195 | |||
| 196 | # ifdef __cplusplus | ||
| 197 | } | ||
| 198 | # endif | ||
| 199 | |||
| 200 | #endif | ||
| 201 | |||
| 202 | /* ========================================================================= */ | ||
| 203 | |||
| 204 | #if USE_WINDOWS_THREADS | ||
| 205 | |||
| 206 | # define WIN32_LEAN_AND_MEAN /* avoid including junk */ | ||
| 207 | # include <windows.h> | ||
| 208 | |||
| 209 | # include "windows-once.h" | ||
| 210 | |||
| 211 | # ifdef __cplusplus | ||
| 212 | extern "C" { | ||
| 213 | # endif | ||
| 214 | |||
| 215 | /* We can use CRITICAL_SECTION directly, rather than the native Windows Event, | ||
| 216 | Mutex, Semaphore types, because | ||
| 217 | - we need only to synchronize inside a single process (address space), | ||
| 218 | not inter-process locking, | ||
| 219 | - we don't need to support trylock operations. (TryEnterCriticalSection | ||
| 220 | does not work on Windows 95/98/ME. Packages that need trylock usually | ||
| 221 | define their own mutex type.) */ | ||
| 222 | |||
| 223 | /* There is no way to statically initialize a CRITICAL_SECTION. It needs | ||
| 224 | to be done lazily, once only. For this we need spinlocks. */ | ||
| 225 | |||
| 226 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 227 | |||
| 228 | typedef glwthread_once_t gl_once_t; | ||
| 229 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 230 | STORAGECLASS gl_once_t NAME = GLWTHREAD_ONCE_INIT; | ||
| 231 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 232 | (glwthread_once (ONCE_CONTROL, INITFUNCTION), 0) | ||
| 233 | |||
| 234 | # ifdef __cplusplus | ||
| 235 | } | ||
| 236 | # endif | ||
| 237 | |||
| 238 | #endif | ||
| 239 | |||
| 240 | /* ========================================================================= */ | ||
| 241 | |||
| 242 | #if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS) | ||
| 243 | |||
| 244 | /* Provide dummy implementation if threads are not supported. */ | ||
| 245 | |||
| 246 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 247 | |||
| 248 | typedef int gl_once_t; | ||
| 249 | # define gl_once_define(STORAGECLASS, NAME) \ | ||
| 250 | STORAGECLASS gl_once_t NAME = 0; | ||
| 251 | # define glthread_once(ONCE_CONTROL, INITFUNCTION) \ | ||
| 252 | (*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0) | ||
| 253 | |||
| 254 | #endif | ||
| 255 | |||
| 256 | /* ========================================================================= */ | ||
| 257 | |||
| 258 | /* Macros with built-in error handling. */ | ||
| 259 | |||
| 260 | /* -------------------------- gl_once_t datatype -------------------------- */ | ||
| 261 | |||
| 262 | #define gl_once(NAME, INITFUNCTION) \ | ||
| 263 | do \ | ||
| 264 | { \ | ||
| 265 | if (glthread_once (&NAME, INITFUNCTION)) \ | ||
| 266 | abort (); \ | ||
| 267 | } \ | ||
| 268 | while (0) | ||
| 269 | |||
| 270 | /* ========================================================================= */ | ||
| 271 | |||
| 272 | #endif /* _ONCE_H */ | ||
diff --git a/gl/glthread/threadlib.c b/gl/glthread/threadlib.c index 7a776768..5c0ba1f4 100644 --- a/gl/glthread/threadlib.c +++ b/gl/glthread/threadlib.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* Multithreading primitives. | 1 | /* Multithreading primitives. |
| 2 | Copyright (C) 2005-2024 Free Software Foundation, Inc. | 2 | Copyright (C) 2005-2026 Free Software Foundation, Inc. |
| 3 | 3 | ||
| 4 | This file is free software: you can redistribute it and/or modify | 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 | 5 | it under the terms of the GNU Lesser General Public License as |
