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

Comparing BDB/xthread.h (file contents):
Revision 1.1 by root, Sat May 12 21:18:13 2007 UTC vs.
Revision 1.14 by root, Tue Feb 2 04:07:06 2016 UTC

1/* solaris */ 1#ifndef XTHREAD_H_
2#define _POSIX_PTHREAD_SEMANTICS 1 2#define XTHREAD_H_
3 3
4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
6#endif
7
8/* just in case */
9#define _REENTRANT 1
10
11#include <signal.h>
12#include <pthread.h>
13
14#ifndef PTHREAD_STACK_MIN
15/* care for broken platforms, e.g. windows */
16# define PTHREAD_STACK_MIN 16384
17#endif
18
19#if __ia64
20# define STACKSIZE 65536
21#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
22# define STACKSIZE PTHREAD_STACK_MIN
23#else
24# define STACKSIZE 16384
25#endif
26
27/* wether word reads are potentially non-atomic. 4/* whether word reads are potentially non-atomic.
28 * this is conservatice, likely most arches this runs 5 * this is conservative, likely most arches this runs
29 * on have atomic word read/writes. 6 * on have atomic word read/writes.
30 */ 7 */
31#ifndef WORDACCESS_UNSAFE 8#ifndef WORDACCESS_UNSAFE
32# if __i386 || __x86_64 9# if __i386 || __x86_64
33# define WORDACCESS_UNSAFE 0 10# define WORDACCESS_UNSAFE 0
34# else 11# else
35# define WORDACCESS_UNSAFE 1 12# define WORDACCESS_UNSAFE 1
36# endif 13# endif
37#endif 14#endif
38 15
39typedef pthread_mutex_t mutex_t; 16/////////////////////////////////////////////////////////////////////////////
40#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 17
41# define MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 18#ifdef _WIN32
42#else 19
43# define MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 20//#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls, fails with mingw
21#define _WIN32_WINNT 0x400 // maybe working alternative for mingw
22#include <stdio.h>//D
23#include <fcntl.h>
24#include <io.h>
25#include <time.h>
26#include <winsock2.h>
27#include <process.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
44#endif 33#endif
45 34
46typedef pthread_cond_t cond_t; 35#include <pthread.h>
47#define COND_INIT PTHREAD_COND_INITIALIZER 36#define sigset_t int
37#define sigfillset(a)
38#define pthread_sigmask(a,b,c)
39#define sigaddset(a,b)
40#define sigemptyset(s)
48 41
42typedef pthread_mutex_t xmutex_t;
43#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
49#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 45#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
50#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 46#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
51 47
48typedef pthread_cond_t xcond_t;
49#define X_COND_INIT PTHREAD_COND_INITIALIZER
50#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
52#define COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 51#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
53#define COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 52#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
54#define COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 53#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
55 54
56typedef pthread_t thread_t; 55typedef pthread_t xthread_t;
56#define X_THREAD_PROC(name) static void *name (void *thr_arg)
57#define X_THREAD_ATFORK(a,b,c)
57 58
59static int
58static int thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 60xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
61{
62 int retval;
63 pthread_attr_t attr;
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;
73}
74
75#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
76#define respipe_write(a,b,c) send ((a), (b), (c), 0)
77#define respipe_close(a) PerlSock_closesocket ((a))
78
79#else
80/////////////////////////////////////////////////////////////////////////////
81
82#if __linux && !defined(_GNU_SOURCE)
83# define _GNU_SOURCE
84#endif
85
86/* just in case */
87#define _REENTRANT 1
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>
98#include <fcntl.h>
99#include <signal.h>
100#include <limits.h>
101#include <pthread.h>
102
103typedef pthread_mutex_t xmutex_t;
104#if __linux && defined (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)
113#else
114# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
115# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
116#endif
117#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
118#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
119
120typedef pthread_cond_t xcond_t;
121#define X_COND_INIT PTHREAD_COND_INITIALIZER
122#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
123#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
124#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
125#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
126
127typedef pthread_t xthread_t;
128#define X_THREAD_PROC(name) static void *name (void *thr_arg)
129#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
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
140static int
141xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
59{ 142{
60 int retval; 143 int retval;
61 sigset_t fullsigset, oldsigset; 144 sigset_t fullsigset, oldsigset;
62 pthread_attr_t attr; 145 pthread_attr_t attr;
63 146
64 pthread_attr_init (&attr); 147 pthread_attr_init (&attr);
65 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);
66#ifdef PTHREAD_SCOPE_PROCESS 150#ifdef PTHREAD_SCOPE_PROCESS
67 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 151 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
68#endif 152#endif
69 153
70 sigfillset (&fullsigset); 154 sigfillset (&fullsigset);
71 155
72 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 156 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
73 retval = pthread_create (tid, &attr, proc, arg) == 0; 157 retval = pthread_create (tid, &attr, proc, arg) == 0;
74 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 158 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
75 159
160 pthread_attr_destroy (&attr);
161
76 return retval; 162 return retval;
77} 163}
78 164
79#define ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 165#define respipe_read(a,b,c) read ((a), (b), (c))
166#define respipe_write(a,b,c) write ((a), (b), (c))
167#define respipe_close(a) close ((a))
80 168
169#endif
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
183
184#endif
185

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines