|
|
1.1 root 1: /* threadwrap.h */
2:
3: /* Thread-related cross-platform development wrappers */
4:
1.1.1.2 ! root 5: /* $Id: threadwrap.h,v 1.40 2011/09/09 08:06:15 rswindell Exp $ */
1.1 root 6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
1.1.1.2 ! root 11: * Copyright 2011 Rob Swindell - http://www.synchro.net/copyright.html *
1.1 root 12: * *
13: * This library is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU Lesser General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU Lesser General Public License for more details: lgpl.txt or *
18: * http://www.fsf.org/copyleft/lesser.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #ifndef _THREADWRAP_H
39: #define _THREADWRAP_H
40:
41: #include "gen_defs.h" /* HANDLE */
42: #include "wrapdll.h" /* DLLEXPORT and DLLCALL */
43:
44: #if defined(__cplusplus)
45: extern "C" {
46: #endif
47:
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: /* Win32 thread API wrappers */
55: ulong _beginthread(void( *start_address )( void * )
56: ,unsigned stack_size, void *arglist);
57:
58: #define GetCurrentThreadId() pthread_self()
59:
60: #elif defined(_WIN32)
61:
62: #include <process.h> /* _beginthread */
63: #include <limits.h> /* INT_MAX */
64: #include <errno.h> /* EAGAIN and EBUSY */
65:
66: /* POSIX threads */
67: typedef DWORD pthread_t;
68: #define pthread_self() GetCurrentThreadId()
1.1.1.2 ! root 69: #define pthread_equal(t1,t2) ((t1)==(t2))
1.1 root 70:
71: /* POSIX mutexes */
72: #ifdef PTHREAD_MUTEX_AS_WIN32_MUTEX /* Much slower/heavier than critical sections */
73:
74: typedef HANDLE pthread_mutex_t;
75:
76: #else /* Implemented as Win32 Critical Sections */
77:
78: typedef CRITICAL_SECTION pthread_mutex_t;
79:
80: #endif
81:
82: #elif defined(__OS2__)
83:
84: /* POSIX mutexes */
85: typedef TID pthread_t;
86: typedef HEV pthread_mutex_t;
87:
88: #else
89:
90: #error "Need thread wrappers."
91:
92: #endif
93:
94: /****************************************************************************/
95: /* Wrappers for POSIX thread (pthread) mutexes */
96: /****************************************************************************/
97:
1.1.1.2 ! root 98: pthread_mutex_t pthread_mutex_initializer_np(BOOL recursive);
1.1 root 99:
100: #if defined(_POSIX_THREADS)
101:
102: #ifdef _DEBUG
103: #if defined (__FreeBSD__) || defined (__OpenBSD__)
104: #include <pthread_np.h>
105: #define SetThreadName(c) pthread_set_name_np(pthread_self(),c)
106: #else
107: #define SetThreadName(c)
108: #endif
109: #else
110: #define SetThreadName(c)
111: #endif
112:
113: #else
114:
115: int pthread_mutex_init(pthread_mutex_t*, void* attr);
116: int pthread_mutex_lock(pthread_mutex_t*);
117: int pthread_mutex_trylock(pthread_mutex_t*);
118: int pthread_mutex_unlock(pthread_mutex_t*);
119: int pthread_mutex_destroy(pthread_mutex_t*);
120:
121: #define SetThreadName(c)
122:
123: #endif
124:
1.1.1.2 ! root 125: #if !defined(PTHREAD_MUTEX_INITIALIZER_NP)
! 126: #define PTHREAD_MUTEX_INITIALIZER_NP pthread_mutex_initializer_np(/* recursive: */FALSE)
! 127: #endif
! 128: #if !defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
! 129: #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP pthread_mutex_initializer_np(/* recursive: */TRUE)
! 130: #endif
! 131:
! 132: /************************************************************************/
! 133: /* Protected (thread-safe) Integers (e.g. atomic/interlocked variables) */
! 134: /************************************************************************/
! 135: /* Use of these types and functions is not as fast as your compiler or */
! 136: /* platform-specific functions (e.g. InterlockedIncrement on Windows or */
! 137: /* atomic_add_int on FreeBSD) but they have the advantage of always */
! 138: /* working and being thread-safe on all platforms that support pthread */
! 139: /* mutexes. */
! 140: /************************************************************************/
! 141: typedef struct {
! 142: int32_t value;
! 143: pthread_mutex_t mutex;
! 144: } protected_int32_t;
! 145:
! 146: typedef struct {
! 147: uint32_t value;
! 148: pthread_mutex_t mutex;
! 149: } protected_uint32_t;
! 150:
! 151: typedef struct {
! 152: int64_t value;
! 153: pthread_mutex_t mutex;
! 154: } protected_int64_t;
! 155:
! 156: typedef struct {
! 157: uint64_t value;
! 158: pthread_mutex_t mutex;
! 159: } protected_uint64_t;
! 160:
! 161: /* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
! 162: int protected_int32_init(protected_int32_t*, int32_t value);
! 163: #define protected_uint32_init(i, val) protected_int32_init((protected_int32_t*)i, val)
! 164: int protected_int64_init(protected_int64_t*, int64_t value);
! 165: #define protected_uint64_init(i, val) protected_int64_init((protected_int64_t*)i, val)
! 166:
! 167: /* Return new value: */
! 168: int32_t protected_int32_adjust(protected_int32_t*, int32_t adjustment);
! 169: uint32_t protected_uint32_adjust(protected_uint32_t*,int32_t adjustment);
! 170: int64_t protected_int64_adjust(protected_int64_t*, int64_t adjustment);
! 171: uint64_t protected_uint64_adjust(protected_uint64_t*,int64_t adjustment);
! 172:
! 173: /* Return 0 on success, non-zero on failure (see pthread_mutex_destroy): */
! 174: #define protected_int32_destroy(i) pthread_mutex_destroy(&i.mutex)
! 175: #define protected_uint32_destroy protected_int32_destroy
! 176: #define protected_int64_destroy protected_int32_destroy
! 177: #define protected_uint64_destroy protected_int32_destroy
! 178:
1.1 root 179: #if defined(__cplusplus)
180: }
181: #endif
182:
183: #include "semwrap.h"
184:
185: #endif /* Don't add anything after this line */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.