ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/xthread.h
(Generate patch)

Comparing BDB/xthread.h (file contents):
Revision 1.3 by root, Sun Jul 8 11:12:12 2007 UTC vs.
Revision 1.14 by root, Tue Feb 2 04:07:06 2016 UTC

1#ifndef XTHREAD_H_
2#define XTHREAD_H_
3
1/* wether word reads are potentially non-atomic. 4/* whether word reads are potentially non-atomic.
2 * this is conservatice, likely most arches this runs 5 * this is conservative, likely most arches this runs
3 * on have atomic word read/writes. 6 * on have atomic word read/writes.
4 */ 7 */
5#ifndef WORDACCESS_UNSAFE 8#ifndef WORDACCESS_UNSAFE
6# if __i386 || __x86_64 9# if __i386 || __x86_64
7# define WORDACCESS_UNSAFE 0 10# define WORDACCESS_UNSAFE 0
11#endif 14#endif
12 15
13///////////////////////////////////////////////////////////////////////////// 16/////////////////////////////////////////////////////////////////////////////
14 17
15#ifdef _WIN32 18#ifdef _WIN32
16typedef int ssize_t;
17 19
18#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls 20//#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls, fails with mingw
19#define _WIN32_WINNT 0x400 21#define _WIN32_WINNT 0x400 // maybe working alternative for mingw
20#include <stdio.h>//D 22#include <stdio.h>//D
21#include <fcntl.h> 23#include <fcntl.h>
22#include <io.h> 24#include <io.h>
25#include <time.h>
23#include <winsock2.h> 26#include <winsock2.h>
24#include <process.h> 27#include <process.h>
25#include <windows.h> 28#include <windows.h>
29
30/* work around some bugs in ptw32 */
31#if defined(__MINGW32__) && defined(_TIMESPEC_DEFINED)
32#define HAVE_STRUCT_TIMESPEC 1
33#endif
34
35#include <pthread.h>
26#define sigset_t int 36#define sigset_t int
27#define sigfillset(a) 37#define sigfillset(a)
28#define pthread_sigmask(a,b,c) 38#define pthread_sigmask(a,b,c)
29#define sigaddset(a,b) 39#define sigaddset(a,b)
30#define sigemptyset(s) 40#define sigemptyset(s)
31#define sigfillset(s)
32 41
33#define pthread_kill(a,b) 42typedef pthread_mutex_t xmutex_t;
34#define pthread_self() 0 43#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
45#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
46#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
35 47
36typedef HANDLE mutex_t; 48typedef pthread_cond_t xcond_t;
37#define X_MUTEX_INIT 0 49#define X_COND_INIT PTHREAD_COND_INITIALIZER
38#define X_MUTEX_CHECK(mutex) if (!(mutex)) (mutex) = CreateMutex (NULL, FALSE, NULL) 50#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
39#define X_LOCK(mutex) WaitForSingleObject ((mutex), INFINITE) 51#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
40#define X_UNLOCK(mutex) ReleaseMutex (mutex) 52#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
53#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
41 54
42typedef HANDLE cond_t; 55typedef pthread_t xthread_t;
43#define X_COND_INIT 0 56#define X_THREAD_PROC(name) static void *name (void *thr_arg)
44#define X_COND_CHECK(cond) if (!(cond)) (cond) = CreateEvent (NULL, FALSE, FALSE, NULL)
45#define X_COND_SIGNAL(cond) SetEvent (cond)
46#define X_COND_WAIT(cond,mutex) do { SignalObjectAndWait ((mutex), (cond),INFINITE, FALSE); WaitForSingleObject ((mutex), INFINITE); } while (0)
47
48#define ETIMEDOUT 1
49
50struct timespec {
51 unsigned long tv_sec, tv_nsec;
52};
53
54static int
55X_COND_TIMEDWAIT (mutex_t mutex, cond_t cond, struct timespec to)
56{
57 unsigned long ms = to.tv_nsec / 1000 + to.tv_sec * 1000;
58
59 if (SignalObjectAndWait (mutex, cond, ms, FALSE) == WAIT_TIMEOUT)
60 return ETIMEDOUT;
61
62 if (WaitForSingleObject (mutex, ms) == WAIT_TIMEOUT)
63 return ETIMEDOUT;
64
65 return 0;
66}
67
68typedef DWORD thread_t;
69#define X_THREAD_PROC(name) DWORD WINAPI name (LPVOID thr_arg)
70#define X_THREAD_ATFORK(a,b,c) 57#define X_THREAD_ATFORK(a,b,c)
71 58
72static int 59static int
73thread_create (thread_t *tid, LPTHREAD_START_ROUTINE proc, void *arg) 60xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
74{ 61{
75 *tid = 0; 62 int retval;
76 CreateThread (0, 4096, proc, arg, 0, tid); 63 pthread_attr_t attr;
77 return !!*tid; 64
65 pthread_attr_init (&attr);
66 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
67
68 retval = pthread_create (tid, &attr, proc, arg) == 0;
69
70 pthread_attr_destroy (&attr);
71
72 return retval;
78} 73}
79 74
80#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) 75#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
81#define respipe_write(a,b,c) send ((a), (b), (c), 0) 76#define respipe_write(a,b,c) send ((a), (b), (c), 0)
82#define respipe_close(a) PerlSock_closesocket ((a)) 77#define respipe_close(a) PerlSock_closesocket ((a))
83 78
84#else 79#else
85///////////////////////////////////////////////////////////////////////////// 80/////////////////////////////////////////////////////////////////////////////
86 81
87/* solaris */
88#define _POSIX_PTHREAD_SEMANTICS 1
89
90#if __linux && !defined(_GNU_SOURCE) 82#if __linux && !defined(_GNU_SOURCE)
91# define _GNU_SOURCE 83# define _GNU_SOURCE
92#endif 84#endif
93 85
94/* just in case */ 86/* just in case */
95#define _REENTRANT 1 87#define _REENTRANT 1
96 88
89#if __solaris
90# define _POSIX_PTHREAD_SEMANTICS 1
91/* try to bribe solaris headers into providing a current pthread API
92 * despite environment being configured for an older version.
93 */
94# define __EXTENSIONS__ 1
95#endif
96
97#include <unistd.h> 97#include <unistd.h>
98#include <fcntl.h> 98#include <fcntl.h>
99#include <signal.h> 99#include <signal.h>
100#include <limits.h>
100#include <pthread.h> 101#include <pthread.h>
101 102
102typedef pthread_mutex_t mutex_t; 103typedef pthread_mutex_t xmutex_t;
103#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 104#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
104# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 105# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
106# define X_MUTEX_CREATE(mutex) \
107 do { \
108 pthread_mutexattr_t attr; \
109 pthread_mutexattr_init (&attr); \
110 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
111 pthread_mutex_init (&(mutex), &attr); \
112 } while (0)
105#else 113#else
106# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 114# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
115# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
107#endif 116#endif
108#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 117#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
109#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 118#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
110 119
111typedef pthread_cond_t cond_t; 120typedef pthread_cond_t xcond_t;
112#define X_COND_INIT PTHREAD_COND_INITIALIZER 121#define X_COND_INIT PTHREAD_COND_INITIALIZER
122#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
113#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 123#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
114#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 124#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
115#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 125#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
116 126
117typedef pthread_t thread_t; 127typedef pthread_t xthread_t;
118#define X_THREAD_PROC(name) static void *name (void *thr_arg) 128#define X_THREAD_PROC(name) static void *name (void *thr_arg)
119#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 129#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
120 130
131// the broken bsd's once more
132#ifndef PTHREAD_STACK_MIN
133# define PTHREAD_STACK_MIN 0
134#endif
135
136#ifndef X_STACKSIZE
137# define X_STACKSIZE sizeof (void *) * 4096
138#endif
139
121static int 140static int
122thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 141xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
123{ 142{
124 int retval; 143 int retval;
125 sigset_t fullsigset, oldsigset; 144 sigset_t fullsigset, oldsigset;
126 pthread_attr_t attr; 145 pthread_attr_t attr;
127 146
128 pthread_attr_init (&attr); 147 pthread_attr_init (&attr);
129 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 148 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
149 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
130#ifdef PTHREAD_SCOPE_PROCESS 150#ifdef PTHREAD_SCOPE_PROCESS
131 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 151 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
132#endif 152#endif
133 153
134 sigfillset (&fullsigset); 154 sigfillset (&fullsigset);
135 155
136 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 156 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
137 retval = pthread_create (tid, &attr, proc, arg) == 0; 157 retval = pthread_create (tid, &attr, proc, arg) == 0;
138 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 158 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
139 159
160 pthread_attr_destroy (&attr);
161
140 return retval; 162 return retval;
141} 163}
142 164
143#define respipe_read(a,b,c) read ((a), (b), (c)) 165#define respipe_read(a,b,c) read ((a), (b), (c))
144#define respipe_write(a,b,c) write ((a), (b), (c)) 166#define respipe_write(a,b,c) write ((a), (b), (c))
145#define respipe_close(a) close ((a)) 167#define respipe_close(a) close ((a))
146 168
147#endif 169#endif
148 170
171#if __linux && __GNUC__ >= 4 && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 3 && 0 /* also check arch */
172/* __thread has little to no advantage over pthread_* in most configurations, so this is not used */
173# define X_TLS_DECLARE(varname) __thread void *varname
174# define X_TLS_INIT(varname)
175# define X_TLS_SET(varname,value) varname = (value)
176# define X_TLS_GET(varname) varname
177#else
178# define X_TLS_DECLARE(varname) pthread_key_t varname
179# define X_TLS_INIT(varname) do { if (pthread_key_create (&(varname), 0)) abort (); } while (0)
180# define X_TLS_SET(varname,value) pthread_setspecific (varname, (value))
181# define X_TLS_GET(varname) pthread_getspecific (varname)
182#endif
149 183
184#endif
150 185

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines