ObjFW
Loading...
Searching...
No Matches
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>
37typedef pthread_key_t OFTLSKey;
38#elif defined(OF_WINDOWS)
39# include <windows.h>
40typedef DWORD OFTLSKey;
41#elif defined(OF_MORPHOS)
42# include <proto/exec.h>
43typedef ULONG OFTLSKey;
44#elif defined(OF_AMIGAOS)
45typedef struct _OFTLSKey {
46 struct objc_hashtable *table;
47 struct _OFTLSKey *next, *previous;
48} *OFTLSKey;
49#endif
50
51#ifdef __cplusplus
52extern "C" {
53#endif
60extern int OFTLSKeyNew(OFTLSKey *key);
61
68extern 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)
82static OF_INLINE void *
83OFTLSKeyGet(OFTLSKey key)
84{
85 return pthread_getspecific(key);
86}
87
94static OF_INLINE int
95OFTLSKeySet(OFTLSKey key, void *value)
96{
97 return pthread_setspecific(key, value);
98}
99#elif defined(OF_WINDOWS)
100static OF_INLINE void *
101OFTLSKeyGet(OFTLSKey key)
102{
103 return TlsGetValue(key);
104}
105
106static OF_INLINE int
107OFTLSKeySet(OFTLSKey key, void *value)
108{
109 return (TlsSetValue(key, value) ? 0 : EINVAL);
110}
111#elif defined(OF_MORPHOS)
112static OF_INLINE void *
113OFTLSKeyGet(OFTLSKey key)
114{
115 return (void *)TLSGetValue(key);
116}
117
118static OF_INLINE int
119OFTLSKeySet(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
126extern "C" {
127# endif
128extern void *OFTLSKeyGet(OFTLSKey key);
129extern 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.
static OF_INLINE int OFTLSKeySet(OFTLSKey key, void *value)
Sets the current value for the specified Thread Local Storage key.
Definition OFTLSKey.h:95
int OFTLSKeyFree(OFTLSKey key)
Destroys the specified Thread Local Storage key.
static OF_INLINE void * OFTLSKeyGet(OFTLSKey key)
Returns the current value for the specified Thread Local Storage key.
Definition OFTLSKey.h:83