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

Comparing BDB/xthread.h (file contents):
Revision 1.2 by root, Sun Jul 8 09:16:19 2007 UTC vs.
Revision 1.10 by root, Sat Oct 23 09:40:53 2010 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 conservatice, 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
18#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls 21#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls
19#define _WIN32_WINNT 0x400 22#define _WIN32_WINNT 0x400
20#include <stdio.h>//D 23#include <stdio.h>//D
21#include <fcntl.h> 24#include <fcntl.h>
22#include <io.h> 25#include <io.h>
26#include <time.h>
23#include <winsock2.h> 27#include <winsock2.h>
24#include <process.h> 28#include <process.h>
25#include <windows.h> 29#include <windows.h>
30#include <pthread.h>
26#define sigset_t int 31#define sigset_t int
27#define sigfillset(a)
28#define pthread_sigmask(a,b,c) 32#define pthread_sigmask(a,b,c)
29#define sigaddset(a,b) 33#define sigaddset(a,b)
30#define sigemptyset(s) 34#define sigemptyset(s)
31#define sigfillset(s) 35#define sigfillset(s)
32 36
33#define pthread_kill(a,b) 37typedef pthread_mutex_t xmutex_t;
34#define pthread_self() 0 38#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
39#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
40#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
35 41
36typedef HANDLE mutex_t; 42typedef pthread_cond_t xcond_t;
37#define X_MUTEX_INIT 0 43#define X_COND_INIT PTHREAD_COND_INITIALIZER
38#define X_MUTEX_CHECK(mutex) if (!(mutex)) (mutex) = CreateMutex (NULL, FALSE, NULL) 44#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
39#define X_LOCK(mutex) WaitForSingleObject ((mutex), INFINITE) 45#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
40#define X_UNLOCK(mutex) ReleaseMutex (mutex) 46#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
41 47
42typedef HANDLE cond_t; 48typedef pthread_t xthread_t;
43#define X_COND_INIT 0 49#define X_THREAD_PROC(name) 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) 50#define X_THREAD_ATFORK(a,b,c)
71 51
72static int 52static int
73thread_create (thread_t *tid, LPTHREAD_START_ROUTINE proc, void *arg) 53thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
74{ 54{
75 *tid = 0; 55 int retval;
76 CreateThread (0, 4096, proc, arg, 0, tid); 56 pthread_attr_t attr;
77 return !!*tid; 57
58 pthread_attr_init (&attr);
59 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
60
61 retval = pthread_create (tid, &attr, proc, arg) == 0;
62
63 pthread_attr_destroy (&attr);
64
65 return retval;
78} 66}
79 67
80int Perl_my_socketpair (int family, int type, int protocol, int fd[2]); 68#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
81 69#define respipe_write(a,b,c) send ((a), (b), (c), 0)
82static int 70#define respipe_close(a) PerlSock_closesocket ((a))
83create_pipe (int fd[2])
84{
85 int arg = 1;
86 Perl_my_socketpair (AF_UNIX, SOCK_STREAM, 0, fd);
87 ioctlsocket (fd [0], FIONBIO, &arg);
88 ioctlsocket (fd [1], FIONBIO, &arg);
89
90 return 1;
91}
92 71
93#else 72#else
94///////////////////////////////////////////////////////////////////////////// 73/////////////////////////////////////////////////////////////////////////////
95
96/* solaris */
97#define _POSIX_PTHREAD_SEMANTICS 1
98 74
99#if __linux && !defined(_GNU_SOURCE) 75#if __linux && !defined(_GNU_SOURCE)
100# define _GNU_SOURCE 76# define _GNU_SOURCE
101#endif 77#endif
102 78
103/* just in case */ 79/* just in case */
104#define _REENTRANT 1 80#define _REENTRANT 1
105 81
82#if __solaris
83# define _POSIX_PTHREAD_SEMANTICS 1
84/* try to bribe solaris headers into providing a current pthread API
85 * despite environment being configured for an older version.
86 */
87# define __EXTENSIONS__ 1
88#endif
89
106#include <unistd.h> 90#include <unistd.h>
107#include <fcntl.h> 91#include <fcntl.h>
108#include <signal.h> 92#include <signal.h>
93#include <limits.h>
109#include <pthread.h> 94#include <pthread.h>
110 95
111#ifndef PTHREAD_STACK_MIN
112/* care for broken platforms, e.g. windows */
113# define PTHREAD_STACK_MIN 16384
114#endif
115
116typedef pthread_mutex_t mutex_t; 96typedef pthread_mutex_t xmutex_t;
117#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 97#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
118# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 98# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
119#else 99#else
120# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 100# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
121#endif 101#endif
122#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 102#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
123#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 103#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
124 104
125typedef pthread_cond_t cond_t; 105typedef pthread_cond_t xcond_t;
126#define X_COND_INIT PTHREAD_COND_INITIALIZER 106#define X_COND_INIT PTHREAD_COND_INITIALIZER
127#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 107#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
128#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 108#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
129#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 109#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
130 110
131typedef pthread_t thread_t; 111typedef pthread_t xthread_t;
132#define X_THREAD_PROC(name) static void *name (void *thr_arg) 112#define X_THREAD_PROC(name) static void *name (void *thr_arg)
133#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 113#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
134 114
115// the broken bsd's once more
116#ifndef PTHREAD_STACK_MIN
117# define PTHREAD_STACK_MIN 0
118#endif
119
120#ifndef X_STACKSIZE
121# define X_STACKSIZE sizeof (long) * 4096
122#endif
123
135static int 124static int
136thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 125thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
137{ 126{
138 int retval; 127 int retval;
139 sigset_t fullsigset, oldsigset; 128 sigset_t fullsigset, oldsigset;
140 pthread_attr_t attr; 129 pthread_attr_t attr;
141 130
142 pthread_attr_init (&attr); 131 pthread_attr_init (&attr);
143 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 132 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
133 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
144#ifdef PTHREAD_SCOPE_PROCESS 134#ifdef PTHREAD_SCOPE_PROCESS
145 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 135 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
146#endif 136#endif
147 137
148 sigfillset (&fullsigset); 138 sigfillset (&fullsigset);
149 139
150 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 140 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
151 retval = pthread_create (tid, &attr, proc, arg) == 0; 141 retval = pthread_create (tid, &attr, proc, arg) == 0;
152 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 142 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
153 143
144 pthread_attr_destroy (&attr);
145
154 return retval; 146 return retval;
155} 147}
156 148
157static int 149#define respipe_read(a,b,c) read ((a), (b), (c))
158create_pipe (int fd[2]) 150#define respipe_write(a,b,c) write ((a), (b), (c))
159{ 151#define respipe_close(a) close ((a))
160 if (pipe (fd)
161 || fcntl (fd [0], F_SETFL, O_NONBLOCK)
162 || fcntl (fd [1], F_SETFL, O_NONBLOCK))
163 return 0;
164
165 return 1;
166}
167 152
168#endif 153#endif
169 154
170#if __ia64
171# define STACKSIZE 65536
172#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
173# define STACKSIZE PTHREAD_STACK_MIN
174#else
175# define STACKSIZE 16384
176#endif 155#endif
177 156
178

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines