|
|
1 | /* most win32 perls are beyond fixing, requiring dTHX */ |
|
|
2 | /* even for ISO-C functions such as malloc. avoid! avoid! avoid! */ |
|
|
3 | /* and fail to define numerous symbols, but still overrwide them */ |
|
|
4 | /* with non-working versions (e.g. setjmp). */ |
|
|
5 | #ifdef _WIN32 |
|
|
6 | /*# define PERL_CORE 1 fixes some, breaks others */ |
|
|
7 | #else |
1 | #define PERL_NO_GET_CONTEXT |
8 | # define PERL_NO_GET_CONTEXT |
|
|
9 | #endif |
2 | |
10 | |
3 | #include "EXTERN.h" |
11 | #include "EXTERN.h" |
4 | #include "perl.h" |
12 | #include "perl.h" |
5 | #include "XSUB.h" |
13 | #include "XSUB.h" |
6 | |
14 | |
… | |
… | |
10 | #include "perlmulticore.h" |
18 | #include "perlmulticore.h" |
11 | #include "schmorp.h" |
19 | #include "schmorp.h" |
12 | #include "xthread.h" |
20 | #include "xthread.h" |
13 | |
21 | |
14 | #ifdef _WIN32 |
22 | #ifdef _WIN32 |
15 | typedef char sigset_t; |
23 | #ifndef sigset_t |
16 | #define pthread_sigmask(mode,new,old) |
24 | #define sigset_t int |
17 | #endif |
25 | #endif |
|
|
26 | #endif |
18 | |
27 | |
19 | static pthread_key_t current_key; |
28 | #ifndef SvREFCNT_dec_NN |
|
|
29 | #define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
30 | #endif |
|
|
31 | |
|
|
32 | #ifndef SvREFCNT_inc_NN |
|
|
33 | #define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
34 | #endif |
|
|
35 | |
|
|
36 | #define RECURSION_CHECK 0 |
|
|
37 | |
|
|
38 | static X_TLS_DECLARE(current_key); |
|
|
39 | #if RECURSION_CHECK |
|
|
40 | static X_TLS_DECLARE(check_key); |
|
|
41 | #endif |
|
|
42 | |
20 | |
43 | |
21 | static s_epipe ep; |
44 | static s_epipe ep; |
22 | static void *perl_thx; |
45 | static void *perl_thx; |
23 | static sigset_t cursigset, fullsigset; |
46 | static sigset_t cursigset, fullsigset; |
24 | |
47 | |
… | |
… | |
28 | /* assigned to a thread for each release/acquire */ |
51 | /* assigned to a thread for each release/acquire */ |
29 | struct tctx |
52 | struct tctx |
30 | { |
53 | { |
31 | void *coro; |
54 | void *coro; |
32 | int wait_f; |
55 | int wait_f; |
33 | xcond_t wait_c; |
56 | xcond_t acquire_c; |
|
|
57 | int jeret; |
34 | }; |
58 | }; |
35 | |
59 | |
36 | static struct tctx *tctx_free; |
60 | static struct tctx *tctx_free; |
37 | |
61 | |
|
|
62 | static struct tctx * |
|
|
63 | tctx_get (void) |
|
|
64 | { |
|
|
65 | struct tctx *ctx; |
|
|
66 | |
|
|
67 | if (!tctx_free) |
|
|
68 | { |
|
|
69 | ctx = malloc (sizeof (*tctx_free)); |
|
|
70 | X_COND_CREATE (ctx->acquire_c); |
|
|
71 | } |
|
|
72 | else |
|
|
73 | { |
|
|
74 | ctx = tctx_free; |
|
|
75 | tctx_free = tctx_free->coro; |
|
|
76 | } |
|
|
77 | |
|
|
78 | return ctx; |
|
|
79 | } |
|
|
80 | |
|
|
81 | static void |
|
|
82 | tctx_put (struct tctx *ctx) |
|
|
83 | { |
|
|
84 | ctx->coro = tctx_free; |
|
|
85 | tctx_free = ctx; |
|
|
86 | } |
|
|
87 | |
|
|
88 | /* a stack of tctxs */ |
|
|
89 | struct tctxs |
|
|
90 | { |
|
|
91 | struct tctx **ctxs; |
|
|
92 | int cur, max; |
|
|
93 | }; |
|
|
94 | |
|
|
95 | static struct tctx * |
|
|
96 | tctxs_get (struct tctxs *ctxs) |
|
|
97 | { |
|
|
98 | return ctxs->ctxs[--ctxs->cur]; |
|
|
99 | } |
|
|
100 | |
|
|
101 | static void |
|
|
102 | tctxs_put (struct tctxs *ctxs, struct tctx *ctx) |
|
|
103 | { |
|
|
104 | if (ctxs->cur >= ctxs->max) |
|
|
105 | { |
|
|
106 | ctxs->max = ctxs->max ? ctxs->max * 2 : 16; |
|
|
107 | ctxs->ctxs = realloc (ctxs->ctxs, ctxs->max * sizeof (ctxs->ctxs[0])); |
|
|
108 | } |
|
|
109 | |
|
|
110 | ctxs->ctxs[ctxs->cur++] = ctx; |
|
|
111 | } |
|
|
112 | |
|
|
113 | static xmutex_t release_m = X_MUTEX_INIT; |
|
|
114 | static xcond_t release_c = X_COND_INIT; |
|
|
115 | static struct tctxs releasers; |
38 | static int idle; |
116 | static int idle; |
39 | static int min_idle = 1; |
117 | static int min_idle = 1; |
|
|
118 | static int curthreads, max_threads = 1; /* protected by release_m */ |
40 | |
119 | |
41 | static xmutex_t perl_m = X_MUTEX_INIT; |
|
|
42 | static xcond_t perl_c = X_COND_INIT; |
|
|
43 | static struct tctx *perl_f; |
|
|
44 | |
|
|
45 | static xmutex_t wait_m = X_MUTEX_INIT; |
120 | static xmutex_t acquire_m = X_MUTEX_INIT; |
46 | |
|
|
47 | static int wakeup_f; |
|
|
48 | static struct tctx **waiters; |
121 | static struct tctxs acquirers; |
49 | static int waiters_count, waiters_max; |
|
|
50 | |
|
|
51 | static struct tctx * |
|
|
52 | tctx_get (void) |
|
|
53 | { |
|
|
54 | struct tctx *ctx; |
|
|
55 | |
|
|
56 | if (!tctx_free) |
|
|
57 | { |
|
|
58 | ctx = malloc (sizeof (*tctx_free)); |
|
|
59 | X_COND_CREATE (ctx->wait_c); |
|
|
60 | } |
|
|
61 | else |
|
|
62 | { |
|
|
63 | ctx = tctx_free; |
|
|
64 | tctx_free = tctx_free->coro; |
|
|
65 | } |
|
|
66 | |
|
|
67 | return ctx; |
|
|
68 | } |
|
|
69 | |
|
|
70 | static void |
|
|
71 | tctx_put (struct tctx *ctx) |
|
|
72 | { |
|
|
73 | ctx->coro = tctx_free; |
|
|
74 | tctx_free = ctx; |
|
|
75 | } |
|
|
76 | |
122 | |
77 | X_THREAD_PROC(thread_proc) |
123 | X_THREAD_PROC(thread_proc) |
78 | { |
124 | { |
79 | PERL_SET_CONTEXT (perl_thx); |
125 | PERL_SET_CONTEXT (perl_thx); |
80 | |
126 | |
81 | { |
127 | { |
82 | dTHX; /* inefficient, we already have perl_thx, but I see no better way */ |
128 | dTHXa (perl_thx); |
|
|
129 | dJMPENV; |
83 | struct tctx *ctx; |
130 | struct tctx *ctx; |
|
|
131 | int catchret; |
84 | |
132 | |
85 | X_LOCK (perl_m); |
133 | X_LOCK (release_m); |
86 | |
134 | |
87 | for (;;) |
135 | for (;;) |
88 | { |
136 | { |
89 | while (!perl_f) |
137 | while (!releasers.cur) |
90 | if (idle <= min_idle || 1) |
138 | if (idle <= min_idle || 1) |
91 | X_COND_WAIT (perl_c, perl_m); |
139 | X_COND_WAIT (release_c, release_m); |
92 | else |
140 | else |
93 | { |
141 | { |
94 | struct timespec ts = { time (0) + idle - min_idle, 0 }; |
142 | struct timespec ts = { time (0) + idle - min_idle, 0 }; |
95 | |
143 | |
96 | if (X_COND_TIMEDWAIT (perl_c, perl_m, ts) == ETIMEDOUT) |
144 | if (X_COND_TIMEDWAIT (release_c, release_m, ts) == ETIMEDOUT) |
97 | if (idle > min_idle && !perl_f) |
145 | if (idle > min_idle && !releasers.cur) |
98 | break; |
146 | break; |
99 | } |
147 | } |
100 | |
148 | |
101 | ctx = perl_f; |
149 | ctx = tctxs_get (&releasers); |
102 | perl_f = 0; |
|
|
103 | --idle; |
150 | --idle; |
104 | X_UNLOCK (perl_m); |
151 | X_UNLOCK (release_m); |
105 | |
152 | |
106 | if (!ctx) /* timed out? */ |
153 | if (!ctx) /* timed out? */ |
107 | break; |
154 | break; |
108 | |
155 | |
109 | pthread_sigmask (SIG_SETMASK, &cursigset, 0); |
156 | pthread_sigmask (SIG_SETMASK, &cursigset, 0); |
|
|
157 | JMPENV_PUSH (ctx->jeret); |
110 | |
158 | |
|
|
159 | if (!ctx->jeret) |
111 | while (ctx->coro) |
160 | while (ctx->coro) |
112 | CORO_SCHEDULE; |
161 | CORO_SCHEDULE; |
113 | |
162 | |
|
|
163 | JMPENV_POP; |
114 | pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset); |
164 | pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset); |
115 | |
165 | |
116 | X_LOCK (wait_m); |
166 | X_LOCK (acquire_m); |
117 | ctx->wait_f = 1; |
167 | ctx->wait_f = 1; |
118 | X_COND_SIGNAL (ctx->wait_c); |
168 | X_COND_SIGNAL (ctx->acquire_c); |
119 | X_UNLOCK (wait_m); |
169 | X_UNLOCK (acquire_m); |
120 | |
170 | |
121 | X_LOCK (perl_m); |
171 | X_LOCK (release_m); |
122 | ++idle; |
172 | ++idle; |
123 | } |
173 | } |
124 | } |
174 | } |
125 | } |
175 | } |
126 | |
176 | |
127 | static void |
177 | static void |
128 | start_thread (void) |
178 | start_thread (void) |
129 | { |
179 | { |
130 | xthread_t tid; |
180 | xthread_t tid; |
131 | |
181 | |
|
|
182 | if (curthreads >= max_threads && 0) |
|
|
183 | return; |
|
|
184 | |
|
|
185 | ++curthreads; |
132 | ++idle; |
186 | ++idle; |
133 | xthread_create (&tid, thread_proc, 0); |
187 | xthread_create (&tid, thread_proc, 0); |
134 | } |
188 | } |
135 | |
189 | |
136 | static void |
190 | static void |
137 | pmapi_release (void) |
191 | pmapi_release (void) |
138 | { |
192 | { |
|
|
193 | #if RECURSION_CHECK |
|
|
194 | if (X_TLS_GET (check_key)) |
|
|
195 | croak ("perlinterp_release () called without valid perl context"); |
|
|
196 | |
|
|
197 | X_TLS_SET (check_key, &check_key); |
|
|
198 | #endif |
|
|
199 | |
139 | if (!(thread_enable ? thread_enable & 1 : global_enable)) |
200 | if (! ((thread_enable ? thread_enable : global_enable) & 1)) |
140 | { |
201 | { |
141 | pthread_setspecific (current_key, 0); |
202 | X_TLS_SET (current_key, 0); |
142 | return; |
203 | return; |
143 | } |
204 | } |
144 | |
205 | |
145 | struct tctx *ctx = tctx_get (); |
206 | struct tctx *ctx = tctx_get (); |
146 | ctx->coro = SvREFCNT_inc_NN (CORO_CURRENT); |
207 | ctx->coro = SvREFCNT_inc_simple_NN (CORO_CURRENT); |
147 | ctx->wait_f = 0; |
208 | ctx->wait_f = 0; |
148 | |
209 | |
149 | pthread_setspecific (current_key, ctx); |
210 | X_TLS_SET (current_key, ctx); |
150 | pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset); |
211 | pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset); |
151 | |
212 | |
152 | X_LOCK (perl_m); |
213 | X_LOCK (release_m); |
153 | |
214 | |
154 | if (idle <= min_idle) |
215 | if (idle <= min_idle) |
155 | start_thread (); |
216 | start_thread (); |
156 | |
217 | |
157 | perl_f = ctx; |
218 | tctxs_put (&releasers, ctx); |
158 | X_COND_SIGNAL (perl_c); |
219 | X_COND_SIGNAL (release_c); |
159 | |
220 | |
|
|
221 | while (!idle && releasers.cur) |
|
|
222 | { |
|
|
223 | X_UNLOCK (release_m); |
|
|
224 | X_LOCK (release_m); |
|
|
225 | } |
|
|
226 | |
160 | X_UNLOCK (perl_m); |
227 | X_UNLOCK (release_m); |
161 | } |
228 | } |
162 | |
229 | |
163 | static void |
230 | static void |
164 | pmapi_acquire (void) |
231 | pmapi_acquire (void) |
165 | { |
232 | { |
166 | struct tctx *ctx = pthread_getspecific (current_key); |
233 | int jeret; |
|
|
234 | struct tctx *ctx = X_TLS_GET (current_key); |
|
|
235 | |
|
|
236 | #if RECURSION_CHECK |
|
|
237 | if (X_TLS_GET (check_key) != &check_key) |
|
|
238 | croak ("perlinterp_acquire () called with valid perl context"); |
|
|
239 | |
|
|
240 | X_TLS_SET (check_key, 0); |
|
|
241 | #endif |
167 | |
242 | |
168 | if (!ctx) |
243 | if (!ctx) |
169 | return; |
244 | return; |
170 | |
245 | |
171 | X_LOCK (wait_m); |
246 | X_LOCK (acquire_m); |
172 | |
247 | |
173 | if (waiters_count >= waiters_max) |
248 | tctxs_put (&acquirers, ctx); |
174 | { |
|
|
175 | waiters_max = waiters_max ? waiters_max * 2 : 16; |
|
|
176 | waiters = realloc (waiters, waiters_max * sizeof (*waiters)); |
|
|
177 | } |
|
|
178 | |
|
|
179 | waiters [waiters_count++] = ctx; |
|
|
180 | |
249 | |
181 | s_epipe_signal (&ep); |
250 | s_epipe_signal (&ep); |
182 | while (!ctx->wait_f) |
251 | while (!ctx->wait_f) |
183 | X_COND_WAIT (ctx->wait_c, wait_m); |
252 | X_COND_WAIT (ctx->acquire_c, acquire_m); |
184 | X_UNLOCK (wait_m); |
253 | X_UNLOCK (acquire_m); |
185 | |
254 | |
|
|
255 | jeret = ctx->jeret; |
186 | tctx_put (ctx); |
256 | tctx_put (ctx); |
187 | pthread_sigmask (SIG_SETMASK, &cursigset, 0); |
257 | pthread_sigmask (SIG_SETMASK, &cursigset, 0); |
|
|
258 | |
|
|
259 | if (jeret) |
|
|
260 | { |
|
|
261 | dTHX; |
|
|
262 | JMPENV_JUMP (jeret); |
|
|
263 | } |
188 | } |
264 | } |
189 | |
265 | |
190 | static void |
266 | static void |
191 | set_thread_enable (pTHX_ void *arg) |
267 | set_thread_enable (pTHX_ void *arg) |
192 | { |
268 | { |
… | |
… | |
197 | |
273 | |
198 | PROTOTYPES: DISABLE |
274 | PROTOTYPES: DISABLE |
199 | |
275 | |
200 | BOOT: |
276 | BOOT: |
201 | { |
277 | { |
202 | #ifndef _WIN32 |
278 | #ifndef _WIN32 |
203 | sigfillset (&fullsigset); |
279 | sigfillset (&fullsigset); |
204 | #endif |
280 | #endif |
205 | |
281 | |
206 | pthread_key_create (¤t_key, 0); |
282 | X_TLS_INIT (current_key); |
|
|
283 | #if RECURSION_CHECK |
|
|
284 | X_TLS_INIT (check_key); |
|
|
285 | #endif |
207 | |
286 | |
208 | if (s_epipe_new (&ep)) |
287 | if (s_epipe_new (&ep)) |
209 | croak ("Coro::Multicore: unable to initialise event pipe.\n"); |
288 | croak ("Coro::Multicore: unable to initialise event pipe.\n"); |
210 | |
289 | |
211 | perl_thx = PERL_GET_CONTEXT; |
290 | perl_thx = PERL_GET_CONTEXT; |
212 | |
291 | |
213 | I_CORO_API ("Coro::Multicore"); |
292 | I_CORO_API ("Coro::Multicore"); |
214 | |
293 | |
215 | X_LOCK (perl_m); |
294 | X_LOCK (release_m); |
216 | while (idle < min_idle) |
295 | while (idle < min_idle) |
217 | start_thread (); |
296 | start_thread (); |
218 | start_thread ();//D |
|
|
219 | X_UNLOCK (perl_m); |
297 | X_UNLOCK (release_m); |
220 | |
298 | |
221 | /* not perfectly efficient to do it this way, but it's simple */ |
299 | /* not perfectly efficient to do it this way, but it is simple */ |
222 | perl_multicore_init (); |
300 | perl_multicore_init (); /* calls release */ |
223 | perl_multicore_api->pmapi_release = pmapi_release; |
301 | perl_multicore_api->pmapi_release = pmapi_release; |
224 | perl_multicore_api->pmapi_acquire = pmapi_acquire; |
302 | perl_multicore_api->pmapi_acquire = pmapi_acquire; |
225 | } |
303 | } |
226 | |
304 | |
227 | bool |
305 | bool |
… | |
… | |
248 | ENTER; /* see Guard.xs */ |
326 | ENTER; /* see Guard.xs */ |
249 | |
327 | |
250 | U32 |
328 | U32 |
251 | min_idle_threads (U32 min = NO_INIT) |
329 | min_idle_threads (U32 min = NO_INIT) |
252 | CODE: |
330 | CODE: |
253 | X_LOCK (wait_m); |
331 | X_LOCK (acquire_m); |
254 | RETVAL = min_idle; |
332 | RETVAL = min_idle; |
255 | if (items) |
333 | if (items) |
256 | min_idle = min; |
334 | min_idle = min; |
257 | X_UNLOCK (wait_m); |
335 | X_UNLOCK (acquire_m); |
258 | OUTPUT: |
336 | OUTPUT: |
259 | RETVAL |
337 | RETVAL |
260 | |
338 | |
261 | |
339 | |
262 | int |
340 | int |
… | |
… | |
268 | |
346 | |
269 | void |
347 | void |
270 | poll (...) |
348 | poll (...) |
271 | CODE: |
349 | CODE: |
272 | s_epipe_drain (&ep); |
350 | s_epipe_drain (&ep); |
273 | X_LOCK (wait_m); |
351 | X_LOCK (acquire_m); |
274 | while (waiters_count) |
352 | while (acquirers.cur) |
275 | { |
353 | { |
276 | struct tctx *ctx = waiters [--waiters_count]; |
354 | struct tctx *ctx = tctxs_get (&acquirers); |
277 | CORO_READY ((SV *)ctx->coro); |
355 | CORO_READY ((SV *)ctx->coro); |
278 | SvREFCNT_dec_NN ((SV *)ctx->coro); |
356 | SvREFCNT_dec_NN ((SV *)ctx->coro); |
279 | ctx->coro = 0; |
357 | ctx->coro = 0; |
280 | } |
358 | } |
281 | X_UNLOCK (wait_m); |
359 | X_UNLOCK (acquire_m); |
282 | |
360 | |
283 | void |
361 | void |
284 | sleep (NV seconds) |
362 | sleep (NV seconds) |
285 | CODE: |
363 | CODE: |
286 | perlinterp_release (); |
364 | perlinterp_release (); |
287 | usleep (seconds * 1e6); |
365 | { |
|
|
366 | int nsec = seconds; |
|
|
367 | if (nsec) sleep (nsec); |
|
|
368 | nsec = (seconds - nsec) * 1e9; |
|
|
369 | if (nsec) usleep (nsec); |
|
|
370 | } |
288 | perlinterp_acquire (); |
371 | perlinterp_acquire (); |
289 | |
372 | |