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.7 by root, Tue May 20 05:51:34 2008 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) 32#define sigfillset(a)
28#define pthread_sigmask(a,b,c) 33#define pthread_sigmask(a,b,c)
29#define sigaddset(a,b) 34#define sigaddset(a,b)
30#define sigemptyset(s) 35#define sigemptyset(s)
31#define sigfillset(s) 36#define sigfillset(s)
32 37
33#define pthread_kill(a,b) 38typedef pthread_mutex_t mutex_t;
34#define pthread_self() 0 39#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
40#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
41#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
35 42
36typedef HANDLE mutex_t; 43typedef pthread_cond_t cond_t;
37#define X_MUTEX_INIT 0 44#define X_COND_INIT PTHREAD_COND_INITIALIZER
38#define X_MUTEX_CHECK(mutex) if (!(mutex)) (mutex) = CreateMutex (NULL, FALSE, NULL) 45#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
39#define X_LOCK(mutex) WaitForSingleObject ((mutex), INFINITE) 46#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
40#define X_UNLOCK(mutex) ReleaseMutex (mutex) 47#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
41 48
42typedef HANDLE cond_t; 49typedef pthread_t thread_t;
43#define X_COND_INIT 0 50#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) 51#define X_THREAD_ATFORK(a,b,c)
71 52
72static int 53static int
73thread_create (thread_t *tid, LPTHREAD_START_ROUTINE proc, void *arg) 54thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
74{ 55{
75 *tid = 0; 56 int retval;
76 CreateThread (0, 4096, proc, arg, 0, tid); 57 pthread_attr_t attr;
77 return !!*tid; 58
59 pthread_attr_init (&attr);
60 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
61
62 retval = pthread_create (tid, &attr, proc, arg) == 0;
63
64 pthread_attr_destroy (&attr);
65
66 return retval;
78} 67}
79 68
80#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) 69#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
81#define respipe_write(a,b,c) send ((a), (b), (c), 0) 70#define respipe_write(a,b,c) send ((a), (b), (c), 0)
82#define respipe_close(a) PerlSock_closesocket ((a)) 71#define respipe_close(a) PerlSock_closesocket ((a))
83 72
84#else 73#else
85///////////////////////////////////////////////////////////////////////////// 74/////////////////////////////////////////////////////////////////////////////
86 75
87/* solaris */
88#define _POSIX_PTHREAD_SEMANTICS 1
89
90#if __linux && !defined(_GNU_SOURCE) 76#if __linux && !defined(_GNU_SOURCE)
91# define _GNU_SOURCE 77# define _GNU_SOURCE
92#endif 78#endif
93 79
94/* just in case */ 80/* just in case */
95#define _REENTRANT 1 81#define _REENTRANT 1
96 82
83#if __solaris
84# define _POSIX_PTHREAD_SEMANTICS 1
85/* try to bribe solaris headers into providing a current pthread API
86 * despite environment being configured for an older version.
87 */
88# define __EXTENSIONS__ 1
89#endif
90
97#include <unistd.h> 91#include <unistd.h>
98#include <fcntl.h> 92#include <fcntl.h>
99#include <signal.h> 93#include <signal.h>
94#include <limits.h>
100#include <pthread.h> 95#include <pthread.h>
101 96
102typedef pthread_mutex_t mutex_t; 97typedef pthread_mutex_t mutex_t;
103#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 98#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
104# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 99# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
116 111
117typedef pthread_t thread_t; 112typedef pthread_t thread_t;
118#define X_THREAD_PROC(name) static void *name (void *thr_arg) 113#define X_THREAD_PROC(name) static void *name (void *thr_arg)
119#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 114#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
120 115
116// the broken bsd's once more
117#ifndef PTHREAD_STACK_MIN
118# define PTHREAD_STACK_MIN 0
119#endif
120
121static int 121static int
122thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 122thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
123{ 123{
124 int retval; 124 int retval;
125 sigset_t fullsigset, oldsigset; 125 sigset_t fullsigset, oldsigset;
126 pthread_attr_t attr; 126 pthread_attr_t attr;
127 127
128 pthread_attr_init (&attr); 128 pthread_attr_init (&attr);
129 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 129 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
130 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096
131 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
130#ifdef PTHREAD_SCOPE_PROCESS 132#ifdef PTHREAD_SCOPE_PROCESS
131 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 133 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
132#endif 134#endif
133 135
134 sigfillset (&fullsigset); 136 sigfillset (&fullsigset);
135 137
136 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 138 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
137 retval = pthread_create (tid, &attr, proc, arg) == 0; 139 retval = pthread_create (tid, &attr, proc, arg) == 0;
138 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 140 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
139 141
142 pthread_attr_destroy (&attr);
143
140 return retval; 144 return retval;
141} 145}
142 146
143#define respipe_read(a,b,c) read ((a), (b), (c)) 147#define respipe_read(a,b,c) read ((a), (b), (c))
144#define respipe_write(a,b,c) write ((a), (b), (c)) 148#define respipe_write(a,b,c) write ((a), (b), (c))
145#define respipe_close(a) close ((a)) 149#define respipe_close(a) close ((a))
146 150
147#endif 151#endif
148 152
153#endif
149 154
150

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines