ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Multicore/Multicore.xs
(Generate patch)

Comparing Coro-Multicore/Multicore.xs (file contents):
Revision 1.4 by root, Sun Jun 28 17:39:42 2015 UTC vs.
Revision 1.21 by root, Sun Aug 26 15:30:55 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines