1 | /* solaris */ |
|
|
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
|
|
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. |
1 | /* wether word reads are potentially non-atomic. |
28 | * this is conservatice, likely most arches this runs |
2 | * this is conservatice, likely most arches this runs |
29 | * on have atomic word read/writes. |
3 | * on have atomic word read/writes. |
30 | */ |
4 | */ |
31 | #ifndef WORDACCESS_UNSAFE |
5 | #ifndef WORDACCESS_UNSAFE |
… | |
… | |
34 | # else |
8 | # else |
35 | # define WORDACCESS_UNSAFE 1 |
9 | # define WORDACCESS_UNSAFE 1 |
36 | # endif |
10 | # endif |
37 | #endif |
11 | #endif |
38 | |
12 | |
|
|
13 | ///////////////////////////////////////////////////////////////////////////// |
|
|
14 | |
|
|
15 | #ifdef _WIN32 |
|
|
16 | typedef int ssize_t; |
|
|
17 | |
|
|
18 | #define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls |
|
|
19 | #define _WIN32_WINNT 0x400 |
|
|
20 | #include <stdio.h>//D |
|
|
21 | #include <fcntl.h> |
|
|
22 | #include <io.h> |
|
|
23 | #include <winsock2.h> |
|
|
24 | #include <process.h> |
|
|
25 | #include <windows.h> |
|
|
26 | #define sigset_t int |
|
|
27 | #define sigfillset(a) |
|
|
28 | #define pthread_sigmask(a,b,c) |
|
|
29 | #define sigaddset(a,b) |
|
|
30 | #define sigemptyset(s) |
|
|
31 | #define sigfillset(s) |
|
|
32 | |
|
|
33 | #define pthread_kill(a,b) |
|
|
34 | #define pthread_self() 0 |
|
|
35 | |
|
|
36 | typedef HANDLE mutex_t; |
|
|
37 | #define X_MUTEX_INIT 0 |
|
|
38 | #define X_MUTEX_CHECK(mutex) if (!(mutex)) (mutex) = CreateMutex (NULL, FALSE, NULL) |
|
|
39 | #define X_LOCK(mutex) WaitForSingleObject ((mutex), INFINITE) |
|
|
40 | #define X_UNLOCK(mutex) ReleaseMutex (mutex) |
|
|
41 | |
|
|
42 | typedef HANDLE cond_t; |
|
|
43 | #define X_COND_INIT 0 |
|
|
44 | #define X_COND_CHECK(cond) if (!(cond)) (cond) = CreateEvent (NULL, FALSE, FALSE, NULL) |
|
|
45 | #define X_COND_SIGNAL(cond) SetEvent (cond) |
|
|
46 | #define X_COND_WAIT(cond,mutex) do { SignalObjectAndWait ((mutex), (cond),INFINITE, FALSE); WaitForSingleObject ((mutex), INFINITE); } while (0) |
|
|
47 | |
|
|
48 | #define ETIMEDOUT 1 |
|
|
49 | |
|
|
50 | struct timespec { |
|
|
51 | unsigned long tv_sec, tv_nsec; |
|
|
52 | }; |
|
|
53 | |
|
|
54 | static int |
|
|
55 | X_COND_TIMEDWAIT (mutex_t mutex, cond_t cond, struct timespec to) |
|
|
56 | { |
|
|
57 | unsigned long ms = to.tv_nsec / 1000 + to.tv_sec * 1000; |
|
|
58 | |
|
|
59 | if (SignalObjectAndWait (mutex, cond, ms, FALSE) == WAIT_TIMEOUT) |
|
|
60 | return ETIMEDOUT; |
|
|
61 | |
|
|
62 | if (WaitForSingleObject (mutex, ms) == WAIT_TIMEOUT) |
|
|
63 | return ETIMEDOUT; |
|
|
64 | |
|
|
65 | return 0; |
|
|
66 | } |
|
|
67 | |
|
|
68 | typedef DWORD thread_t; |
|
|
69 | #define X_THREAD_PROC(name) DWORD WINAPI name (LPVOID thr_arg) |
|
|
70 | #define X_THREAD_ATFORK(a,b,c) |
|
|
71 | |
|
|
72 | static int |
|
|
73 | thread_create (thread_t *tid, LPTHREAD_START_ROUTINE proc, void *arg) |
|
|
74 | { |
|
|
75 | *tid = 0; |
|
|
76 | CreateThread (0, 4096, proc, arg, 0, tid); |
|
|
77 | return !!*tid; |
|
|
78 | } |
|
|
79 | |
|
|
80 | int Perl_my_socketpair (int family, int type, int protocol, int fd[2]); |
|
|
81 | |
|
|
82 | static int |
|
|
83 | create_pipe (int fd[2]) |
|
|
84 | { |
|
|
85 | int arg = 1; |
|
|
86 | Perl_my_socketpair (AF_UNIX, SOCK_STREAM, 0, fd); |
|
|
87 | ioctlsocket (fd [0], FIONBIO, &arg); |
|
|
88 | ioctlsocket (fd [1], FIONBIO, &arg); |
|
|
89 | |
|
|
90 | return 1; |
|
|
91 | } |
|
|
92 | |
|
|
93 | #else |
|
|
94 | ///////////////////////////////////////////////////////////////////////////// |
|
|
95 | |
|
|
96 | /* solaris */ |
|
|
97 | #define _POSIX_PTHREAD_SEMANTICS 1 |
|
|
98 | |
|
|
99 | #if __linux && !defined(_GNU_SOURCE) |
|
|
100 | # define _GNU_SOURCE |
|
|
101 | #endif |
|
|
102 | |
|
|
103 | /* just in case */ |
|
|
104 | #define _REENTRANT 1 |
|
|
105 | |
|
|
106 | #include <unistd.h> |
|
|
107 | #include <fcntl.h> |
|
|
108 | #include <signal.h> |
|
|
109 | #include <pthread.h> |
|
|
110 | |
|
|
111 | #ifndef PTHREAD_STACK_MIN |
|
|
112 | /* care for broken platforms, e.g. windows */ |
|
|
113 | # define PTHREAD_STACK_MIN 16384 |
|
|
114 | #endif |
|
|
115 | |
39 | typedef pthread_mutex_t mutex_t; |
116 | typedef pthread_mutex_t mutex_t; |
40 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
117 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
41 | # define MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
118 | # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
42 | #else |
119 | #else |
43 | # define MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
120 | # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
44 | #endif |
121 | #endif |
|
|
122 | #define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
123 | #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
45 | |
124 | |
46 | typedef pthread_cond_t cond_t; |
125 | typedef pthread_cond_t cond_t; |
47 | #define COND_INIT PTHREAD_COND_INITIALIZER |
126 | #define X_COND_INIT PTHREAD_COND_INITIALIZER |
48 | |
|
|
49 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
50 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
51 | |
|
|
52 | #define COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
127 | #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) |
53 | #define COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
128 | #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) |
54 | #define COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
129 | #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) |
55 | |
130 | |
56 | typedef pthread_t thread_t; |
131 | typedef pthread_t thread_t; |
|
|
132 | #define X_THREAD_PROC(name) static void *name (void *thr_arg) |
|
|
133 | #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
57 | |
134 | |
|
|
135 | static int |
58 | static int thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
136 | thread_create (thread_t *tid, void *(*proc)(void *), void *arg) |
59 | { |
137 | { |
60 | int retval; |
138 | int retval; |
61 | sigset_t fullsigset, oldsigset; |
139 | sigset_t fullsigset, oldsigset; |
62 | pthread_attr_t attr; |
140 | pthread_attr_t attr; |
63 | |
141 | |
… | |
… | |
74 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
152 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
75 | |
153 | |
76 | return retval; |
154 | return retval; |
77 | } |
155 | } |
78 | |
156 | |
79 | #define ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) |
157 | static int |
|
|
158 | create_pipe (int fd[2]) |
|
|
159 | { |
|
|
160 | if (pipe (fd) |
|
|
161 | || fcntl (fd [0], F_SETFL, O_NONBLOCK) |
|
|
162 | || fcntl (fd [1], F_SETFL, O_NONBLOCK)) |
|
|
163 | return 0; |
80 | |
164 | |
|
|
165 | return 1; |
|
|
166 | } |
|
|
167 | |
|
|
168 | #endif |
|
|
169 | |
|
|
170 | #if __ia64 |
|
|
171 | # define STACKSIZE 65536 |
|
|
172 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
173 | # define STACKSIZE PTHREAD_STACK_MIN |
|
|
174 | #else |
|
|
175 | # define STACKSIZE 16384 |
|
|
176 | #endif |
|
|
177 | |
|
|
178 | |