gettext: Sync with gettext 0.23.
[gnulib.git] / lib / tss.c
blobf58fd8dfcc23ef3689b094db8c9bd5c3985db3e3
1 /* ISO C 11 thread-specific storage in multithreaded situations.
2 Copyright (C) 2005-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005, 2019. */
19 #include <config.h>
21 #include <threads.h>
23 #include <string.h>
25 #if defined _WIN32 && ! defined __CYGWIN__
26 /* Use Windows threads. */
28 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
29 # include <windows.h>
31 #else
32 /* Use POSIX threads. */
34 # include <pthread.h>
36 #endif
38 #if defined _WIN32 && ! defined __CYGWIN__
39 /* Use Windows threads. */
41 int
42 tss_create (tss_t *keyp, tss_dtor_t destructor)
44 int err = glwthread_tls_key_create (keyp, destructor);
45 if (err == 0)
46 return thrd_success;
47 else
49 memset (keyp, '\0', sizeof (tss_t));
50 return thrd_error;
54 int
55 tss_set (tss_t key, void *value)
57 int err = glwthread_tls_set (key, value);
58 return (err == 0 ? thrd_success : thrd_error);
61 void *
62 tss_get (tss_t key)
64 return glwthread_tls_get (key);
67 void
68 tss_delete (tss_t key)
70 glwthread_tls_key_delete (key);
73 #else
74 /* Use POSIX threads. */
76 int
77 tss_create (tss_t *keyp, tss_dtor_t destructor)
79 int err = pthread_key_create (keyp, destructor);
80 if (err == 0)
81 return thrd_success;
82 else
84 memset (keyp, '\0', sizeof (tss_t));
85 return thrd_error;
89 int
90 tss_set (tss_t key, void *value)
92 int err = pthread_setspecific (key, value);
93 return (err == 0 ? thrd_success : thrd_error);
96 void *
97 tss_get (tss_t key)
99 return pthread_getspecific (key);
102 void
103 tss_delete (tss_t key)
105 pthread_key_delete (key);
108 #endif