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 | |
|
|
16 | ///////////////////////////////////////////////////////////////////////////// |
|
|
17 | |
|
|
18 | #ifdef _WIN32 |
|
|
19 | |
|
|
20 | #define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls |
|
|
21 | #define _WIN32_WINNT 0x400 |
|
|
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 | #include <pthread.h> |
|
|
30 | #define sigset_t int |
|
|
31 | #define sigfillset(a) |
|
|
32 | #define pthread_sigmask(a,b,c) |
|
|
33 | #define sigaddset(a,b) |
|
|
34 | #define sigemptyset(s) |
|
|
35 | |
39 | typedef pthread_mutex_t mutex_t; |
36 | typedef pthread_mutex_t xmutex_t; |
40 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
41 | # define MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
37 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
38 | #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
|
|
39 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
40 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
41 | |
|
|
42 | typedef pthread_cond_t xcond_t; |
|
|
43 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
|
|
44 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
|
|
45 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
|
|
46 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
|
|
47 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
|
|
48 | |
|
|
49 | typedef pthread_t xthread_t; |
|
|
50 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
|
|
51 | #define X_THREAD_ATFORK(a,b,c) |
|
|
52 | |
|
|
53 | static int |
|
|
54 | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
|
|
55 | { |
|
|
56 | int retval; |
|
|
57 | pthread_attr_t attr; |
|
|
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; |
|
|
67 | } |
|
|
68 | |
|
|
69 | #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) |
|
|
70 | #define respipe_write(a,b,c) send ((a), (b), (c), 0) |
|
|
71 | #define respipe_close(a) PerlSock_closesocket ((a)) |
|
|
72 | |
42 | #else |
73 | #else |
43 | # define MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
74 | ///////////////////////////////////////////////////////////////////////////// |
|
|
75 | |
|
|
76 | #if __linux && !defined(_GNU_SOURCE) |
|
|
77 | # define _GNU_SOURCE |
44 | #endif |
78 | #endif |
45 | |
79 | |
46 | typedef pthread_cond_t cond_t; |
80 | /* just in case */ |
47 | #define COND_INIT PTHREAD_COND_INITIALIZER |
81 | #define _REENTRANT 1 |
48 | |
82 | |
49 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
83 | #if __solaris |
50 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
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 |
51 | |
90 | |
52 | #define COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
91 | #include <unistd.h> |
53 | #define COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
92 | #include <fcntl.h> |
54 | #define COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
93 | #include <signal.h> |
|
|
94 | #include <limits.h> |
|
|
95 | #include <pthread.h> |
55 | |
96 | |
56 | typedef pthread_t thread_t; |
97 | typedef pthread_mutex_t xmutex_t; |
|
|
98 | #if __linux && defined (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) |
|
|
107 | #else |
|
|
108 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
109 | # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) |
|
|
110 | #endif |
|
|
111 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
112 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
57 | |
113 | |
|
|
114 | typedef pthread_cond_t xcond_t; |
|
|
115 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
|
|
116 | #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) |
|
|
117 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
|
|
118 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
|
|
119 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
|
|
120 | |
|
|
121 | typedef pthread_t xthread_t; |
|
|
122 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
|
|
123 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
|
|
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 | |
|
|
134 | static int |
58 | static int thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
135 | xthread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
59 | { |
136 | { |
60 | int retval; |
137 | int retval; |
61 | sigset_t fullsigset, oldsigset; |
138 | sigset_t fullsigset, oldsigset; |
62 | pthread_attr_t attr; |
139 | pthread_attr_t attr; |
63 | |
140 | |
64 | pthread_attr_init (&attr); |
141 | pthread_attr_init (&attr); |
65 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
142 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
143 | pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); |
66 | #ifdef PTHREAD_SCOPE_PROCESS |
144 | #ifdef PTHREAD_SCOPE_PROCESS |
67 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
145 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
68 | #endif |
146 | #endif |
69 | |
147 | |
70 | sigfillset (&fullsigset); |
148 | sigfillset (&fullsigset); |
71 | |
149 | |
72 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
150 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
73 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
151 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
74 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
152 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
75 | |
153 | |
|
|
154 | pthread_attr_destroy (&attr); |
|
|
155 | |
76 | return retval; |
156 | return retval; |
77 | } |
157 | } |
78 | |
158 | |
79 | #define ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
159 | #define respipe_read(a,b,c) read ((a), (b), (c)) |
|
|
160 | #define respipe_write(a,b,c) write ((a), (b), (c)) |
|
|
161 | #define respipe_close(a) close ((a)) |
80 | |
162 | |
|
|
163 | #endif |
|
|
164 | |
|
|
165 | #endif |
|
|
166 | |