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