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.c 9.11 KB
Newer Older
1 2 3 4
/* threadwrap.c */

/* Thread-related cross-platform development wrappers */

5
/* $Id: threadwrap.c,v 1.37 2019/02/18 04:04:11 rswindell Exp $ */
6 7 8 9 10

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
12
 *																			*
13 14
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
15 16
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
17 18
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *																			*
 * 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.	*
 ****************************************************************************/

38 39
#if defined(__unix__)
	#include <unistd.h>	/* _POSIX_THREADS */
deuce's avatar
deuce committed
40
	#include <sys/param.h>	/* BSD */
41 42
#endif

43 44 45 46
#if defined(_WIN32) && !defined(_WIN32_WINNT)
	#define _WIN32_WINNT 0x0400	/* Needed for TryEnterCriticalSection */
#endif

47
#include "genwrap.h"	/* SLEEP() */
Rob Swindell's avatar
Rob Swindell committed
48
#include "threadwrap.h"
49 50 51 52 53

/****************************************************************************/
/* Wrapper for Win32 create/begin thread function							*/
/* Uses POSIX threads														*/
/****************************************************************************/
54
#if defined(__unix__)
55
#if defined(_POSIX_THREADS)
Rob Swindell's avatar
Rob Swindell committed
56
ulong _beginthread(void( *start_address )( void * )
57 58 59
		,unsigned stack_size, void *arglist)
{
	pthread_t	thread;
60 61 62
	pthread_attr_t attr;
	size_t		default_stack;

63 64
	(void)stack_size;

65
	pthread_attr_init(&attr);     /* initialize attribute structure */
66

67 68 69
	/* set thread attributes to PTHREAD_CREATE_DETACHED which will ensure
	   that thread resources are freed on exit() */
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
70

71
	/* Default stack size in BSD is too small for JS stuff */
72 73
	/* Force to at least 256k */
#define XPDEV_MIN_THREAD_STACK_SIZE	(256*1024)
74 75
	if(stack_size==0 && pthread_attr_getstacksize(&attr, &default_stack)==0 
			&& default_stack < XPDEV_MIN_THREAD_STACK_SIZE)
76 77
		stack_size=XPDEV_MIN_THREAD_STACK_SIZE;

deuce's avatar
deuce committed
78 79
	if(stack_size!=0)
		pthread_attr_setstacksize(&attr, stack_size);
80

81
	if(pthread_create(&thread
82
#if defined(__BORLANDC__) /* a (hopefully temporary) work-around */
83
			,NULL
84
#else
85
			,&attr	/* default attributes */
86
#endif
87 88 89
			/* POSIX defines this arg as "void *(*start_address)" */
			,(void * (*)(void *)) start_address
			,arglist)==0) {
90
		pthread_attr_destroy(&attr);
deuce's avatar
deuce committed
91
		return((ulong) thread /* thread handle */);
92
	}
93

94
	pthread_attr_destroy(&attr);
95 96 97 98 99 100 101 102 103
	return(-1);	/* error */
}
#else

#error "Need _beginthread implementation for non-POSIX thread library."

#endif

#endif	/* __unix__ */
104 105 106 107

/****************************************************************************/
/* Wrappers for POSIX thread (pthread) mutexes								*/
/****************************************************************************/
Rob Swindell's avatar
Rob Swindell committed
108
pthread_mutex_t pthread_mutex_initializer_np(BOOL recursive)
109 110
{
	pthread_mutex_t	mutex;
111 112 113 114
#if defined(_POSIX_THREADS)
	pthread_mutexattr_t attr;
	pthread_mutexattr_init(&attr);
	if(recursive)
115
#if defined(__linux__) && defined(PTHREAD_MUTEX_RECURSIVE_NP) && !defined(__USE_UNIX98)
116 117
		pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE_NP);
#else
118
		pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
119
#endif
120 121
	pthread_mutex_init(&mutex, &attr);
#else	/* Assumes recursive (e.g. Windows) */
rswindell's avatar
rswindell committed
122
	(void)recursive;
123
	pthread_mutex_init(&mutex,NULL);
124
#endif
125 126 127 128 129
	return(mutex);
}

#if !defined(_POSIX_THREADS)

Rob Swindell's avatar
Rob Swindell committed
130
int pthread_once(pthread_once_t *oc, void (*init)(void))
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
	if (oc == NULL || init == NULL)
		return EINVAL;
	switch(InterlockedCompareExchange(&(oc->state), 1, 0)) {
		case 0:	// Never called
			init();
			InterlockedIncrement(&(oc->state));
			return 0;
		case 1:	// In init function
			/* We may not need to use InterlockedCompareExchange() here,
			 * but I hate marking things as volatile, and hate tight loops
			 * testing things that aren't marked volatile.
			 */
			while(InterlockedCompareExchange(&(oc->state), 1, 0) != 2)
				SLEEP(1);
			return 0;
		case 2:	// Done.
			return 0;
	}
150
	return EINVAL;
151 152
}

Rob Swindell's avatar
Rob Swindell committed
153
int pthread_mutex_init(pthread_mutex_t* mutex, void* attr)
154
{
155
	(void)attr;
156 157 158 159 160 161 162 163 164 165
#if defined(PTHREAD_MUTEX_AS_WIN32_MUTEX)
	return ((((*mutex)=CreateMutex(/* security */NULL, /* owned */FALSE, /* name */NULL))==NULL) ? -1 : 0);
#elif defined(_WIN32)	/* Win32 Critical Section */
	InitializeCriticalSection(mutex);
	return 0;	/* No error */
#elif defined(__OS2__)
	return DosCreateMutexSem(/* name */NULL, mutex, /* attr */0, /* owned */0);
#endif
}

Rob Swindell's avatar
Rob Swindell committed
166
int pthread_mutex_lock(pthread_mutex_t* mutex)
167 168 169 170 171 172 173 174 175 176 177
{
#if defined(PTHREAD_MUTEX_AS_WIN32_MUTEX)
	return (WaitForSingleObject(*mutex, INFINITE)==WAIT_OBJECT_0 ? 0 : EBUSY);
#elif defined(_WIN32)	/* Win32 Critical Section */
	EnterCriticalSection(mutex);
	return 0;	/* No error */
#elif defined(__OS2__)
	return DosRequestMutexSem(*mutex, -1 /* SEM_INDEFINITE_WAIT */);
#endif
}

Rob Swindell's avatar
Rob Swindell committed
178
int pthread_mutex_trylock(pthread_mutex_t* mutex)
179 180 181 182 183 184 185 186 187 188 189
{
#if defined(PTHREAD_MUTEX_AS_WIN32_MUTEX)
	return (WaitForSingleObject(*mutex, 0)==WAIT_OBJECT_0 ? 0 : EBUSY);
#elif defined(_WIN32)	/* Win32 Critical Section */
	/* TryEnterCriticalSection only available on NT4+ :-( */
	return (TryEnterCriticalSection(mutex) ? 0 : EBUSY);
#elif defined(__OS2__)
	return DosRequestMutexSem(*mutex, 0 /* SEM_IMMEDIATE_RETURN */);
#endif
}

Rob Swindell's avatar
Rob Swindell committed
190
int pthread_mutex_unlock(pthread_mutex_t* mutex)
191 192 193 194 195 196 197 198 199 200 201
{
#if defined(PTHREAD_MUTEX_AS_WIN32_MUTEX)
	return (ReleaseMutex(*mutex) ? 0 : GetLastError());
#elif defined(_WIN32)	/* Win32 Critical Section */
	LeaveCriticalSection(mutex);
	return 0;	/* No error */
#elif defined(__OS2__)
	return DosReleaseMutexSem(*mutex);
#endif
}

Rob Swindell's avatar
Rob Swindell committed
202
int pthread_mutex_destroy(pthread_mutex_t* mutex)
203 204 205 206 207 208 209 210 211 212 213
{
#if defined(PTHREAD_MUTEX_AS_WIN32_MUTEX)
	return (CloseHandle(*mutex) ? 0 : GetLastError());
#elif defined(_WIN32)	/* Win32 Critical Section */
	DeleteCriticalSection(mutex);
	return 0;	/* No error */
#elif defined(__OS2__)
	return DosCloseMutexSem(*mutex);
#endif
}

rswindell's avatar
rswindell committed
214
#endif	/* POSIX thread mutexes */
215 216 217 218 219

/************************************************************************/
/* Protected (thread-safe) Integers (e.g. atomic/interlocked variables) */
/************************************************************************/

220
#if __STDC_NO_ATOMICS__
Rob Swindell's avatar
Rob Swindell committed
221
void protected_int32_init(protected_int32_t* prot, int32_t value)
222 223
{
	prot->value = value;
224
	pthread_mutex_init(&prot->mutex,NULL);
225 226
}

Rob Swindell's avatar
Rob Swindell committed
227
void protected_int64_init(protected_int64_t* prot, int64_t value)
228 229
{
	prot->value = value;
230
	pthread_mutex_init(&prot->mutex,NULL);
231 232
}

Rob Swindell's avatar
Rob Swindell committed
233
int32_t protected_int32_adjust(protected_int32_t* i, int32_t adjustment)
234 235 236 237 238 239 240 241
{
	int32_t	newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value += adjustment;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}

Rob Swindell's avatar
Rob Swindell committed
242
uint32_t protected_uint32_adjust(protected_uint32_t* i, int32_t adjustment)
243 244 245 246 247 248 249 250
{
	uint32_t newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value += adjustment;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}

Rob Swindell's avatar
Rob Swindell committed
251
int64_t protected_int64_adjust(protected_int64_t* i, int64_t adjustment)
252 253 254 255 256 257 258 259
{
	int64_t	newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value += adjustment;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}

Rob Swindell's avatar
Rob Swindell committed
260
uint64_t protected_uint64_adjust(protected_uint64_t* i, int64_t adjustment)
261 262 263 264 265 266 267
{
	uint64_t newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value += adjustment;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}
268

Rob Swindell's avatar
Rob Swindell committed
269
int32_t protected_int32_set(protected_int32_t* i, int32_t val)
270 271 272 273 274 275 276 277
{
	int32_t	newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value = val;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}

Rob Swindell's avatar
Rob Swindell committed
278
uint32_t protected_uint32_set(protected_uint32_t* i, uint32_t val)
279 280 281 282 283 284 285 286
{
	uint32_t newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value = val;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}

Rob Swindell's avatar
Rob Swindell committed
287
int64_t protected_int64_set(protected_int64_t* i, int64_t val)
288 289 290 291 292 293 294 295
{
	int64_t	newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value = val;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}

Rob Swindell's avatar
Rob Swindell committed
296
uint64_t protected_uint64_set(protected_uint64_t* i, uint64_t val)
297 298 299 300 301 302 303
{
	uint64_t newval;
	pthread_mutex_lock(&i->mutex);
	newval = i->value = val;
	pthread_mutex_unlock(&i->mutex);
	return newval;
}
Deucе's avatar
Deucе committed
304
#endif