Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

threadwrap.h 7.86 KB
Newer Older
1 2
/* Thread-related cross-platform development wrappers */

3
/* $Id: threadwrap.h,v 1.52 2019/07/24 04:15:24 rswindell Exp $ */
4
// vi: tabstop=4
5 6 7 8 9

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
10
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
11
 *																			*
12 13
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
14 15
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
16 17
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * 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 */
41
#include "wrapdll.h"	/* DLLEXPORT and DLLCALL */
42

43
#ifdef __unix__
Deucе's avatar
Deucе committed
44 45 46
#include <stdatomic.h>
#endif

47
#if defined(__cplusplus)
48 49 50
extern "C" {
#endif

51
#if defined(__unix__)
52

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

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

61 62
	#define GetCurrentThreadId()		pthread_self()

63 64
#elif defined(_WIN32)	

65 66
	#include <process.h>	/* _beginthread */
	#include <limits.h>		/* INT_MAX */
67
	#include <errno.h>		/* EAGAIN and EBUSY */
68

69 70 71
	/* POSIX threads */
	typedef DWORD pthread_t;
	#define pthread_self()				GetCurrentThreadId()
72
	#define pthread_equal(t1,t2)		((t1)==(t2))
73

74
	/* POSIX mutexes */
75 76 77 78 79
	#ifdef PTHREAD_MUTEX_AS_WIN32_MUTEX	/* Much slower/heavier than critical sections */

		typedef HANDLE pthread_mutex_t;

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

81 82 83 84 85
		typedef CRITICAL_SECTION pthread_mutex_t;

	#endif

#elif defined(__OS2__)
rswindell's avatar
rswindell committed
86 87

	/* POSIX mutexes */
rswindell's avatar
rswindell committed
88
	typedef TID pthread_t;
rswindell's avatar
rswindell committed
89 90
	typedef HEV pthread_mutex_t;

91 92
#else

93
	#error "Need thread wrappers."
94 95 96

#endif

97 98 99 100
/****************************************************************************/
/* Wrappers for POSIX thread (pthread) mutexes								*/
/****************************************************************************/

101
pthread_mutex_t pthread_mutex_initializer_np(BOOL recursive);
102

deuce's avatar
deuce committed
103 104
#if defined(_POSIX_THREADS)

105 106 107
#if defined (__FreeBSD__) || defined (__OpenBSD__)
 #include <pthread_np.h>
 #define	SetThreadName(c)	pthread_set_name_np(pthread_self(),c)
deuce's avatar
deuce committed
108 109 110 111
#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
112 113
 #else
  #define SetThreadName(c)
deuce's avatar
deuce committed
114
 #endif
deuce's avatar
deuce committed
115 116
#else
 #define SetThreadName(c)
deuce's avatar
deuce committed
117 118 119
#endif

#else
120

deuce's avatar
deuce committed
121 122 123 124 125
DLLEXPORT int DLLCALL pthread_mutex_init(pthread_mutex_t*, void* attr);
DLLEXPORT int DLLCALL pthread_mutex_lock(pthread_mutex_t*);
DLLEXPORT int DLLCALL pthread_mutex_trylock(pthread_mutex_t*);
DLLEXPORT int DLLCALL pthread_mutex_unlock(pthread_mutex_t*);
DLLEXPORT int DLLCALL pthread_mutex_destroy(pthread_mutex_t*);
126

deuce's avatar
deuce committed
127
#define SetThreadName(c)
128

129 130
// A structure in case we need to add an event or something...
typedef struct {
rswindell's avatar
rswindell committed
131
	long	state;
132 133 134 135 136
} pthread_once_t;

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

137 138
#endif

139 140
#if !defined(PTHREAD_MUTEX_INITIALIZER_NP)
	#define PTHREAD_MUTEX_INITIALIZER_NP			pthread_mutex_initializer_np(/* recursive: */FALSE)
141
#endif
142 143
#if !defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
	#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP	pthread_mutex_initializer_np(/* recursive: */TRUE)
144 145
#endif

146 147 148 149 150 151 152 153 154
/************************************************************************/
/* 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.																*/
/************************************************************************/
155
#ifdef __unix__
Deucе's avatar
Deucе committed
156 157 158 159 160 161 162 163 164 165 166 167 168
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;

DLLEXPORT int DLLCALL protected_uint32_init(protected_uint32_t*,	uint32_t value);
DLLEXPORT int DLLCALL protected_uint64_init(protected_uint64_t*,	uint64_t value);

#define protected_int32_destroy(i)	0
#define protected_uint32_destroy(i)	0
#define protected_int64_destroy(i)	0
#define protected_uint64_destroy(i)	0
#else
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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
189 190 191 192 193 194 195 196 197
#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): */
#define protected_int32_destroy(i)	pthread_mutex_destroy(&i.mutex)
#define protected_uint32_destroy	protected_int32_destroy	
#define protected_int64_destroy		protected_int32_destroy	
#define protected_uint64_destroy	protected_int32_destroy	
#endif

198
/* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
deuce's avatar
deuce committed
199 200
DLLEXPORT int DLLCALL protected_int32_init(protected_int32_t*,	int32_t value);
DLLEXPORT int DLLCALL protected_int64_init(protected_int64_t*,	int64_t value);
201 202

/* Return new value: */
203 204
DLLEXPORT int32_t DLLCALL protected_int32_adjust(protected_int32_t*, int32_t adjustment);
DLLEXPORT int32_t DLLCALL protected_int32_set(protected_int32_t*, int32_t val);
205
#define protected_int32_value(i)		protected_int32_adjust(&i,0)
206 207
DLLEXPORT uint32_t DLLCALL protected_uint32_adjust(protected_uint32_t*, int32_t adjustment);
DLLEXPORT uint32_t DLLCALL protected_uint32_set(protected_uint32_t*, uint32_t val);
208
#define protected_uint32_value(i)		protected_uint32_adjust(&i,0)
209 210
DLLEXPORT int64_t DLLCALL protected_int64_adjust(protected_int64_t*, int64_t adjustment);
DLLEXPORT int64_t DLLCALL protected_int64_set(protected_int64_t*, int64_t val);
211
#define protected_int64_value(i)		protected_int64_adjust(&i,0)
212 213
DLLEXPORT uint64_t DLLCALL protected_uint64_adjust(protected_uint64_t*, int64_t adjustment);
DLLEXPORT uint64_t DLLCALL protected_uint64_set(protected_uint64_t*, uint64_t adjustment);
214
#define protected_uint64_value(i)		protected_uint64_adjust(&i,0)
215

216
#if defined(__cplusplus)
217 218 219
}
#endif

220 221
#include "semwrap.h"

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