ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/xthread.h
Revision: 1.13
Committed: Wed Dec 18 02:52:34 2013 UTC (10 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +8 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.7 #ifndef XTHREAD_H_
2     #define XTHREAD_H_
3    
4     /* whether word reads are potentially non-atomic.
5 root 1.11 * this is conservative, likely most arches this runs
6 root 1.2 * on have atomic word read/writes.
7     */
8     #ifndef WORDACCESS_UNSAFE
9     # if __i386 || __x86_64
10     # define WORDACCESS_UNSAFE 0
11     # else
12     # define WORDACCESS_UNSAFE 1
13     # endif
14     #endif
15    
16     /////////////////////////////////////////////////////////////////////////////
17    
18     #ifdef _WIN32
19    
20 root 1.13 //#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 root 1.2 #include <stdio.h>//D
23     #include <fcntl.h>
24     #include <io.h>
25 root 1.4 #include <time.h>
26 root 1.2 #include <winsock2.h>
27     #include <process.h>
28     #include <windows.h>
29 root 1.13
30     /* work around some bugs in ptw32 */
31     #if defined(__MINGW32__) && defined(_TIMESPEC_DEFINED)
32     #define HAVE_STRUCT_TIMESPEC 1
33     #endif
34    
35 root 1.4 #include <pthread.h>
36 root 1.2 #define sigset_t int
37 root 1.11 #define sigfillset(a)
38 root 1.2 #define pthread_sigmask(a,b,c)
39     #define sigaddset(a,b)
40     #define sigemptyset(s)
41    
42 root 1.9 typedef pthread_mutex_t xmutex_t;
43 root 1.4 #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44 root 1.11 #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
45 root 1.4 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
46     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
47 root 1.2
48 root 1.9 typedef pthread_cond_t xcond_t;
49 root 1.4 #define X_COND_INIT PTHREAD_COND_INITIALIZER
50 root 1.11 #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
51 root 1.4 #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
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))
54    
55 root 1.9 typedef pthread_t xthread_t;
56 root 1.12 #define X_THREAD_PROC(name) static void *name (void *thr_arg)
57 root 1.4 #define X_THREAD_ATFORK(a,b,c)
58 root 1.2
59     static int
60 root 1.12 xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
61 root 1.2 {
62 root 1.7 int retval;
63 root 1.4 pthread_attr_t attr;
64 root 1.2
65 root 1.4 pthread_attr_init (&attr);
66     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
67 root 1.2
68 root 1.7 retval = pthread_create (tid, &attr, proc, arg) == 0;
69    
70     pthread_attr_destroy (&attr);
71    
72     return retval;
73 root 1.2 }
74    
75 root 1.3 #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 root 1.2
79     #else
80     /////////////////////////////////////////////////////////////////////////////
81    
82 root 1.1 #if __linux && !defined(_GNU_SOURCE)
83     # define _GNU_SOURCE
84     #endif
85    
86     /* just in case */
87     #define _REENTRANT 1
88    
89 root 1.6 #if __solaris
90 root 1.7 # define _POSIX_PTHREAD_SEMANTICS 1
91 root 1.6 /* try to bribe solaris headers into providing a current pthread API
92 root 1.7 * despite environment being configured for an older version.
93 root 1.6 */
94     # define __EXTENSIONS__ 1
95     #endif
96    
97 root 1.2 #include <unistd.h>
98     #include <fcntl.h>
99 root 1.1 #include <signal.h>
100 root 1.5 #include <limits.h>
101 root 1.1 #include <pthread.h>
102    
103 root 1.9 typedef pthread_mutex_t xmutex_t;
104 root 1.1 #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
105 root 1.11 # 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 root 1.1 #else
114 root 1.11 # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
115     # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
116 root 1.1 #endif
117 root 1.11 #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
118     #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
119 root 1.1
120 root 1.9 typedef pthread_cond_t xcond_t;
121 root 1.11 #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 root 1.1
127 root 1.9 typedef pthread_t xthread_t;
128 root 1.2 #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 root 1.1
131 root 1.7 // the broken bsd's once more
132     #ifndef PTHREAD_STACK_MIN
133     # define PTHREAD_STACK_MIN 0
134     #endif
135    
136 root 1.8 #ifndef X_STACKSIZE
137 root 1.11 # define X_STACKSIZE sizeof (void *) * 4096
138 root 1.8 #endif
139    
140 root 1.2 static int
141 root 1.12 xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
142 root 1.1 {
143     int retval;
144     sigset_t fullsigset, oldsigset;
145     pthread_attr_t attr;
146    
147     pthread_attr_init (&attr);
148     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
149 root 1.8 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
150 root 1.1 #ifdef PTHREAD_SCOPE_PROCESS
151     pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
152     #endif
153    
154     sigfillset (&fullsigset);
155    
156     pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
157     retval = pthread_create (tid, &attr, proc, arg) == 0;
158     pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
159    
160 root 1.7 pthread_attr_destroy (&attr);
161    
162 root 1.1 return retval;
163     }
164    
165 root 1.3 #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))
168 root 1.2
169     #endif
170    
171 root 1.7 #endif
172 root 1.1