2 * Copyright © 2024 Nick Bowler
4 * Helpers for hosts using POSIX threading (pthreads) API.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * The pthread_in_use detection is based on Gnulib code written by Bruno
12 * Haible, distributed with the following copyright and permission notice:
14 * Copyright (C) 2005-2022 Free Software Foundation, Inc.
16 * This file is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as
18 * published by the Free Software Foundation; either version 2.1 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 extern int glthread_in_use();
34 #if !defined c11_threads_in_use
35 # if HAVE_THREADS_H && USE_POSIX_THREADS_FROM_LIBC
36 # define c11_threads_in_use 1
37 # elif HAVE_THREADS_H && USE_POSIX_THREADS_WEAK
39 # pragma weak thrd_exit
40 # define c11_threads_in_use (thrd_exit != NULL)
42 # define c11_threads_in_use 0
46 #if PTHREAD_IN_USE_DETECTION_HARD
47 # define pthread_in_use glthread_in_use()
48 #elif USE_POSIX_THREADS_WEAK
49 # pragma weak pthread_mutexattr_gettype
50 # define pthread_in_use (pthread_mutexattr_gettype || c11_threads_in_use)
52 # define pthread_in_use 1
55 #if USE_POSIX_THREADS_WEAK
56 # pragma weak pthread_key_create
57 # pragma weak pthread_getspecific
58 # pragma weak pthread_setspecific
59 # pragma weak pthread_once
65 #if USE_POSIX_THREADS_WEAK
70 #if USE_POSIX_THREADS_WEAK
76 #define tls_key_valid (tls_key.valid != 0)
78 static void init_once_cb(void);
80 static void *tls_get(void)
82 #if USE_POSIX_THREADS_WEAK
83 if (tls_key.valid < 0)
86 return pthread_getspecific(tls_key.u.mt);
89 static int tls_set(void *p)
91 #if USE_POSIX_THREADS_WEAK
92 if (tls_key.valid < 0)
93 return (tls_key.u.st = p, 1);
95 return !pthread_setspecific(tls_key.u.mt, p);
98 static void init_once_with_tls(void)
100 tls_key.valid = !pthread_key_create(&tls_key.u.mt, free);
104 static int init_once(void)
106 if (pthread_in_use) {
107 static pthread_once_t ctrl = PTHREAD_ONCE_INIT;
108 return !pthread_once(&ctrl, init_once_with_tls);
109 } else if (!tls_key_valid) {