|
|
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 |
… | |
… | |
24 | #include <winsock2.h> |
27 | #include <winsock2.h> |
25 | #include <process.h> |
28 | #include <process.h> |
26 | #include <windows.h> |
29 | #include <windows.h> |
27 | #include <pthread.h> |
30 | #include <pthread.h> |
28 | #define sigset_t int |
31 | #define sigset_t int |
29 | #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) |
35 | #define sigfillset(s) |
34 | |
36 | |
35 | typedef pthread_mutex_t mutex_t; |
37 | typedef pthread_mutex_t xmutex_t; |
36 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
38 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
37 | #define X_MUTEX_CHECK(mutex) |
|
|
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 | |
41 | typedef pthread_cond_t cond_t; |
42 | typedef 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_SIGNAL(cond) pthread_cond_signal (&(cond)) |
44 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
45 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
45 | #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)) |
46 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
47 | |
47 | |
48 | typedef pthread_t thread_t; |
48 | typedef pthread_t xthread_t; |
49 | #define X_THREAD_PROC(name) void *name (void *thr_arg) |
49 | #define X_THREAD_PROC(name) void *name (void *thr_arg) |
50 | #define X_THREAD_ATFORK(a,b,c) |
50 | #define X_THREAD_ATFORK(a,b,c) |
51 | |
51 | |
52 | static int |
52 | static int |
53 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
53 | thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
54 | { |
54 | { |
|
|
55 | int retval; |
55 | pthread_attr_t attr; |
56 | pthread_attr_t attr; |
56 | |
57 | |
57 | pthread_attr_init (&attr); |
58 | pthread_attr_init (&attr); |
58 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
59 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
59 | |
60 | |
60 | return pthread_create (tid, &attr, proc, arg) == 0; |
61 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
|
|
62 | |
|
|
63 | pthread_attr_destroy (&attr); |
|
|
64 | |
|
|
65 | return retval; |
61 | } |
66 | } |
62 | |
67 | |
63 | #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) |
68 | #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) |
64 | #define respipe_write(a,b,c) send ((a), (b), (c), 0) |
69 | #define respipe_write(a,b,c) send ((a), (b), (c), 0) |
65 | #define respipe_close(a) PerlSock_closesocket ((a)) |
70 | #define respipe_close(a) PerlSock_closesocket ((a)) |
66 | |
71 | |
67 | #else |
72 | #else |
68 | ///////////////////////////////////////////////////////////////////////////// |
73 | ///////////////////////////////////////////////////////////////////////////// |
69 | |
74 | |
70 | /* solaris */ |
|
|
71 | #define _POSIX_PTHREAD_SEMANTICS 1 |
|
|
72 | |
|
|
73 | #if __linux && !defined(_GNU_SOURCE) |
75 | #if __linux && !defined(_GNU_SOURCE) |
74 | # define _GNU_SOURCE |
76 | # define _GNU_SOURCE |
75 | #endif |
77 | #endif |
76 | |
78 | |
77 | /* just in case */ |
79 | /* just in case */ |
78 | #define _REENTRANT 1 |
80 | #define _REENTRANT 1 |
79 | |
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 | |
80 | #include <unistd.h> |
90 | #include <unistd.h> |
81 | #include <fcntl.h> |
91 | #include <fcntl.h> |
82 | #include <signal.h> |
92 | #include <signal.h> |
|
|
93 | #include <limits.h> |
83 | #include <pthread.h> |
94 | #include <pthread.h> |
84 | |
95 | |
85 | typedef pthread_mutex_t mutex_t; |
96 | typedef pthread_mutex_t xmutex_t; |
86 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
97 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
87 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
98 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
88 | #else |
99 | #else |
89 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
100 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
90 | #endif |
101 | #endif |
91 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
102 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
92 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
103 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
93 | |
104 | |
94 | typedef pthread_cond_t cond_t; |
105 | typedef pthread_cond_t xcond_t; |
95 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
106 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
96 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
107 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
97 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
108 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
98 | #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)) |
99 | |
110 | |
100 | typedef pthread_t thread_t; |
111 | typedef pthread_t xthread_t; |
101 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
112 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
102 | #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) |
103 | |
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 | |
104 | static int |
124 | static int |
105 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
125 | thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) |
106 | { |
126 | { |
107 | int retval; |
127 | int retval; |
108 | sigset_t fullsigset, oldsigset; |
128 | sigset_t fullsigset, oldsigset; |
109 | pthread_attr_t attr; |
129 | pthread_attr_t attr; |
110 | |
130 | |
111 | pthread_attr_init (&attr); |
131 | pthread_attr_init (&attr); |
112 | 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); |
113 | #ifdef PTHREAD_SCOPE_PROCESS |
134 | #ifdef PTHREAD_SCOPE_PROCESS |
114 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
135 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
115 | #endif |
136 | #endif |
116 | |
137 | |
117 | sigfillset (&fullsigset); |
138 | sigfillset (&fullsigset); |
118 | |
139 | |
119 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
140 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
120 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
141 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
121 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
142 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
122 | |
143 | |
|
|
144 | pthread_attr_destroy (&attr); |
|
|
145 | |
123 | return retval; |
146 | return retval; |
124 | } |
147 | } |
125 | |
148 | |
126 | #define respipe_read(a,b,c) read ((a), (b), (c)) |
149 | #define respipe_read(a,b,c) read ((a), (b), (c)) |
127 | #define respipe_write(a,b,c) write ((a), (b), (c)) |
150 | #define respipe_write(a,b,c) write ((a), (b), (c)) |
128 | #define respipe_close(a) close ((a)) |
151 | #define respipe_close(a) close ((a)) |
129 | |
152 | |
130 | #endif |
153 | #endif |
131 | |
154 | |
|
|
155 | #endif |
132 | |
156 | |
133 | |
|
|