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 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #if USE_POSIX_THREADS_WEAK
23 # pragma weak pthread_key_create
24 # pragma weak pthread_getspecific
25 # pragma weak pthread_setspecific
31 #if USE_POSIX_THREADS_WEAK
36 #if USE_POSIX_THREADS_WEAK
42 #define tls_key_valid (tls_key.valid != 0)
44 static void tls_key_init(void)
47 tls_key.valid = !pthread_key_create(&tls_key.u.mt, free);
52 static void *tls_get(void)
54 #if USE_POSIX_THREADS_WEAK
55 if (tls_key.valid < 0)
58 return pthread_getspecific(tls_key.u.mt);
61 static int tls_set(void *p)
63 #if USE_POSIX_THREADS_WEAK
64 if (tls_key.valid < 0)
65 return (tls_key.u.st = p, 1);
67 return !pthread_setspecific(tls_key.u.mt, p);