threadwrap.h 10.6 KB
Newer Older
1
2
3
4
5
6
/* Thread-related cross-platform development wrappers */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8
 *																			*
9
10
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
11
12
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
13
14
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
15
16
17
18
19
20
21
22
23
24
25
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#ifndef _THREADWRAP_H
#define _THREADWRAP_H

#include "gen_defs.h"	/* HANDLE */
26
#include "wrapdll.h"	/* DLLEXPORT and */
27

28
#if !__STDC_NO_ATOMICS__
Deucе's avatar
Deucе committed
29
	#if defined __GNUC__ && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)) && !defined(__llvm__) && !defined(__INTEL_COMPILER)
30
		#define __STDC_NO_ATOMICS__ 1
31
	#elif defined __BORLANDC__ || defined _MSC_VER
32
33
34
35
		#define __STDC_NO_ATOMICS__ 1
	#endif
#endif
#if !__STDC_NO_ATOMICS__
36
37
38
39
#include <stdbool.h>
#ifdef __cplusplus
#include <atomic>
#else
Deucе's avatar
Deucе committed
40
41
#include <stdatomic.h>
#endif
42
#endif
Deucе's avatar
Deucе committed
43

44
#if defined(__cplusplus)
45
46
47
extern "C" {
#endif

48
#if defined(__unix__)
49

50
	#include <sys/param.h>
51
	#include <pthread.h>	/* POSIX threads and mutexes */
52
	#include <unistd.h>	/* _POSIX_THREADS definition on FreeBSD (at least) */
53
54
55

	/* Win32 thread API wrappers */
	ulong _beginthread(void( *start_address )( void * )
deuce's avatar
deuce committed
56
			,unsigned stack_size, void *arglist);
57

58
59
	#define GetCurrentThreadId()		pthread_self()

60
61
#elif defined(_WIN32)	

62
63
	#include <process.h>	/* _beginthread */
	#include <limits.h>		/* INT_MAX */
64
	#include <errno.h>		/* EAGAIN and EBUSY */
65

66
	/* POSIX threads */
67
	typedef uintptr_t pthread_t;
68
	#define pthread_self()				GetCurrentThreadId()
69
	#define pthread_equal(t1,t2)		((t1)==(t2))
70

71
	/* POSIX mutexes */
72
73
74
75
76
	#ifdef PTHREAD_MUTEX_AS_WIN32_MUTEX	/* Much slower/heavier than critical sections */

		typedef HANDLE pthread_mutex_t;

	#else	/* Implemented as Win32 Critical Sections */
77

78
		typedef CRITICAL_SECTION pthread_mutex_t;
79
80
81
82

	#endif

#elif defined(__OS2__)
rswindell's avatar
rswindell committed
83
84

	/* POSIX mutexes */
rswindell's avatar
rswindell committed
85
	typedef TID pthread_t;
rswindell's avatar
rswindell committed
86
87
	typedef HEV pthread_mutex_t;

88
89
#else

90
	#error "Need thread wrappers."
91
92
93

#endif

94
95
96
97
/****************************************************************************/
/* Wrappers for POSIX thread (pthread) mutexes								*/
/****************************************************************************/

98
pthread_mutex_t pthread_mutex_initializer_np(BOOL recursive);
99

deuce's avatar
deuce committed
100
101
#if defined(_POSIX_THREADS)

102
103
104
#if defined (__FreeBSD__) || defined (__OpenBSD__)
 #include <pthread_np.h>
 #define	SetThreadName(c)	pthread_set_name_np(pthread_self(),c)
deuce's avatar
deuce committed
105
106
107
108
#elif defined(__GLIBC__)
 #include <features.h>
 #if (__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12))
  #define	SetThreadName(c)	pthread_setname_np(pthread_self(),c)
deuce's avatar
deuce committed
109
110
 #else
  #define SetThreadName(c)
deuce's avatar
deuce committed
111
 #endif
deuce's avatar
deuce committed
112
113
#else
 #define SetThreadName(c)
deuce's avatar
deuce committed
114
115
116
#endif

#else
117

118
119
120
121
122
DLLEXPORT int pthread_mutex_init(pthread_mutex_t*, void* attr);
DLLEXPORT int pthread_mutex_lock(pthread_mutex_t*);
DLLEXPORT int pthread_mutex_trylock(pthread_mutex_t*);
DLLEXPORT int pthread_mutex_unlock(pthread_mutex_t*);
DLLEXPORT int pthread_mutex_destroy(pthread_mutex_t*);
123

deuce's avatar
deuce committed
124
#define SetThreadName(c)
125

126
127
// A structure in case we need to add an event or something...
typedef struct {
rswindell's avatar
rswindell committed
128
	long	state;
129
130
131
} pthread_once_t;

#define PTHREAD_ONCE_INIT	{0};
132
DLLEXPORT int pthread_once(pthread_once_t *oc, void (*init)(void));
133

134
135
#endif

136
137
#if !defined(PTHREAD_MUTEX_INITIALIZER_NP)
	#define PTHREAD_MUTEX_INITIALIZER_NP			pthread_mutex_initializer_np(/* recursive: */FALSE)
138
#endif
139
140
#if !defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
	#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP	pthread_mutex_initializer_np(/* recursive: */TRUE)
141
142
#endif

143
144
145
146
147
148
149
150
151
/************************************************************************/
/* Protected (thread-safe) Integers (e.g. atomic/interlocked variables) */
/************************************************************************/
/* Use of these types and functions is not as fast as your compiler or  */
/* platform-specific functions (e.g. InterlockedIncrement on Windows or */
/* atomic_add_int on FreeBSD) but they have the advantage of always		*/
/* working and being thread-safe on all platforms that support pthread	*/
/* mutexes.																*/
/************************************************************************/
152
#if !__STDC_NO_ATOMICS__
153
154
155
156
157
#ifdef __cplusplus
typedef std::atomic<int32_t> protected_int32_t;
typedef std::atomic<uint32_t> protected_uint32_t;
typedef std::atomic<int64_t> protected_int64_t;
typedef std::atomic<uint64_t> protected_uint64_t;
158
159
160
161
#define protected_int32_init(pval, val) std::atomic_store<int32_t>(pval, val)
#define protected_uint32_init(pval, val) std::atomic_store<uint32_t>(pval, val)
#define protected_int64_init(pval, val) std::atomic_store<int64_t>(pval, val)
#define protected_uint64_init(pval, val) std::atomic_store<uint64_t>(pval, val)
Deucе's avatar
Deucе committed
162

163
164
165
166
167
#define protected_int32_set(pval, val) std::atomic_store<int32_t>(pval, val)
#define protected_uint32_set(pval, val) std::atomic_store<uint32_t>(pval, val)
#define protected_int64_set(pval, val) std::atomic_store<int64_t>(pval, val)
#define protected_uint64_(pval, val) std::atomic_store<uint64_t>(pval, val)

168
169
170
171
172
173
174
175
176
#define protected_int32_adjust(pval, adj) std::atomic_fetch_add<int32_t>(pval, adj)
#define protected_uint32_adjust(pval, adj) std::atomic_fetch_add<uint32_t>(pval, adj)
#define protected_int64_adjust(pval, adj) std::atomic_fetch_add<int64_t>(pval, adj)
#define protected_uint64_adjust(pval, adj) std::atomic_fetch_add<uint64_t>(pval, adj)

#define protected_int32_adjust_fetch(pval, adj) (std::atomic_fetch_add<int32_t>(pval, adj) + adj)
#define protected_uint32_adjust_fetch(pval, adj) (std::atomic_fetch_add<uint32_t>(pval, adj) + adj)
#define protected_int64_adjust_fetch(pval, adj) (std::atomic_fetch_add<int64_t>(pval, adj) + adj)
#define protected_uint64_adjust_fetch(pval, adj) (std::atomic_fetch_add<uint64_t>(pval, adj) + adj)
Deucе's avatar
Deucе committed
177
178
179
180
181

#define protected_int32_value(val) std::atomic_load<int32_t>(&val)
#define protected_uint32_value(val) std::atomic_load<uint32_t>(&val)
#define protected_int64_value(val) std::atomic_load<int64_t>(&val)
#define protected_uint64_value(val) std::atomic_load<uint64_t>(&val)
182
#else
Deucе's avatar
Deucе committed
183
184
185
186
187
typedef _Atomic(int32_t) protected_int32_t;
typedef _Atomic(uint32_t) protected_uint32_t;
typedef _Atomic(int64_t) protected_int64_t;
typedef _Atomic(uint64_t) protected_uint64_t;

188
189
190
191
#define protected_int32_init(pval, val) atomic_init(pval, val)
#define protected_uint32_init(pval, val) atomic_init(pval, val)
#define protected_int64_init(pval, val) atomic_init(pval, val)
#define protected_uint64_init(pval, val) atomic_init(pval, val)
Deucе's avatar
Deucе committed
192

193
194
195
196
197
#define protected_int32_set(pval, val) atomic_init(pval, val)
#define protected_uint32_set(pval, val) atomic_init(pval, val)
#define protected_int64_set(pval, val) atomic_init(pval, val)
#define protected_uint64_set(pval, val) atomic_init(pval, val)

198
199
200
201
202
203
204
205
206
#define protected_int32_adjust(pval, adj) atomic_fetch_add(pval, adj)
#define protected_uint32_adjust(pval, adj) atomic_fetch_add(pval, adj)
#define protected_int64_adjust(pval, adj) atomic_fetch_add(pval, adj)
#define protected_uint64_adjust(pval, adj) atomic_fetch_add(pval, adj)

#define protected_int32_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_uint32_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_int64_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
#define protected_uint64_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
Deucе's avatar
Deucе committed
207

208
209
210
211
#define protected_int32_value(val) atomic_load(&val)
#define protected_uint32_value(val) atomic_load(&val)
#define protected_int64_value(val) atomic_load(&val)
#define protected_uint64_value(val) atomic_load(&val)
Deucе's avatar
Deucе committed
212
#endif
213

214
215
216
217
#define protected_int32_destroy(i)
#define protected_uint32_destroy(i)
#define protected_int64_destroy(i)
#define protected_uint64_destroy(i)
Deucе's avatar
Deucе committed
218
#else
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
typedef struct {
	int32_t				value;
	pthread_mutex_t		mutex;
} protected_int32_t;

typedef struct {
	uint32_t			value;
	pthread_mutex_t		mutex;
} protected_uint32_t;

typedef struct {
	int64_t				value;
	pthread_mutex_t		mutex;
} protected_int64_t;

typedef struct {
	uint64_t			value;
	pthread_mutex_t		mutex;
} protected_uint64_t;

Deucе's avatar
Deucе committed
239
240
241
#define protected_uint32_init(i, val)	protected_int32_init((protected_int32_t*)i, val)
#define protected_uint64_init(i, val)	protected_int64_init((protected_int64_t*)i, val)
/* Return 0 on success, non-zero on failure (see pthread_mutex_destroy): */
242
#define protected_int32_destroy(i)	pthread_mutex_destroy(&(i).mutex)
Deucе's avatar
Deucе committed
243
244
245
#define protected_uint32_destroy	protected_int32_destroy	
#define protected_int64_destroy		protected_int32_destroy	
#define protected_uint64_destroy	protected_int32_destroy	
246
247
248
249
#define protected_int32_value(i)		protected_int32_adjust(&(i),0)
#define protected_uint32_value(i)		protected_uint32_adjust(&(i),0)
#define protected_int64_value(i)		protected_int64_adjust(&(i),0)
#define protected_uint64_value(i)		protected_uint64_adjust(&(i),0)
Deucе's avatar
Deucе committed
250

251
252
253
254
255
#define protected_int32_adjust_fetch(a, b)	protected_int32_adjust(a, b)
#define protected_uint32_adjust_fetch(a, b)	protected_uint32_adjust(a, b)
#define protected_int64_adjust_fetch(a, b)	protected_int64_adjust(a, b)
#define protected_uint64_adjust_fetch(a, b)	protected_uint64_adjust(a, b)

256
/* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
257
258
DLLEXPORT void protected_int32_init(protected_int32_t*,	int32_t value);
DLLEXPORT void protected_int64_init(protected_int64_t*,	int64_t value);
259
260

/* Return new value: */
261
262
263
264
265
266
267
268
DLLEXPORT int32_t protected_int32_adjust(protected_int32_t*, int32_t adjustment);
DLLEXPORT int32_t protected_int32_set(protected_int32_t*, int32_t val);
DLLEXPORT uint32_t protected_uint32_adjust(protected_uint32_t*, int32_t adjustment);
DLLEXPORT uint32_t protected_uint32_set(protected_uint32_t*, uint32_t val);
DLLEXPORT int64_t protected_int64_adjust(protected_int64_t*, int64_t adjustment);
DLLEXPORT int64_t protected_int64_set(protected_int64_t*, int64_t val);
DLLEXPORT uint64_t protected_uint64_adjust(protected_uint64_t*, int64_t adjustment);
DLLEXPORT uint64_t protected_uint64_set(protected_uint64_t*, uint64_t adjustment);
269

Deucе's avatar
Deucе committed
270
271
#endif

272
#if defined(__cplusplus)
273
274
275
}
#endif

276
277
#include "semwrap.h"

278
#endif	/* Don't add anything after this line */