|
|
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 |
… | |
… | |
32 | #define sigemptyset(s) |
35 | #define sigemptyset(s) |
33 | #define sigfillset(s) |
36 | #define sigfillset(s) |
34 | |
37 | |
35 | typedef pthread_mutex_t mutex_t; |
38 | typedef pthread_mutex_t mutex_t; |
36 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
39 | #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
37 | #define X_MUTEX_CHECK(mutex) |
|
|
38 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
40 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
39 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
41 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
40 | |
42 | |
41 | typedef pthread_cond_t cond_t; |
43 | typedef pthread_cond_t cond_t; |
42 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
44 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
43 | #define X_COND_CHECK(cond) |
|
|
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 | |
48 | typedef pthread_t thread_t; |
49 | typedef pthread_t thread_t; |
… | |
… | |
50 | #define X_THREAD_ATFORK(a,b,c) |
51 | #define X_THREAD_ATFORK(a,b,c) |
51 | |
52 | |
52 | static int |
53 | static int |
53 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
54 | thread_create (thread_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> |
… | |
… | |
107 | |
111 | |
108 | typedef pthread_t thread_t; |
112 | typedef pthread_t thread_t; |
109 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
113 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
110 | #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) |
111 | |
115 | |
|
|
116 | // the broken bsd's once more |
|
|
117 | #ifndef PTHREAD_STACK_MIN |
|
|
118 | # define PTHREAD_STACK_MIN 0 |
|
|
119 | #endif |
|
|
120 | |
112 | static int |
121 | static int |
113 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
122 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
114 | { |
123 | { |
115 | int retval; |
124 | int retval; |
116 | sigset_t fullsigset, oldsigset; |
125 | sigset_t fullsigset, oldsigset; |
… | |
… | |
128 | |
137 | |
129 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
138 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
130 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
139 | retval = pthread_create (tid, &attr, proc, arg) == 0; |
131 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
140 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
132 | |
141 | |
|
|
142 | pthread_attr_destroy (&attr); |
|
|
143 | |
133 | return retval; |
144 | return retval; |
134 | } |
145 | } |
135 | |
146 | |
136 | #define respipe_read(a,b,c) read ((a), (b), (c)) |
147 | #define respipe_read(a,b,c) read ((a), (b), (c)) |
137 | #define respipe_write(a,b,c) write ((a), (b), (c)) |
148 | #define respipe_write(a,b,c) write ((a), (b), (c)) |
138 | #define respipe_close(a) close ((a)) |
149 | #define respipe_close(a) close ((a)) |
139 | |
150 | |
140 | #endif |
151 | #endif |
141 | |
152 | |
|
|
153 | #endif |
142 | |
154 | |
143 | |
|
|