ObjFW
OFTLSKey.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "objfw-defs.h"
21 
22 #include <errno.h>
23 
24 #include "platform.h"
25 
26 #if !defined(OF_HAVE_THREADS) || \
27  (!defined(OF_HAVE_PTHREADS) && !defined(OF_WINDOWS) && !defined(OF_AMIGAOS))
28 # error No thread-local storage available!
29 #endif
30 
31 #import "macros.h"
32 
35 #if defined(OF_HAVE_PTHREADS)
36 # include <pthread.h>
37 typedef pthread_key_t OFTLSKey;
38 #elif defined(OF_WINDOWS)
39 # include <windows.h>
40 typedef DWORD OFTLSKey;
41 #elif defined(OF_MORPHOS)
42 # include <proto/exec.h>
43 typedef ULONG OFTLSKey;
44 #elif defined(OF_AMIGAOS)
45 typedef struct _OFTLSKey {
46  struct objc_hashtable *table;
47  struct _OFTLSKey *next, *previous;
48 } *OFTLSKey;
49 #endif
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
60 extern int OFTLSKeyNew(OFTLSKey *key);
61 
68 extern int OFTLSKeyFree(OFTLSKey key);
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 /* TLS keys are inlined for performance. */
74 
75 #if defined(OF_HAVE_PTHREADS) || defined(DOXYGEN)
76 
82 static OF_INLINE void *
83 OFTLSKeyGet(OFTLSKey key)
84 {
85  return pthread_getspecific(key);
86 }
87 
94 static OF_INLINE int
95 OFTLSKeySet(OFTLSKey key, void *value)
96 {
97  return pthread_setspecific(key, value);
98 }
99 #elif defined(OF_WINDOWS)
100 static OF_INLINE void *
101 OFTLSKeyGet(OFTLSKey key)
102 {
103  return TlsGetValue(key);
104 }
105 
106 static OF_INLINE int
107 OFTLSKeySet(OFTLSKey key, void *value)
108 {
109  return (TlsSetValue(key, value) ? 0 : EINVAL);
110 }
111 #elif defined(OF_MORPHOS)
112 static OF_INLINE void *
113 OFTLSKeyGet(OFTLSKey key)
114 {
115  return (void *)TLSGetValue(key);
116 }
117 
118 static OF_INLINE int
119 OFTLSKeySet(OFTLSKey key, void *value)
120 {
121  return (TLSSetValue(key, (APTR)value) ? 0 : EINVAL);
122 }
123 #elif defined(OF_AMIGAOS)
124 /* Those are too big too inline. */
125 # ifdef __cplusplus
126 extern "C" {
127 # endif
128 extern void *OFTLSKeyGet(OFTLSKey key);
129 extern int OFTLSKeySet(OFTLSKey key, void *value);
130 # ifdef __cplusplus
131 }
132 # endif
133 #endif
int OFTLSKeyNew(OFTLSKey *key)
Creates a new Thread Local Storage key.
int OFTLSKeyFree(OFTLSKey key)
Destroys the specified Thread Local Storage key.
static OF_INLINE int OFTLSKeySet(OFTLSKey key, void *value)
Sets the current value for the specified Thread Local Storage key.
Definition: OFTLSKey.h:95
static OF_INLINE void * OFTLSKeyGet(OFTLSKey key)
Returns the current value for the specified Thread Local Storage key.
Definition: OFTLSKey.h:83