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

Comparing BDB/xthread.h (file contents):
Revision 1.6 by root, Mon Apr 21 20:09:44 2008 UTC vs.
Revision 1.11 by root, Fri Jul 29 08:35:35 2011 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
19#define _WIN32_WINNT 0x400 21#define _WIN32_WINNT 0x400
20#include <stdio.h>//D 22#include <stdio.h>//D
21#include <fcntl.h> 23#include <fcntl.h>
28#define sigset_t int 30#define sigset_t int
29#define sigfillset(a) 31#define sigfillset(a)
30#define pthread_sigmask(a,b,c) 32#define pthread_sigmask(a,b,c)
31#define sigaddset(a,b) 33#define sigaddset(a,b)
32#define sigemptyset(s) 34#define sigemptyset(s)
33#define sigfillset(s)
34 35
35typedef pthread_mutex_t mutex_t; 36typedef pthread_mutex_t xmutex_t;
36#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 37#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
37#define X_MUTEX_CHECK(mutex) 38#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
38#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 39#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
39#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 40#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
40 41
41typedef pthread_cond_t cond_t; 42typedef pthread_cond_t xcond_t;
42#define X_COND_INIT PTHREAD_COND_INITIALIZER 43#define X_COND_INIT PTHREAD_COND_INITIALIZER
43#define X_COND_CHECK(cond) 44#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
44#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 45#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
45#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 46#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
46#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 47#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
47 48
48typedef pthread_t thread_t; 49typedef pthread_t xthread_t;
49#define X_THREAD_PROC(name) void *name (void *thr_arg) 50#define X_THREAD_PROC(name) void *name (void *thr_arg)
50#define X_THREAD_ATFORK(a,b,c) 51#define X_THREAD_ATFORK(a,b,c)
51 52
52static int 53static int
53thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 54thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
54{ 55{
56 int retval;
55 pthread_attr_t attr; 57 pthread_attr_t attr;
56 58
57 pthread_attr_init (&attr); 59 pthread_attr_init (&attr);
58 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 60 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
59 61
60 return pthread_create (tid, &attr, proc, arg) == 0; 62 retval = pthread_create (tid, &attr, proc, arg) == 0;
63
64 pthread_attr_destroy (&attr);
65
66 return retval;
61} 67}
62 68
63#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)
64#define respipe_write(a,b,c) send ((a), (b), (c), 0) 70#define respipe_write(a,b,c) send ((a), (b), (c), 0)
65#define respipe_close(a) PerlSock_closesocket ((a)) 71#define respipe_close(a) PerlSock_closesocket ((a))
66 72
67#else 73#else
68///////////////////////////////////////////////////////////////////////////// 74/////////////////////////////////////////////////////////////////////////////
69 75
70/* solaris */
71#define _POSIX_PTHREAD_SEMANTICS 1
72
73#if __linux && !defined(_GNU_SOURCE) 76#if __linux && !defined(_GNU_SOURCE)
74# define _GNU_SOURCE 77# define _GNU_SOURCE
75#endif 78#endif
76 79
77/* just in case */ 80/* just in case */
78#define _REENTRANT 1 81#define _REENTRANT 1
79 82
80#if __solaris 83#if __solaris
84# define _POSIX_PTHREAD_SEMANTICS 1
81/* try to bribe solaris headers into providing a current pthread API 85/* try to bribe solaris headers into providing a current pthread API
82 * despite perl being configured for an older version. 86 * despite environment being configured for an older version.
83 */ 87 */
84# define __EXTENSIONS__ 1 88# define __EXTENSIONS__ 1
85#endif 89#endif
86 90
87#include <unistd.h> 91#include <unistd.h>
88#include <fcntl.h> 92#include <fcntl.h>
89#include <signal.h> 93#include <signal.h>
90#include <limits.h> 94#include <limits.h>
91#include <pthread.h> 95#include <pthread.h>
92 96
93typedef pthread_mutex_t mutex_t; 97typedef pthread_mutex_t xmutex_t;
94#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 98#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
95# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 99# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
100# define X_MUTEX_CREATE(mutex) \
101 do { \
102 pthread_mutexattr_t attr; \
103 pthread_mutexattr_init (&attr); \
104 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
105 pthread_mutex_init (&(mutex), &attr); \
106 } while (0)
96#else 107#else
97# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 108# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
109# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
98#endif 110#endif
99#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) 111#define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
100#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 112#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
101 113
102typedef pthread_cond_t cond_t; 114typedef pthread_cond_t xcond_t;
103#define X_COND_INIT PTHREAD_COND_INITIALIZER 115#define X_COND_INIT PTHREAD_COND_INITIALIZER
116#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
104#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) 117#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
105#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) 118#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
106#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) 119#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
107 120
108typedef pthread_t thread_t; 121typedef pthread_t xthread_t;
109#define X_THREAD_PROC(name) static void *name (void *thr_arg) 122#define X_THREAD_PROC(name) static void *name (void *thr_arg)
110#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) 123#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
111 124
125// the broken bsd's once more
126#ifndef PTHREAD_STACK_MIN
127# define PTHREAD_STACK_MIN 0
128#endif
129
130#ifndef X_STACKSIZE
131# define X_STACKSIZE sizeof (void *) * 4096
132#endif
133
112static int 134static int
113thread_create (thread_t *tid, void *(*proc)(void *), void *arg) 135thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
114{ 136{
115 int retval; 137 int retval;
116 sigset_t fullsigset, oldsigset; 138 sigset_t fullsigset, oldsigset;
117 pthread_attr_t attr; 139 pthread_attr_t attr;
118 140
119 pthread_attr_init (&attr); 141 pthread_attr_init (&attr);
120 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 142 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
121 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 143 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
122 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
123#ifdef PTHREAD_SCOPE_PROCESS 144#ifdef PTHREAD_SCOPE_PROCESS
124 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 145 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
125#endif 146#endif
126 147
127 sigfillset (&fullsigset); 148 sigfillset (&fullsigset);
128 149
129 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); 150 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
130 retval = pthread_create (tid, &attr, proc, arg) == 0; 151 retval = pthread_create (tid, &attr, proc, arg) == 0;
131 pthread_sigmask (SIG_SETMASK, &oldsigset, 0); 152 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
132 153
154 pthread_attr_destroy (&attr);
155
133 return retval; 156 return retval;
134} 157}
135 158
136#define respipe_read(a,b,c) read ((a), (b), (c)) 159#define respipe_read(a,b,c) read ((a), (b), (c))
137#define respipe_write(a,b,c) write ((a), (b), (c)) 160#define respipe_write(a,b,c) write ((a), (b), (c))
138#define respipe_close(a) close ((a)) 161#define respipe_close(a) close ((a))
139 162
140#endif 163#endif
141 164
165#endif
142 166
143

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines