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

Comparing Coro/Coro/State.xs (file contents):
Revision 1.305 by root, Wed Nov 19 10:44:41 2008 UTC vs.
Revision 1.359 by root, Mon Jun 29 05:07:19 2009 UTC

55 55
56#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
57 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
58 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
59 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
60 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 60 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
61 61
62#if !PERL_VERSION_ATLEAST (5,6,0) 62#if !PERL_VERSION_ATLEAST (5,6,0)
63# ifndef PL_ppaddr 63# ifndef PL_ppaddr
64# define PL_ppaddr ppaddr 64# define PL_ppaddr ppaddr
65# endif 65# endif
98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on 100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
103 109
104/* 5.8.7 */ 110/* 5.8.7 */
105#ifndef SvRV_set 111#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 113#endif
127#else 133#else
128# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
130#endif 136#endif
131 137
132#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
133 139
134#if __GNUC__ >= 3 140#if __GNUC__ >= 3
135# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
136# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr), (value))
137# define INLINE static inline 143# define INLINE static inline
138#else 144#else
139# define attribute(x) 145# define attribute(x)
140# define expect(expr,value) (expr) 146# define expect(expr,value) (expr)
141# define INLINE static 147# define INLINE static
150#define GCoroAPI (&coroapi) /* very sneaky */ 156#define GCoroAPI (&coroapi) /* very sneaky */
151 157
152#ifdef USE_ITHREADS 158#ifdef USE_ITHREADS
153# if CORO_PTHREAD 159# if CORO_PTHREAD
154static void *coro_thx; 160static void *coro_thx;
161# endif
162#endif
163
164#ifdef __linux
165# include <time.h> /* for timespec */
166# include <syscall.h> /* for SYS_* */
167# ifdef SYS_clock_gettime
168# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
169# define CORO_CLOCK_MONOTONIC 1
170# define CORO_CLOCK_THREAD_CPUTIME_ID 3
155# endif 171# endif
156#endif 172#endif
157 173
158static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
159 175
167static AV *main_mainstack; /* used to differentiate between $main and others */ 183static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 184static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 185static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 186static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 187
188static AV *av_destroy; /* destruction queue */
189static SV *sv_manager; /* the manager coro */
190static SV *sv_idle; /* $Coro::idle */
191
172static GV *irsgv; /* $/ */ 192static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 193static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 194static SV *rv_diehook;
175static SV *rv_warnhook; 195static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 196static HV *hv_sig; /* %SIG */
177 197
178/* async_pool helper stuff */ 198/* async_pool helper stuff */
179static SV *sv_pool_rss; 199static SV *sv_pool_rss;
180static SV *sv_pool_size; 200static SV *sv_pool_size;
201static SV *sv_async_pool_idle; /* description string */
181static AV *av_async_pool; 202static AV *av_async_pool; /* idle pool */
203static SV *sv_Coro; /* class string */
204static CV *cv_pool_handler;
205static CV *cv_coro_state_new;
182 206
183/* Coro::AnyEvent */ 207/* Coro::AnyEvent */
184static SV *sv_activity; 208static SV *sv_activity;
209
210/* enable processtime/realtime profiling */
211static char profile_times;
212typedef U32 coro_ts[2];
185 213
186static struct coro_cctx *cctx_first; 214static struct coro_cctx *cctx_first;
187static int cctx_count, cctx_idle; 215static int cctx_count, cctx_idle;
188 216
189enum { 217enum {
215 int valgrind_id; 243 int valgrind_id;
216#endif 244#endif
217 unsigned char flags; 245 unsigned char flags;
218} coro_cctx; 246} coro_cctx;
219 247
248coro_cctx *cctx_current; /* the currently running cctx */
249
250/*****************************************************************************/
251
220enum { 252enum {
221 CF_RUNNING = 0x0001, /* coroutine is running */ 253 CF_RUNNING = 0x0001, /* coroutine is running */
222 CF_READY = 0x0002, /* coroutine is ready */ 254 CF_READY = 0x0002, /* coroutine is ready */
223 CF_NEW = 0x0004, /* has never been switched to */ 255 CF_NEW = 0x0004, /* has never been switched to */
224 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 256 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
257 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
225}; 258};
226 259
227/* the structure where most of the perl state is stored, overlaid on the cxstack */ 260/* the structure where most of the perl state is stored, overlaid on the cxstack */
228typedef struct 261typedef struct
229{ 262{
230 SV *defsv; 263 SV *defsv;
231 AV *defav; 264 AV *defav;
232 SV *errsv; 265 SV *errsv;
233 SV *irsgv; 266 SV *irsgv;
267 HV *hinthv;
234#define VAR(name,type) type name; 268#define VAR(name,type) type name;
235# include "state.h" 269# include "state.h"
236#undef VAR 270#undef VAR
237} perl_slots; 271} perl_slots;
238 272
241/* this is a structure representing a perl-level coroutine */ 275/* this is a structure representing a perl-level coroutine */
242struct coro { 276struct coro {
243 /* the C coroutine allocated to this perl coroutine, if any */ 277 /* the C coroutine allocated to this perl coroutine, if any */
244 coro_cctx *cctx; 278 coro_cctx *cctx;
245 279
280 /* ready queue */
281 struct coro *next_ready;
282
246 /* state data */ 283 /* state data */
247 struct CoroSLF slf_frame; /* saved slf frame */ 284 struct CoroSLF slf_frame; /* saved slf frame */
248 AV *mainstack; 285 AV *mainstack;
249 perl_slots *slot; /* basically the saved sp */ 286 perl_slots *slot; /* basically the saved sp */
250 287
288 CV *startcv; /* the CV to execute */
251 AV *args; /* data associated with this coroutine (initial args) */ 289 AV *args; /* data associated with this coroutine (initial args) */
252 int refcnt; /* coroutines are refcounted, yes */ 290 int refcnt; /* coroutines are refcounted, yes */
253 int flags; /* CF_ flags */ 291 int flags; /* CF_ flags */
254 HV *hv; /* the perl hash associated with this coro, if any */ 292 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro); 293 void (*on_destroy)(pTHX_ struct coro *coro);
256 294
257 /* statistics */ 295 /* statistics */
258 int usecount; /* number of transfers to this coro */ 296 int usecount; /* number of transfers to this coro */
259 297
262 SV *except; /* exception to be thrown */ 300 SV *except; /* exception to be thrown */
263 SV *rouse_cb; 301 SV *rouse_cb;
264 302
265 /* async_pool */ 303 /* async_pool */
266 SV *saved_deffh; 304 SV *saved_deffh;
305 SV *invoke_cb;
306 AV *invoke_av;
307
308 /* on_enter/on_leave */
309 AV *on_enter;
310 AV *on_leave;
267 311
268 /* linked list */ 312 /* linked list */
269 struct coro *next, *prev; 313 struct coro *next, *prev;
270}; 314};
271 315
277/* the mainr easonw e don't support windows process emulation */ 321/* the mainr easonw e don't support windows process emulation */
278static struct CoroSLF slf_frame; /* the current slf frame */ 322static struct CoroSLF slf_frame; /* the current slf frame */
279 323
280/** Coro ********************************************************************/ 324/** Coro ********************************************************************/
281 325
282#define PRIO_MAX 3 326#define CORO_PRIO_MAX 3
283#define PRIO_HIGH 1 327#define CORO_PRIO_HIGH 1
284#define PRIO_NORMAL 0 328#define CORO_PRIO_NORMAL 0
285#define PRIO_LOW -1 329#define CORO_PRIO_LOW -1
286#define PRIO_IDLE -3 330#define CORO_PRIO_IDLE -3
287#define PRIO_MIN -4 331#define CORO_PRIO_MIN -4
288 332
289/* for Coro.pm */ 333/* for Coro.pm */
290static SV *coro_current; 334static SV *coro_current;
291static SV *coro_readyhook; 335static SV *coro_readyhook;
292static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 336static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
337static CV *cv_coro_run, *cv_coro_terminate;
293static struct coro *coro_first; 338static struct coro *coro_first;
294#define coro_nready coroapi.nready 339#define coro_nready coroapi.nready
295 340
296/** lowlevel stuff **********************************************************/ 341/** lowlevel stuff **********************************************************/
297 342
323 get_hv (name, create); 368 get_hv (name, create);
324#endif 369#endif
325 return get_hv (name, create); 370 return get_hv (name, create);
326} 371}
327 372
373/* may croak */
374INLINE CV *
375coro_sv_2cv (pTHX_ SV *sv)
376{
377 HV *st;
378 GV *gvp;
379 CV *cv = sv_2cv (sv, &st, &gvp, 0);
380
381 if (!cv)
382 croak ("code reference expected");
383
384 return cv;
385}
386
387/*****************************************************************************/
388/* magic glue */
389
390#define CORO_MAGIC_type_cv 26
391#define CORO_MAGIC_type_state PERL_MAGIC_ext
392
393#define CORO_MAGIC_NN(sv, type) \
394 (expect_true (SvMAGIC (sv)->mg_type == type) \
395 ? SvMAGIC (sv) \
396 : mg_find (sv, type))
397
398#define CORO_MAGIC(sv, type) \
399 (expect_true (SvMAGIC (sv)) \
400 ? CORO_MAGIC_NN (sv, type) \
401 : 0)
402
403#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
404#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
405
406INLINE struct coro *
407SvSTATE_ (pTHX_ SV *coro)
408{
409 HV *stash;
410 MAGIC *mg;
411
412 if (SvROK (coro))
413 coro = SvRV (coro);
414
415 if (expect_false (SvTYPE (coro) != SVt_PVHV))
416 croak ("Coro::State object required");
417
418 stash = SvSTASH (coro);
419 if (expect_false (stash != coro_stash && stash != coro_state_stash))
420 {
421 /* very slow, but rare, check */
422 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
423 croak ("Coro::State object required");
424 }
425
426 mg = CORO_MAGIC_state (coro);
427 return (struct coro *)mg->mg_ptr;
428}
429
430#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
431
432/* faster than SvSTATE, but expects a coroutine hv */
433#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
434#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
435
436/*****************************************************************************/
437/* padlist management and caching */
438
328static AV * 439static AV *
329coro_clone_padlist (pTHX_ CV *cv) 440coro_derive_padlist (pTHX_ CV *cv)
330{ 441{
331 AV *padlist = CvPADLIST (cv); 442 AV *padlist = CvPADLIST (cv);
332 AV *newpadlist, *newpad; 443 AV *newpadlist, *newpad;
333 444
334 newpadlist = newAV (); 445 newpadlist = newAV ();
339 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 450 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
340#endif 451#endif
341 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 452 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
342 --AvFILLp (padlist); 453 --AvFILLp (padlist);
343 454
344 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 455 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
345 av_store (newpadlist, 1, (SV *)newpad); 456 av_store (newpadlist, 1, (SV *)newpad);
346 457
347 return newpadlist; 458 return newpadlist;
348} 459}
349 460
350static void 461static void
351free_padlist (pTHX_ AV *padlist) 462free_padlist (pTHX_ AV *padlist)
352{ 463{
353 /* may be during global destruction */ 464 /* may be during global destruction */
354 if (SvREFCNT (padlist)) 465 if (!IN_DESTRUCT)
355 { 466 {
356 I32 i = AvFILLp (padlist); 467 I32 i = AvFILLp (padlist);
357 while (i >= 0) 468
469 while (i > 0) /* special-case index 0 */
358 { 470 {
359 SV **svp = av_fetch (padlist, i--, FALSE); 471 /* we try to be extra-careful here */
360 if (svp) 472 AV *av = (AV *)AvARRAY (padlist)[i--];
361 { 473 I32 j = AvFILLp (av);
362 SV *sv; 474
363 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 475 while (j >= 0)
476 SvREFCNT_dec (AvARRAY (av)[j--]);
477
478 AvFILLp (av) = -1;
364 SvREFCNT_dec (sv); 479 SvREFCNT_dec (av);
365
366 SvREFCNT_dec (*svp);
367 }
368 } 480 }
369 481
482 SvREFCNT_dec (AvARRAY (padlist)[0]);
483
484 AvFILLp (padlist) = -1;
370 SvREFCNT_dec ((SV*)padlist); 485 SvREFCNT_dec ((SV*)padlist);
371 } 486 }
372} 487}
373 488
374static int 489static int
375coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 490coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
376{ 491{
377 AV *padlist; 492 AV *padlist;
378 AV *av = (AV *)mg->mg_obj; 493 AV *av = (AV *)mg->mg_obj;
494
495 /* perl manages to free our internal AV and _then_ call us */
496 if (IN_DESTRUCT)
497 return;
379 498
380 /* casting is fun. */ 499 /* casting is fun. */
381 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 500 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
382 free_padlist (aTHX_ padlist); 501 free_padlist (aTHX_ padlist);
383 502
384 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 503 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
385 504
386 return 0; 505 return 0;
387} 506}
388
389#define CORO_MAGIC_type_cv 26
390#define CORO_MAGIC_type_state PERL_MAGIC_ext
391 507
392static MGVTBL coro_cv_vtbl = { 508static MGVTBL coro_cv_vtbl = {
393 0, 0, 0, 0, 509 0, 0, 0, 0,
394 coro_cv_free 510 coro_cv_free
395}; 511};
396
397#define CORO_MAGIC_NN(sv, type) \
398 (expect_true (SvMAGIC (sv)->mg_type == type) \
399 ? SvMAGIC (sv) \
400 : mg_find (sv, type))
401
402#define CORO_MAGIC(sv, type) \
403 (expect_true (SvMAGIC (sv)) \
404 ? CORO_MAGIC_NN (sv, type) \
405 : 0)
406
407#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
408#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
409
410INLINE struct coro *
411SvSTATE_ (pTHX_ SV *coro)
412{
413 HV *stash;
414 MAGIC *mg;
415
416 if (SvROK (coro))
417 coro = SvRV (coro);
418
419 if (expect_false (SvTYPE (coro) != SVt_PVHV))
420 croak ("Coro::State object required");
421
422 stash = SvSTASH (coro);
423 if (expect_false (stash != coro_stash && stash != coro_state_stash))
424 {
425 /* very slow, but rare, check */
426 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
427 croak ("Coro::State object required");
428 }
429
430 mg = CORO_MAGIC_state (coro);
431 return (struct coro *)mg->mg_ptr;
432}
433
434#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
435
436/* faster than SvSTATE, but expects a coroutine hv */
437#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
438#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
439 512
440/* the next two functions merely cache the padlists */ 513/* the next two functions merely cache the padlists */
441static void 514static void
442get_padlist (pTHX_ CV *cv) 515get_padlist (pTHX_ CV *cv)
443{ 516{
454 CV *cp = Perl_cv_clone (aTHX_ cv); 527 CV *cp = Perl_cv_clone (aTHX_ cv);
455 CvPADLIST (cv) = CvPADLIST (cp); 528 CvPADLIST (cv) = CvPADLIST (cp);
456 CvPADLIST (cp) = 0; 529 CvPADLIST (cp) = 0;
457 SvREFCNT_dec (cp); 530 SvREFCNT_dec (cp);
458#else 531#else
459 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 532 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
460#endif 533#endif
461 } 534 }
462} 535}
463 536
464static void 537static void
471 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 544 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
472 545
473 av = (AV *)mg->mg_obj; 546 av = (AV *)mg->mg_obj;
474 547
475 if (expect_false (AvFILLp (av) >= AvMAX (av))) 548 if (expect_false (AvFILLp (av) >= AvMAX (av)))
476 av_extend (av, AvMAX (av) + 1); 549 av_extend (av, AvFILLp (av) + 1);
477 550
478 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 551 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
479} 552}
480 553
481/** load & save, init *******************************************************/ 554/** load & save, init *******************************************************/
555
556static void
557on_enterleave_call (pTHX_ SV *cb);
482 558
483static void 559static void
484load_perl (pTHX_ Coro__State c) 560load_perl (pTHX_ Coro__State c)
485{ 561{
486 perl_slots *slot = c->slot; 562 perl_slots *slot = c->slot;
487 c->slot = 0; 563 c->slot = 0;
488 564
489 PL_mainstack = c->mainstack; 565 PL_mainstack = c->mainstack;
490 566
491 GvSV (PL_defgv) = slot->defsv; 567 GvSV (PL_defgv) = slot->defsv;
492 GvAV (PL_defgv) = slot->defav; 568 GvAV (PL_defgv) = slot->defav;
493 GvSV (PL_errgv) = slot->errsv; 569 GvSV (PL_errgv) = slot->errsv;
494 GvSV (irsgv) = slot->irsgv; 570 GvSV (irsgv) = slot->irsgv;
571 GvHV (PL_hintgv) = slot->hinthv;
495 572
496 #define VAR(name,type) PL_ ## name = slot->name; 573 #define VAR(name,type) PL_ ## name = slot->name;
497 # include "state.h" 574 # include "state.h"
498 #undef VAR 575 #undef VAR
499 576
513 PUTBACK; 590 PUTBACK;
514 } 591 }
515 592
516 slf_frame = c->slf_frame; 593 slf_frame = c->slf_frame;
517 CORO_THROW = c->except; 594 CORO_THROW = c->except;
595
596 if (expect_false (c->on_enter))
597 {
598 int i;
599
600 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
601 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
602 }
518} 603}
519 604
520static void 605static void
521save_perl (pTHX_ Coro__State c) 606save_perl (pTHX_ Coro__State c)
522{ 607{
608 if (expect_false (c->on_leave))
609 {
610 int i;
611
612 for (i = AvFILLp (c->on_leave); i >= 0; --i)
613 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
614 }
615
523 c->except = CORO_THROW; 616 c->except = CORO_THROW;
524 c->slf_frame = slf_frame; 617 c->slf_frame = slf_frame;
525 618
526 { 619 {
527 dSP; 620 dSP;
540 { 633 {
541 while (expect_true (cxix >= 0)) 634 while (expect_true (cxix >= 0))
542 { 635 {
543 PERL_CONTEXT *cx = &ccstk[cxix--]; 636 PERL_CONTEXT *cx = &ccstk[cxix--];
544 637
545 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 638 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
546 { 639 {
547 CV *cv = cx->blk_sub.cv; 640 CV *cv = cx->blk_sub.cv;
548 641
549 if (expect_true (CvDEPTH (cv))) 642 if (expect_true (CvDEPTH (cv)))
550 { 643 {
585 c->mainstack = PL_mainstack; 678 c->mainstack = PL_mainstack;
586 679
587 { 680 {
588 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 681 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
589 682
590 slot->defav = GvAV (PL_defgv); 683 slot->defav = GvAV (PL_defgv);
591 slot->defsv = DEFSV; 684 slot->defsv = DEFSV;
592 slot->errsv = ERRSV; 685 slot->errsv = ERRSV;
593 slot->irsgv = GvSV (irsgv); 686 slot->irsgv = GvSV (irsgv);
687 slot->hinthv = GvHV (PL_hintgv);
594 688
595 #define VAR(name,type) slot->name = PL_ ## name; 689 #define VAR(name,type) slot->name = PL_ ## name;
596 # include "state.h" 690 # include "state.h"
597 #undef VAR 691 #undef VAR
598 } 692 }
676#if !PERL_VERSION_ATLEAST (5,10,0) 770#if !PERL_VERSION_ATLEAST (5,10,0)
677 Safefree (PL_retstack); 771 Safefree (PL_retstack);
678#endif 772#endif
679} 773}
680 774
775#define CORO_RSS \
776 rss += sizeof (SYM (curstackinfo)); \
777 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
778 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
779 rss += SYM (tmps_max) * sizeof (SV *); \
780 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
781 rss += SYM (scopestack_max) * sizeof (I32); \
782 rss += SYM (savestack_max) * sizeof (ANY);
783
681static size_t 784static size_t
682coro_rss (pTHX_ struct coro *coro) 785coro_rss (pTHX_ struct coro *coro)
683{ 786{
684 size_t rss = sizeof (*coro); 787 size_t rss = sizeof (*coro);
685 788
686 if (coro->mainstack) 789 if (coro->mainstack)
687 { 790 {
688 perl_slots tmp_slot;
689 perl_slots *slot;
690
691 if (coro->flags & CF_RUNNING) 791 if (coro->flags & CF_RUNNING)
692 { 792 {
693 slot = &tmp_slot; 793 #define SYM(sym) PL_ ## sym
694 794 CORO_RSS;
695 #define VAR(name,type) slot->name = PL_ ## name;
696 # include "state.h"
697 #undef VAR 795 #undef SYM
698 } 796 }
699 else 797 else
700 slot = coro->slot;
701
702 if (slot)
703 { 798 {
704 rss += sizeof (slot->curstackinfo); 799 #define SYM(sym) coro->slot->sym
705 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 800 CORO_RSS;
706 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 801 #undef SYM
707 rss += slot->tmps_max * sizeof (SV *);
708 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
709 rss += slot->scopestack_max * sizeof (I32);
710 rss += slot->savestack_max * sizeof (ANY);
711
712#if !PERL_VERSION_ATLEAST (5,10,0)
713 rss += slot->retstack_max * sizeof (OP *);
714#endif
715 } 802 }
716 } 803 }
717 804
718 return rss; 805 return rss;
719} 806}
732#endif 819#endif
733 820
734/* 821/*
735 * This overrides the default magic get method of %SIG elements. 822 * This overrides the default magic get method of %SIG elements.
736 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 823 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
737 * and instead of tryign to save and restore the hash elements, we just provide 824 * and instead of trying to save and restore the hash elements, we just provide
738 * readback here. 825 * readback here.
739 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
740 * not expecting this to actually change the hook. This is a potential problem
741 * when a schedule happens then, but we ignore this.
742 */ 826 */
743static int 827static int
744coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 828coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
745{ 829{
746 const char *s = MgPV_nolen_const (mg); 830 const char *s = MgPV_nolen_const (mg);
799 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 883 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
800 884
801 if (svp) 885 if (svp)
802 { 886 {
803 SV *old = *svp; 887 SV *old = *svp;
804 *svp = newSVsv (sv); 888 *svp = SvOK (sv) ? newSVsv (sv) : 0;
805 SvREFCNT_dec (old); 889 SvREFCNT_dec (old);
806 return 0; 890 return 0;
807 } 891 }
808 } 892 }
809 893
819 903
820static int 904static int
821slf_check_nop (pTHX_ struct CoroSLF *frame) 905slf_check_nop (pTHX_ struct CoroSLF *frame)
822{ 906{
823 return 0; 907 return 0;
908}
909
910static int
911slf_check_repeat (pTHX_ struct CoroSLF *frame)
912{
913 return 1;
824} 914}
825 915
826static UNOP coro_setup_op; 916static UNOP coro_setup_op;
827 917
828static void NOINLINE /* noinline to keep it out of the transfer fast path */ 918static void NOINLINE /* noinline to keep it out of the transfer fast path */
835 925
836 PL_runops = RUNOPS_DEFAULT; 926 PL_runops = RUNOPS_DEFAULT;
837 PL_curcop = &PL_compiling; 927 PL_curcop = &PL_compiling;
838 PL_in_eval = EVAL_NULL; 928 PL_in_eval = EVAL_NULL;
839 PL_comppad = 0; 929 PL_comppad = 0;
930 PL_comppad_name = 0;
931 PL_comppad_name_fill = 0;
932 PL_comppad_name_floor = 0;
840 PL_curpm = 0; 933 PL_curpm = 0;
841 PL_curpad = 0; 934 PL_curpad = 0;
842 PL_localizing = 0; 935 PL_localizing = 0;
843 PL_dirty = 0; 936 PL_dirty = 0;
844 PL_restartop = 0; 937 PL_restartop = 0;
845#if PERL_VERSION_ATLEAST (5,10,0) 938#if PERL_VERSION_ATLEAST (5,10,0)
846 PL_parser = 0; 939 PL_parser = 0;
847#endif 940#endif
941 PL_hints = 0;
848 942
849 /* recreate the die/warn hooks */ 943 /* recreate the die/warn hooks */
850 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 944 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
851 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 945 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
852 946
853 GvSV (PL_defgv) = newSV (0); 947 GvSV (PL_defgv) = newSV (0);
854 GvAV (PL_defgv) = coro->args; coro->args = 0; 948 GvAV (PL_defgv) = coro->args; coro->args = 0;
855 GvSV (PL_errgv) = newSV (0); 949 GvSV (PL_errgv) = newSV (0);
856 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 950 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
951 GvHV (PL_hintgv) = 0;
857 PL_rs = newSVsv (GvSV (irsgv)); 952 PL_rs = newSVsv (GvSV (irsgv));
858 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 953 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
859 954
860 { 955 {
861 dSP; 956 dSP;
862 UNOP myop; 957 UNOP myop;
863 958
864 Zero (&myop, 1, UNOP); 959 Zero (&myop, 1, UNOP);
865 myop.op_next = Nullop; 960 myop.op_next = Nullop;
961 myop.op_type = OP_ENTERSUB;
866 myop.op_flags = OPf_WANT_VOID; 962 myop.op_flags = OPf_WANT_VOID;
867 963
868 PUSHMARK (SP); 964 PUSHMARK (SP);
869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 965 PUSHs ((SV *)coro->startcv);
870 PUTBACK; 966 PUTBACK;
871 PL_op = (OP *)&myop; 967 PL_op = (OP *)&myop;
872 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 968 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
873 SPAGAIN;
874 } 969 }
875 970
876 /* this newly created coroutine might be run on an existing cctx which most 971 /* this newly created coroutine might be run on an existing cctx which most
877 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 972 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
878 */ 973 */
879 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 974 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
880 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 975 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
881 976
882 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 977 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
883 coro_setup_op.op_next = PL_op; 978 coro_setup_op.op_next = PL_op;
884 coro_setup_op.op_type = OP_CUSTOM; 979 coro_setup_op.op_type = OP_ENTERSUB;
885 coro_setup_op.op_ppaddr = pp_slf; 980 coro_setup_op.op_ppaddr = pp_slf;
886 /* no flags required, as an init function won't be called */ 981 /* no flags etc. required, as an init function won't be called */
887 982
888 PL_op = (OP *)&coro_setup_op; 983 PL_op = (OP *)&coro_setup_op;
889 984
890 /* copy throw, in case it was set before coro_setup */ 985 /* copy throw, in case it was set before coro_setup */
891 CORO_THROW = coro->except; 986 CORO_THROW = coro->except;
892} 987}
893 988
894static void 989static void
895coro_destruct (pTHX_ struct coro *coro) 990coro_unwind_stacks (pTHX)
896{ 991{
897 if (!IN_DESTRUCT) 992 if (!IN_DESTRUCT)
898 { 993 {
899 /* restore all saved variables and stuff */ 994 /* restore all saved variables and stuff */
900 LEAVE_SCOPE (0); 995 LEAVE_SCOPE (0);
908 POPSTACK_TO (PL_mainstack); 1003 POPSTACK_TO (PL_mainstack);
909 1004
910 /* unwind main stack */ 1005 /* unwind main stack */
911 dounwind (-1); 1006 dounwind (-1);
912 } 1007 }
1008}
913 1009
914 SvREFCNT_dec (GvSV (PL_defgv)); 1010static void
915 SvREFCNT_dec (GvAV (PL_defgv)); 1011coro_destruct_perl (pTHX_ struct coro *coro)
916 SvREFCNT_dec (GvSV (PL_errgv)); 1012{
917 SvREFCNT_dec (PL_defoutgv); 1013 SV *svf [9];
918 SvREFCNT_dec (PL_rs);
919 SvREFCNT_dec (GvSV (irsgv));
920 1014
921 SvREFCNT_dec (PL_diehook);
922 SvREFCNT_dec (PL_warnhook);
923 1015 {
924 SvREFCNT_dec (CORO_THROW); 1016 struct coro *current = SvSTATE_current;
1017
1018 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1019
1020 save_perl (aTHX_ current);
1021 load_perl (aTHX_ coro);
1022
1023 coro_unwind_stacks (aTHX);
1024 coro_destruct_stacks (aTHX);
1025
1026 // now save some sv's to be free'd later
1027 svf [0] = GvSV (PL_defgv);
1028 svf [1] = (SV *)GvAV (PL_defgv);
1029 svf [2] = GvSV (PL_errgv);
1030 svf [3] = (SV *)PL_defoutgv;
1031 svf [4] = PL_rs;
1032 svf [5] = GvSV (irsgv);
1033 svf [6] = (SV *)GvHV (PL_hintgv);
1034 svf [7] = PL_diehook;
1035 svf [8] = PL_warnhook;
1036 assert (9 == sizeof (svf) / sizeof (*svf));
1037
1038 load_perl (aTHX_ current);
1039 }
1040
1041 {
1042 int i;
1043
1044 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1045 SvREFCNT_dec (svf [i]);
1046
925 SvREFCNT_dec (coro->saved_deffh); 1047 SvREFCNT_dec (coro->saved_deffh);
926 SvREFCNT_dec (coro->rouse_cb); 1048 SvREFCNT_dec (coro->rouse_cb);
927 1049 SvREFCNT_dec (coro->invoke_cb);
928 coro_destruct_stacks (aTHX); 1050 SvREFCNT_dec (coro->invoke_av);
1051 }
929} 1052}
930 1053
931INLINE void 1054INLINE void
932free_coro_mortal (pTHX) 1055free_coro_mortal (pTHX)
933{ 1056{
941static int 1064static int
942runops_trace (pTHX) 1065runops_trace (pTHX)
943{ 1066{
944 COP *oldcop = 0; 1067 COP *oldcop = 0;
945 int oldcxix = -2; 1068 int oldcxix = -2;
946 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
947 coro_cctx *cctx = coro->cctx;
948 1069
949 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1070 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
950 { 1071 {
951 PERL_ASYNC_CHECK (); 1072 PERL_ASYNC_CHECK ();
952 1073
953 if (cctx->flags & CC_TRACE_ALL) 1074 if (cctx_current->flags & CC_TRACE_ALL)
954 { 1075 {
955 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1076 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
956 { 1077 {
957 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1078 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
958 SV **bot, **top; 1079 SV **bot, **top;
959 AV *av = newAV (); /* return values */ 1080 AV *av = newAV (); /* return values */
960 SV **cb; 1081 SV **cb;
997 1118
998 if (PL_curcop != &PL_compiling) 1119 if (PL_curcop != &PL_compiling)
999 { 1120 {
1000 SV **cb; 1121 SV **cb;
1001 1122
1002 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1123 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1003 { 1124 {
1004 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1125 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1005 1126
1006 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1127 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1007 { 1128 {
1008 runops_proc_t old_runops = PL_runops;
1009 dSP; 1129 dSP;
1010 GV *gv = CvGV (cx->blk_sub.cv); 1130 GV *gv = CvGV (cx->blk_sub.cv);
1011 SV *fullname = sv_2mortal (newSV (0)); 1131 SV *fullname = sv_2mortal (newSV (0));
1012 1132
1013 if (isGV (gv)) 1133 if (isGV (gv))
1018 SAVETMPS; 1138 SAVETMPS;
1019 EXTEND (SP, 3); 1139 EXTEND (SP, 3);
1020 PUSHMARK (SP); 1140 PUSHMARK (SP);
1021 PUSHs (&PL_sv_yes); 1141 PUSHs (&PL_sv_yes);
1022 PUSHs (fullname); 1142 PUSHs (fullname);
1023 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1143 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1024 PUTBACK; 1144 PUTBACK;
1025 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1145 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1026 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1146 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1027 SPAGAIN; 1147 SPAGAIN;
1028 FREETMPS; 1148 FREETMPS;
1031 } 1151 }
1032 1152
1033 oldcxix = cxstack_ix; 1153 oldcxix = cxstack_ix;
1034 } 1154 }
1035 1155
1036 if (cctx->flags & CC_TRACE_LINE) 1156 if (cctx_current->flags & CC_TRACE_LINE)
1037 { 1157 {
1038 dSP; 1158 dSP;
1039 1159
1040 PL_runops = RUNOPS_DEFAULT; 1160 PL_runops = RUNOPS_DEFAULT;
1041 ENTER; 1161 ENTER;
1060 1180
1061 TAINT_NOT; 1181 TAINT_NOT;
1062 return 0; 1182 return 0;
1063} 1183}
1064 1184
1065static struct coro_cctx *cctx_ssl_cctx;
1066static struct CoroSLF cctx_ssl_frame; 1185static struct CoroSLF cctx_ssl_frame;
1067 1186
1068static void 1187static void
1069slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1188slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1070{ 1189{
1071 ta->prev = (struct coro *)cctx_ssl_cctx;
1072 ta->next = 0; 1190 ta->prev = 0;
1073} 1191}
1074 1192
1075static int 1193static int
1076slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1194slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1077{ 1195{
1080 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1198 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1081} 1199}
1082 1200
1083/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1201/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1084static void NOINLINE 1202static void NOINLINE
1085cctx_prepare (pTHX_ coro_cctx *cctx) 1203cctx_prepare (pTHX)
1086{ 1204{
1087 PL_top_env = &PL_start_env; 1205 PL_top_env = &PL_start_env;
1088 1206
1089 if (cctx->flags & CC_TRACE) 1207 if (cctx_current->flags & CC_TRACE)
1090 PL_runops = runops_trace; 1208 PL_runops = runops_trace;
1091 1209
1092 /* we already must be executing an SLF op, there is no other valid way 1210 /* we already must be executing an SLF op, there is no other valid way
1093 * that can lead to creation of a new cctx */ 1211 * that can lead to creation of a new cctx */
1094 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1212 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1095 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1213 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1096 1214
1097 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1215 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1098 cctx_ssl_cctx = cctx;
1099 cctx_ssl_frame = slf_frame; 1216 cctx_ssl_frame = slf_frame;
1100 1217
1101 slf_frame.prepare = slf_prepare_set_stacklevel; 1218 slf_frame.prepare = slf_prepare_set_stacklevel;
1102 slf_frame.check = slf_check_set_stacklevel; 1219 slf_frame.check = slf_check_set_stacklevel;
1103} 1220}
1126 /* normally we would need to skip the entersub here */ 1243 /* normally we would need to skip the entersub here */
1127 /* not doing so will re-execute it, which is exactly what we want */ 1244 /* not doing so will re-execute it, which is exactly what we want */
1128 /* PL_nop = PL_nop->op_next */ 1245 /* PL_nop = PL_nop->op_next */
1129 1246
1130 /* inject a fake subroutine call to cctx_init */ 1247 /* inject a fake subroutine call to cctx_init */
1131 cctx_prepare (aTHX_ (coro_cctx *)arg); 1248 cctx_prepare (aTHX);
1132 1249
1133 /* cctx_run is the alternative tail of transfer() */ 1250 /* cctx_run is the alternative tail of transfer() */
1134 transfer_tail (aTHX); 1251 transfer_tail (aTHX);
1135 1252
1136 /* somebody or something will hit me for both perl_run and PL_restartop */ 1253 /* somebody or something will hit me for both perl_run and PL_restartop */
1137 PL_restartop = PL_op; 1254 PL_restartop = PL_op;
1138 perl_run (PL_curinterp); 1255 perl_run (PL_curinterp);
1256 /*
1257 * Unfortunately, there is no way to get at the return values of the
1258 * coro body here, as perl_run destroys these
1259 */
1139 1260
1140 /* 1261 /*
1141 * If perl-run returns we assume exit() was being called or the coro 1262 * If perl-run returns we assume exit() was being called or the coro
1142 * fell off the end, which seems to be the only valid (non-bug) 1263 * fell off the end, which seems to be the only valid (non-bug)
1143 * reason for perl_run to return. We try to exit by jumping to the 1264 * reason for perl_run to return. We try to exit by jumping to the
1144 * bootstrap-time "top" top_env, as we cannot restore the "main" 1265 * bootstrap-time "top" top_env, as we cannot restore the "main"
1145 * coroutine as Coro has no such concept 1266 * coroutine as Coro has no such concept.
1267 * This actually isn't valid with the pthread backend, but OSes requiring
1268 * that backend are too broken to do it in a standards-compliant way.
1146 */ 1269 */
1147 PL_top_env = main_top_env; 1270 PL_top_env = main_top_env;
1148 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1271 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1149 } 1272 }
1150} 1273}
1227cctx_destroy (coro_cctx *cctx) 1350cctx_destroy (coro_cctx *cctx)
1228{ 1351{
1229 if (!cctx) 1352 if (!cctx)
1230 return; 1353 return;
1231 1354
1355 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1356
1232 --cctx_count; 1357 --cctx_count;
1233 coro_destroy (&cctx->cctx); 1358 coro_destroy (&cctx->cctx);
1234 1359
1235 /* coro_transfer creates new, empty cctx's */ 1360 /* coro_transfer creates new, empty cctx's */
1236 if (cctx->sptr) 1361 if (cctx->sptr)
1299 /* TODO: throwing up here is considered harmful */ 1424 /* TODO: throwing up here is considered harmful */
1300 1425
1301 if (expect_true (prev != next)) 1426 if (expect_true (prev != next))
1302 { 1427 {
1303 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1428 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1304 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1429 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1305 1430
1306 if (expect_false (next->flags & CF_RUNNING))
1307 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1308
1309 if (expect_false (next->flags & CF_DESTROYED)) 1431 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1310 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1432 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1311 1433
1312#if !PERL_VERSION_ATLEAST (5,10,0) 1434#if !PERL_VERSION_ATLEAST (5,10,0)
1313 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1435 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1314 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1436 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1315#endif 1437#endif
1321transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1443transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1322{ 1444{
1323 dSTACKLEVEL; 1445 dSTACKLEVEL;
1324 1446
1325 /* sometimes transfer is only called to set idle_sp */ 1447 /* sometimes transfer is only called to set idle_sp */
1326 if (expect_false (!next)) 1448 if (expect_false (!prev))
1327 { 1449 {
1328 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1450 cctx_current->idle_sp = STACKLEVEL;
1329 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1451 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1330 } 1452 }
1331 else if (expect_true (prev != next)) 1453 else if (expect_true (prev != next))
1332 { 1454 {
1333 coro_cctx *prev__cctx; 1455 coro_cctx *cctx_prev;
1334 1456
1335 if (expect_false (prev->flags & CF_NEW)) 1457 if (expect_false (prev->flags & CF_NEW))
1336 { 1458 {
1337 /* create a new empty/source context */ 1459 /* create a new empty/source context */
1338 prev->cctx = cctx_new_empty ();
1339 prev->flags &= ~CF_NEW; 1460 prev->flags &= ~CF_NEW;
1340 prev->flags |= CF_RUNNING; 1461 prev->flags |= CF_RUNNING;
1341 } 1462 }
1342 1463
1343 prev->flags &= ~CF_RUNNING; 1464 prev->flags &= ~CF_RUNNING;
1354 coro_setup (aTHX_ next); 1475 coro_setup (aTHX_ next);
1355 } 1476 }
1356 else 1477 else
1357 load_perl (aTHX_ next); 1478 load_perl (aTHX_ next);
1358 1479
1359 prev__cctx = prev->cctx;
1360
1361 /* possibly untie and reuse the cctx */ 1480 /* possibly untie and reuse the cctx */
1362 if (expect_true ( 1481 if (expect_true (
1363 prev__cctx->idle_sp == STACKLEVEL 1482 cctx_current->idle_sp == STACKLEVEL
1364 && !(prev__cctx->flags & CC_TRACE) 1483 && !(cctx_current->flags & CC_TRACE)
1365 && !force_cctx 1484 && !force_cctx
1366 )) 1485 ))
1367 { 1486 {
1368 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1487 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1369 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1488 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1370 1489
1371 prev->cctx = 0;
1372
1373 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1490 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1374 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1491 /* without this the next cctx_get might destroy the running cctx while still in use */
1375 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1492 if (expect_false (CCTX_EXPIRED (cctx_current)))
1376 if (!next->cctx) 1493 if (expect_true (!next->cctx))
1377 next->cctx = cctx_get (aTHX); 1494 next->cctx = cctx_get (aTHX);
1378 1495
1379 cctx_put (prev__cctx); 1496 cctx_put (cctx_current);
1380 } 1497 }
1498 else
1499 prev->cctx = cctx_current;
1381 1500
1382 ++next->usecount; 1501 ++next->usecount;
1383 1502
1384 if (expect_true (!next->cctx)) 1503 cctx_prev = cctx_current;
1385 next->cctx = cctx_get (aTHX); 1504 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1386 1505
1387 if (expect_false (prev__cctx != next->cctx)) 1506 next->cctx = 0;
1507
1508 if (expect_false (cctx_prev != cctx_current))
1388 { 1509 {
1389 prev__cctx->top_env = PL_top_env; 1510 cctx_prev->top_env = PL_top_env;
1390 PL_top_env = next->cctx->top_env; 1511 PL_top_env = cctx_current->top_env;
1391 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1512 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1392 } 1513 }
1393 1514
1394 transfer_tail (aTHX); 1515 transfer_tail (aTHX);
1395 } 1516 }
1396} 1517}
1404coro_state_destroy (pTHX_ struct coro *coro) 1525coro_state_destroy (pTHX_ struct coro *coro)
1405{ 1526{
1406 if (coro->flags & CF_DESTROYED) 1527 if (coro->flags & CF_DESTROYED)
1407 return 0; 1528 return 0;
1408 1529
1409 if (coro->on_destroy) 1530 if (coro->on_destroy && !PL_dirty)
1410 coro->on_destroy (aTHX_ coro); 1531 coro->on_destroy (aTHX_ coro);
1411 1532
1412 coro->flags |= CF_DESTROYED; 1533 coro->flags |= CF_DESTROYED;
1413 1534
1414 if (coro->flags & CF_READY) 1535 if (coro->flags & CF_READY)
1418 --coro_nready; 1539 --coro_nready;
1419 } 1540 }
1420 else 1541 else
1421 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1542 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1422 1543
1423 if (coro->mainstack && coro->mainstack != main_mainstack) 1544 if (coro->mainstack
1424 { 1545 && coro->mainstack != main_mainstack
1425 struct coro temp; 1546 && coro->slot
1426 1547 && !PL_dirty)
1427 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1428
1429 save_perl (aTHX_ &temp);
1430 load_perl (aTHX_ coro);
1431
1432 coro_destruct (aTHX_ coro); 1548 coro_destruct_perl (aTHX_ coro);
1433
1434 load_perl (aTHX_ &temp);
1435
1436 coro->slot = 0;
1437 }
1438 1549
1439 cctx_destroy (coro->cctx); 1550 cctx_destroy (coro->cctx);
1551 SvREFCNT_dec (coro->startcv);
1440 SvREFCNT_dec (coro->args); 1552 SvREFCNT_dec (coro->args);
1553 SvREFCNT_dec (CORO_THROW);
1441 1554
1442 if (coro->next) coro->next->prev = coro->prev; 1555 if (coro->next) coro->next->prev = coro->prev;
1443 if (coro->prev) coro->prev->next = coro->next; 1556 if (coro->prev) coro->prev->next = coro->next;
1444 if (coro == coro_first) coro_first = coro->next; 1557 if (coro == coro_first) coro_first = coro->next;
1445 1558
1527/** Coro ********************************************************************/ 1640/** Coro ********************************************************************/
1528 1641
1529INLINE void 1642INLINE void
1530coro_enq (pTHX_ struct coro *coro) 1643coro_enq (pTHX_ struct coro *coro)
1531{ 1644{
1532 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1645 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1533}
1534 1646
1535INLINE SV * 1647 SvREFCNT_inc_NN (coro->hv);
1648
1649 coro->next_ready = 0;
1650 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1651 ready [1] = coro;
1652}
1653
1654INLINE struct coro *
1536coro_deq (pTHX) 1655coro_deq (pTHX)
1537{ 1656{
1538 int prio; 1657 int prio;
1539 1658
1540 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1659 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1541 if (AvFILLp (coro_ready [prio]) >= 0) 1660 {
1542 return av_shift (coro_ready [prio]); 1661 struct coro **ready = coro_ready [prio];
1662
1663 if (ready [0])
1664 {
1665 struct coro *coro = ready [0];
1666 ready [0] = coro->next_ready;
1667 return coro;
1668 }
1669 }
1543 1670
1544 return 0; 1671 return 0;
1545} 1672}
1546 1673
1547static int 1674static int
1549{ 1676{
1550 struct coro *coro; 1677 struct coro *coro;
1551 SV *sv_hook; 1678 SV *sv_hook;
1552 void (*xs_hook)(void); 1679 void (*xs_hook)(void);
1553 1680
1554 if (SvROK (coro_sv))
1555 coro_sv = SvRV (coro_sv);
1556
1557 coro = SvSTATE (coro_sv); 1681 coro = SvSTATE (coro_sv);
1558 1682
1559 if (coro->flags & CF_READY) 1683 if (coro->flags & CF_READY)
1560 return 0; 1684 return 0;
1561 1685
1592api_is_ready (pTHX_ SV *coro_sv) 1716api_is_ready (pTHX_ SV *coro_sv)
1593{ 1717{
1594 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1718 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1595} 1719}
1596 1720
1721/* expects to own a reference to next->hv */
1597INLINE void 1722INLINE void
1598prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1723prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1599{ 1724{
1600 SV *prev_sv, *next_sv;
1601
1602 for (;;)
1603 {
1604 next_sv = coro_deq (aTHX);
1605
1606 /* nothing to schedule: call the idle handler */
1607 if (expect_false (!next_sv))
1608 {
1609 dSP;
1610
1611 ENTER;
1612 SAVETMPS;
1613
1614 PUSHMARK (SP);
1615 PUTBACK;
1616 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1617
1618 FREETMPS;
1619 LEAVE;
1620 continue;
1621 }
1622
1623 ta->next = SvSTATE_hv (next_sv);
1624
1625 /* cannot transfer to destroyed coros, skip and look for next */
1626 if (expect_false (ta->next->flags & CF_DESTROYED))
1627 {
1628 SvREFCNT_dec (next_sv);
1629 /* coro_nready has already been taken care of by destroy */
1630 continue;
1631 }
1632
1633 --coro_nready;
1634 break;
1635 }
1636
1637 /* free this only after the transfer */
1638 prev_sv = SvRV (coro_current); 1725 SV *prev_sv = SvRV (coro_current);
1726
1639 ta->prev = SvSTATE_hv (prev_sv); 1727 ta->prev = SvSTATE_hv (prev_sv);
1728 ta->next = next;
1729
1640 TRANSFER_CHECK (*ta); 1730 TRANSFER_CHECK (*ta);
1641 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1731
1642 ta->next->flags &= ~CF_READY;
1643 SvRV_set (coro_current, next_sv); 1732 SvRV_set (coro_current, (SV *)next->hv);
1644 1733
1645 free_coro_mortal (aTHX); 1734 free_coro_mortal (aTHX);
1646 coro_mortal = prev_sv; 1735 coro_mortal = prev_sv;
1647} 1736}
1648 1737
1738static void
1739prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1740{
1741 for (;;)
1742 {
1743 struct coro *next = coro_deq (aTHX);
1744
1745 if (expect_true (next))
1746 {
1747 /* cannot transfer to destroyed coros, skip and look for next */
1748 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1749 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1750 else
1751 {
1752 next->flags &= ~CF_READY;
1753 --coro_nready;
1754
1755 prepare_schedule_to (aTHX_ ta, next);
1756 break;
1757 }
1758 }
1759 else
1760 {
1761 /* nothing to schedule: call the idle handler */
1762 if (SvROK (sv_idle)
1763 && SvOBJECT (SvRV (sv_idle)))
1764 {
1765 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1766 api_ready (aTHX_ SvRV (sv_idle));
1767 --coro_nready;
1768 }
1769 else
1770 {
1771 dSP;
1772
1773 ENTER;
1774 SAVETMPS;
1775
1776 PUSHMARK (SP);
1777 PUTBACK;
1778 call_sv (sv_idle, G_VOID | G_DISCARD);
1779
1780 FREETMPS;
1781 LEAVE;
1782 }
1783 }
1784 }
1785}
1786
1649INLINE void 1787INLINE void
1650prepare_cede (pTHX_ struct coro_transfer_args *ta) 1788prepare_cede (pTHX_ struct coro_transfer_args *ta)
1651{ 1789{
1652 api_ready (aTHX_ coro_current); 1790 api_ready (aTHX_ coro_current);
1653 prepare_schedule (aTHX_ ta); 1791 prepare_schedule (aTHX_ ta);
1672{ 1810{
1673 struct coro_transfer_args ta; 1811 struct coro_transfer_args ta;
1674 1812
1675 prepare_schedule (aTHX_ &ta); 1813 prepare_schedule (aTHX_ &ta);
1676 TRANSFER (ta, 1); 1814 TRANSFER (ta, 1);
1815}
1816
1817static void
1818api_schedule_to (pTHX_ SV *coro_sv)
1819{
1820 struct coro_transfer_args ta;
1821 struct coro *next = SvSTATE (coro_sv);
1822
1823 SvREFCNT_inc_NN (coro_sv);
1824 prepare_schedule_to (aTHX_ &ta, next);
1677} 1825}
1678 1826
1679static int 1827static int
1680api_cede (pTHX) 1828api_cede (pTHX)
1681{ 1829{
1710static void 1858static void
1711api_trace (pTHX_ SV *coro_sv, int flags) 1859api_trace (pTHX_ SV *coro_sv, int flags)
1712{ 1860{
1713 struct coro *coro = SvSTATE (coro_sv); 1861 struct coro *coro = SvSTATE (coro_sv);
1714 1862
1863 if (coro->flags & CF_RUNNING)
1864 croak ("cannot enable tracing on a running coroutine, caught");
1865
1715 if (flags & CC_TRACE) 1866 if (flags & CC_TRACE)
1716 { 1867 {
1717 if (!coro->cctx) 1868 if (!coro->cctx)
1718 coro->cctx = cctx_new_run (); 1869 coro->cctx = cctx_new_run ();
1719 else if (!(coro->cctx->flags & CC_TRACE)) 1870 else if (!(coro->cctx->flags & CC_TRACE))
1720 croak ("cannot enable tracing on coroutine with custom stack,"); 1871 croak ("cannot enable tracing on coroutine with custom stack, caught");
1721 1872
1722 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1873 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1723 } 1874 }
1724 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1875 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1725 { 1876 {
1730 else 1881 else
1731 coro->slot->runops = RUNOPS_DEFAULT; 1882 coro->slot->runops = RUNOPS_DEFAULT;
1732 } 1883 }
1733} 1884}
1734 1885
1886static void
1887coro_call_on_destroy (pTHX_ struct coro *coro)
1888{
1889 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1890 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1891
1892 if (on_destroyp)
1893 {
1894 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1895
1896 while (AvFILLp (on_destroy) >= 0)
1897 {
1898 dSP; /* don't disturb outer sp */
1899 SV *cb = av_pop (on_destroy);
1900
1901 PUSHMARK (SP);
1902
1903 if (statusp)
1904 {
1905 int i;
1906 AV *status = (AV *)SvRV (*statusp);
1907 EXTEND (SP, AvFILLp (status) + 1);
1908
1909 for (i = 0; i <= AvFILLp (status); ++i)
1910 PUSHs (AvARRAY (status)[i]);
1911 }
1912
1913 PUTBACK;
1914 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1915 }
1916 }
1917}
1918
1919static void
1920slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1921{
1922 int i;
1923 HV *hv = (HV *)SvRV (coro_current);
1924 AV *av = newAV ();
1925
1926 av_extend (av, items - 1);
1927 for (i = 0; i < items; ++i)
1928 av_push (av, SvREFCNT_inc_NN (arg [i]));
1929
1930 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1931
1932 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1933 api_ready (aTHX_ sv_manager);
1934
1935 frame->prepare = prepare_schedule;
1936 frame->check = slf_check_repeat;
1937
1938 /* as a minor optimisation, we could unwind all stacks here */
1939 /* but that puts extra pressure on pp_slf, and is not worth much */
1940 /*coro_unwind_stacks (aTHX);*/
1941}
1942
1943/*****************************************************************************/
1944/* async pool handler */
1945
1946static int
1947slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1948{
1949 HV *hv = (HV *)SvRV (coro_current);
1950 struct coro *coro = (struct coro *)frame->data;
1951
1952 if (!coro->invoke_cb)
1953 return 1; /* loop till we have invoke */
1954 else
1955 {
1956 hv_store (hv, "desc", sizeof ("desc") - 1,
1957 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1958
1959 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1960
1961 {
1962 dSP;
1963 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1964 PUTBACK;
1965 }
1966
1967 SvREFCNT_dec (GvAV (PL_defgv));
1968 GvAV (PL_defgv) = coro->invoke_av;
1969 coro->invoke_av = 0;
1970
1971 return 0;
1972 }
1973}
1974
1975static void
1976slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1977{
1978 HV *hv = (HV *)SvRV (coro_current);
1979 struct coro *coro = SvSTATE_hv ((SV *)hv);
1980
1981 if (expect_true (coro->saved_deffh))
1982 {
1983 /* subsequent iteration */
1984 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1985 coro->saved_deffh = 0;
1986
1987 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1988 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1989 {
1990 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1991 coro->invoke_av = newAV ();
1992
1993 frame->prepare = prepare_nop;
1994 }
1995 else
1996 {
1997 av_clear (GvAV (PL_defgv));
1998 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1999
2000 coro->prio = 0;
2001
2002 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2003 api_trace (aTHX_ coro_current, 0);
2004
2005 frame->prepare = prepare_schedule;
2006 av_push (av_async_pool, SvREFCNT_inc (hv));
2007 }
2008 }
2009 else
2010 {
2011 /* first iteration, simply fall through */
2012 frame->prepare = prepare_nop;
2013 }
2014
2015 frame->check = slf_check_pool_handler;
2016 frame->data = (void *)coro;
2017}
2018
1735/*****************************************************************************/ 2019/*****************************************************************************/
1736/* rouse callback */ 2020/* rouse callback */
1737 2021
1738#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 2022#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1739 2023
1744 SV *data = (SV *)GENSUB_ARG; 2028 SV *data = (SV *)GENSUB_ARG;
1745 2029
1746 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2030 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1747 { 2031 {
1748 /* first call, set args */ 2032 /* first call, set args */
1749 int i; 2033 SV *coro = SvRV (data);
1750 AV *av = newAV (); 2034 AV *av = newAV ();
1751 SV *coro = SvRV (data);
1752 2035
1753 SvRV_set (data, (SV *)av); 2036 SvRV_set (data, (SV *)av);
1754 api_ready (aTHX_ coro);
1755 SvREFCNT_dec (coro);
1756 2037
1757 /* better take a full copy of the arguments */ 2038 /* better take a full copy of the arguments */
1758 while (items--) 2039 while (items--)
1759 av_store (av, items, newSVsv (ST (items))); 2040 av_store (av, items, newSVsv (ST (items)));
2041
2042 api_ready (aTHX_ coro);
2043 SvREFCNT_dec (coro);
1760 } 2044 }
1761 2045
1762 XSRETURN_EMPTY; 2046 XSRETURN_EMPTY;
1763} 2047}
1764 2048
1781 2065
1782 EXTEND (SP, AvFILLp (av) + 1); 2066 EXTEND (SP, AvFILLp (av) + 1);
1783 for (i = 0; i <= AvFILLp (av); ++i) 2067 for (i = 0; i <= AvFILLp (av); ++i)
1784 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2068 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1785 2069
1786 /* we have stolen the elements, so ste length to zero and free */ 2070 /* we have stolen the elements, so set length to zero and free */
1787 AvFILLp (av) = -1; 2071 AvFILLp (av) = -1;
1788 av_undef (av); 2072 av_undef (av);
1789 2073
1790 PUTBACK; 2074 PUTBACK;
1791 } 2075 }
1895static void 2179static void
1896slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2180slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1897{ 2181{
1898 frame->prepare = prepare_schedule; 2182 frame->prepare = prepare_schedule;
1899 frame->check = slf_check_nop; 2183 frame->check = slf_check_nop;
2184}
2185
2186static void
2187slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2188{
2189 struct coro *next = (struct coro *)slf_frame.data;
2190
2191 SvREFCNT_inc_NN (next->hv);
2192 prepare_schedule_to (aTHX_ ta, next);
2193}
2194
2195static void
2196slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2197{
2198 if (!items)
2199 croak ("Coro::schedule_to expects a coroutine argument, caught");
2200
2201 frame->data = (void *)SvSTATE (arg [0]);
2202 frame->prepare = slf_prepare_schedule_to;
2203 frame->check = slf_check_nop;
2204}
2205
2206static void
2207slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2208{
2209 api_ready (aTHX_ SvRV (coro_current));
2210
2211 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1900} 2212}
1901 2213
1902static void 2214static void
1903slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2215slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1904{ 2216{
2050 } 2362 }
2051 else 2363 else
2052 slf_argc = 0; 2364 slf_argc = 0;
2053 2365
2054 PL_op->op_ppaddr = pp_slf; 2366 PL_op->op_ppaddr = pp_slf;
2055 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2367 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2056 2368
2057 PL_op = (OP *)&slf_restore; 2369 PL_op = (OP *)&slf_restore;
2370}
2371
2372/*****************************************************************************/
2373/* dynamic wind */
2374
2375static void
2376on_enterleave_call (pTHX_ SV *cb)
2377{
2378 dSP;
2379
2380 PUSHSTACK;
2381
2382 PUSHMARK (SP);
2383 PUTBACK;
2384 call_sv (cb, G_VOID | G_DISCARD);
2385 SPAGAIN;
2386
2387 POPSTACK;
2388}
2389
2390static SV *
2391coro_avp_pop_and_free (pTHX_ AV **avp)
2392{
2393 AV *av = *avp;
2394 SV *res = av_pop (av);
2395
2396 if (AvFILLp (av) < 0)
2397 {
2398 *avp = 0;
2399 SvREFCNT_dec (av);
2400 }
2401
2402 return res;
2403}
2404
2405static void
2406coro_pop_on_enter (pTHX_ void *coro)
2407{
2408 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2409 SvREFCNT_dec (cb);
2410}
2411
2412static void
2413coro_pop_on_leave (pTHX_ void *coro)
2414{
2415 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2416 on_enterleave_call (aTHX_ sv_2mortal (cb));
2058} 2417}
2059 2418
2060/*****************************************************************************/ 2419/*****************************************************************************/
2061/* PerlIO::cede */ 2420/* PerlIO::cede */
2062 2421
2136/* Coro::Semaphore & Coro::Signal */ 2495/* Coro::Semaphore & Coro::Signal */
2137 2496
2138static SV * 2497static SV *
2139coro_waitarray_new (pTHX_ int count) 2498coro_waitarray_new (pTHX_ int count)
2140{ 2499{
2141 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2500 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2142 AV *av = newAV (); 2501 AV *av = newAV ();
2143 SV **ary; 2502 SV **ary;
2144 2503
2145 /* unfortunately, building manually saves memory */ 2504 /* unfortunately, building manually saves memory */
2146 Newx (ary, 2, SV *); 2505 Newx (ary, 2, SV *);
2147 AvALLOC (av) = ary; 2506 AvALLOC (av) = ary;
2507#if PERL_VERSION_ATLEAST (5,10,0)
2148 /*AvARRAY (av) = ary;*/ 2508 AvARRAY (av) = ary;
2509#else
2149 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2510 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2511 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2512 SvPVX ((SV *)av) = (char *)ary;
2513#endif
2150 AvMAX (av) = 1; 2514 AvMAX (av) = 1;
2151 AvFILLp (av) = 0; 2515 AvFILLp (av) = 0;
2152 ary [0] = newSViv (count); 2516 ary [0] = newSViv (count);
2153 2517
2154 return newRV_noinc ((SV *)av); 2518 return newRV_noinc ((SV *)av);
2174 AvARRAY (av)[0] = AvARRAY (av)[1]; 2538 AvARRAY (av)[0] = AvARRAY (av)[1];
2175 AvARRAY (av)[1] = count_sv; 2539 AvARRAY (av)[1] = count_sv;
2176 cb = av_shift (av); 2540 cb = av_shift (av);
2177 2541
2178 if (SvOBJECT (cb)) 2542 if (SvOBJECT (cb))
2543 {
2179 api_ready (aTHX_ cb); 2544 api_ready (aTHX_ cb);
2180 else 2545 --count;
2181 croak ("callbacks not yet supported"); 2546 }
2547 else if (SvTYPE (cb) == SVt_PVCV)
2548 {
2549 dSP;
2550 PUSHMARK (SP);
2551 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2552 PUTBACK;
2553 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2554 }
2182 2555
2183 SvREFCNT_dec (cb); 2556 SvREFCNT_dec (cb);
2184
2185 --count;
2186 } 2557 }
2187} 2558}
2188 2559
2189static void 2560static void
2190coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2561coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2272} 2643}
2273 2644
2274static void 2645static void
2275slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2646slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2276{ 2647{
2648 if (items >= 2)
2649 {
2650 /* callback form */
2651 AV *av = (AV *)SvRV (arg [0]);
2652 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2653
2654 av_push (av, SvREFCNT_inc_NN (cb_cv));
2655
2656 if (SvIVX (AvARRAY (av)[0]) > 0)
2657 coro_semaphore_adjust (aTHX_ av, 0);
2658
2659 frame->prepare = prepare_nop;
2660 frame->check = slf_check_nop;
2661 }
2662 else
2663 {
2277 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); 2664 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2278 frame->check = slf_check_semaphore_wait; 2665 frame->check = slf_check_semaphore_wait;
2666 }
2279} 2667}
2280 2668
2281/* signal */ 2669/* signal */
2282 2670
2283static void 2671static void
2295 AvARRAY (av)[0] = AvARRAY (av)[1]; 2683 AvARRAY (av)[0] = AvARRAY (av)[1];
2296 AvARRAY (av)[1] = cb; 2684 AvARRAY (av)[1] = cb;
2297 2685
2298 cb = av_shift (av); 2686 cb = av_shift (av);
2299 2687
2688 if (SvTYPE (cb) == SVt_PVCV)
2689 {
2690 dSP;
2691 PUSHMARK (SP);
2692 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2693 PUTBACK;
2694 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2695 }
2696 else
2697 {
2300 api_ready (aTHX_ cb); 2698 api_ready (aTHX_ cb);
2301 sv_setiv (cb, 0); /* signal waiter */ 2699 sv_setiv (cb, 0); /* signal waiter */
2700 }
2701
2302 SvREFCNT_dec (cb); 2702 SvREFCNT_dec (cb);
2303 2703
2304 --count; 2704 --count;
2305 } 2705 }
2306} 2706}
2315static void 2715static void
2316slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2716slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2317{ 2717{
2318 AV *av = (AV *)SvRV (arg [0]); 2718 AV *av = (AV *)SvRV (arg [0]);
2319 2719
2720 if (items >= 2)
2721 {
2722 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2723 av_push (av, SvREFCNT_inc_NN (cb_cv));
2724
2320 if (SvIVX (AvARRAY (av)[0])) 2725 if (SvIVX (AvARRAY (av)[0]))
2726 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2727
2728 frame->prepare = prepare_nop;
2729 frame->check = slf_check_nop;
2730 }
2731 else if (SvIVX (AvARRAY (av)[0]))
2321 { 2732 {
2322 SvIVX (AvARRAY (av)[0]) = 0; 2733 SvIVX (AvARRAY (av)[0]) = 0;
2323 frame->prepare = prepare_nop; 2734 frame->prepare = prepare_nop;
2324 frame->check = slf_check_nop; 2735 frame->check = slf_check_nop;
2325 } 2736 }
2326 else 2737 else
2327 { 2738 {
2328 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2739 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2329 2740
2330 av_push (av, waiter); 2741 av_push (av, waiter);
2331 2742
2332 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2743 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2333 frame->prepare = prepare_schedule; 2744 frame->prepare = prepare_schedule;
2355 dXSARGS; 2766 dXSARGS;
2356 AV *state = (AV *)GENSUB_ARG; 2767 AV *state = (AV *)GENSUB_ARG;
2357 SV *coro = av_pop (state); 2768 SV *coro = av_pop (state);
2358 SV *data_sv = newSV (sizeof (struct io_state)); 2769 SV *data_sv = newSV (sizeof (struct io_state));
2359 2770
2360 av_extend (state, items); 2771 av_extend (state, items - 1);
2361 2772
2362 sv_upgrade (data_sv, SVt_PV); 2773 sv_upgrade (data_sv, SVt_PV);
2363 SvCUR_set (data_sv, sizeof (struct io_state)); 2774 SvCUR_set (data_sv, sizeof (struct io_state));
2364 SvPOK_only (data_sv); 2775 SvPOK_only (data_sv);
2365 2776
2500 XSRETURN_EMPTY; 2911 XSRETURN_EMPTY;
2501} 2912}
2502 2913
2503/*****************************************************************************/ 2914/*****************************************************************************/
2504 2915
2916#if CORO_CLONE
2917# include "clone.c"
2918#endif
2919
2505MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2920MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2506 2921
2507PROTOTYPES: DISABLE 2922PROTOTYPES: DISABLE
2508 2923
2509BOOT: 2924BOOT:
2513 coro_thx = PERL_GET_CONTEXT; 2928 coro_thx = PERL_GET_CONTEXT;
2514# endif 2929# endif
2515#endif 2930#endif
2516 BOOT_PAGESIZE; 2931 BOOT_PAGESIZE;
2517 2932
2933 cctx_current = cctx_new_empty ();
2934
2518 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2935 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2519 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2936 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2520 2937
2521 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2938 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2522 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2939 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2541 2958
2542 { 2959 {
2543 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2960 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2544 2961
2545 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2962 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2546 hv_store_ent (PL_custom_op_names, slf, 2963 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2547 newSVpv ("coro_slf", 0), 0);
2548 2964
2549 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2965 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2550 hv_store_ent (PL_custom_op_descs, slf, 2966 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2551 newSVpv ("coro schedule like function", 0), 0);
2552 } 2967 }
2553 2968
2554 coroapi.ver = CORO_API_VERSION; 2969 coroapi.ver = CORO_API_VERSION;
2555 coroapi.rev = CORO_API_REVISION; 2970 coroapi.rev = CORO_API_REVISION;
2556 2971
2570 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 2985 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2571 2986
2572 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 2987 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2573 } 2988 }
2574 2989
2575 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2990 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2576} 2991}
2577 2992
2578SV * 2993SV *
2579new (char *klass, ...) 2994new (char *klass, ...)
2995 ALIAS:
2996 Coro::new = 1
2580 CODE: 2997 CODE:
2581{ 2998{
2582 struct coro *coro; 2999 struct coro *coro;
2583 MAGIC *mg; 3000 MAGIC *mg;
2584 HV *hv; 3001 HV *hv;
3002 CV *cb;
2585 int i; 3003 int i;
3004
3005 if (items > 1)
3006 {
3007 cb = coro_sv_2cv (aTHX_ ST (1));
3008
3009 if (!ix)
3010 {
3011 if (CvISXSUB (cb))
3012 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3013
3014 if (!CvROOT (cb))
3015 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3016 }
3017 }
2586 3018
2587 Newz (0, coro, 1, struct coro); 3019 Newz (0, coro, 1, struct coro);
2588 coro->args = newAV (); 3020 coro->args = newAV ();
2589 coro->flags = CF_NEW; 3021 coro->flags = CF_NEW;
2590 3022
2595 coro->hv = hv = newHV (); 3027 coro->hv = hv = newHV ();
2596 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 3028 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2597 mg->mg_flags |= MGf_DUP; 3029 mg->mg_flags |= MGf_DUP;
2598 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 3030 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2599 3031
3032 if (items > 1)
3033 {
2600 av_extend (coro->args, items - 1); 3034 av_extend (coro->args, items - 1 + ix - 1);
3035
3036 if (ix)
3037 {
3038 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3039 cb = cv_coro_run;
3040 }
3041
3042 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3043
2601 for (i = 1; i < items; i++) 3044 for (i = 2; i < items; i++)
2602 av_push (coro->args, newSVsv (ST (i))); 3045 av_push (coro->args, newSVsv (ST (i)));
3046 }
2603} 3047}
2604 OUTPUT: 3048 OUTPUT:
2605 RETVAL 3049 RETVAL
2606 3050
2607void 3051void
2620void 3064void
2621_exit (int code) 3065_exit (int code)
2622 PROTOTYPE: $ 3066 PROTOTYPE: $
2623 CODE: 3067 CODE:
2624 _exit (code); 3068 _exit (code);
3069
3070SV *
3071clone (Coro::State coro)
3072 CODE:
3073{
3074#if CORO_CLONE
3075 struct coro *ncoro = coro_clone (aTHX_ coro);
3076 MAGIC *mg;
3077 /* TODO: too much duplication */
3078 ncoro->hv = newHV ();
3079 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3080 mg->mg_flags |= MGf_DUP;
3081 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3082#else
3083 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3084#endif
3085}
3086 OUTPUT:
3087 RETVAL
2625 3088
2626int 3089int
2627cctx_stacksize (int new_stacksize = 0) 3090cctx_stacksize (int new_stacksize = 0)
2628 PROTOTYPE: ;$ 3091 PROTOTYPE: ;$
2629 CODE: 3092 CODE:
2679 eval = 1 3142 eval = 1
2680 CODE: 3143 CODE:
2681{ 3144{
2682 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3145 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2683 { 3146 {
2684 struct coro temp; 3147 struct coro *current = SvSTATE_current;
2685 3148
2686 if (!(coro->flags & CF_RUNNING)) 3149 if (current != coro)
2687 { 3150 {
2688 PUTBACK; 3151 PUTBACK;
2689 save_perl (aTHX_ &temp); 3152 save_perl (aTHX_ current);
2690 load_perl (aTHX_ coro); 3153 load_perl (aTHX_ coro);
3154 SPAGAIN;
2691 } 3155 }
2692 3156
2693 {
2694 dSP;
2695 ENTER;
2696 SAVETMPS;
2697 PUTBACK;
2698 PUSHSTACK; 3157 PUSHSTACK;
3158
2699 PUSHMARK (SP); 3159 PUSHMARK (SP);
3160 PUTBACK;
2700 3161
2701 if (ix) 3162 if (ix)
2702 eval_sv (coderef, 0); 3163 eval_sv (coderef, 0);
2703 else 3164 else
2704 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3165 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2705 3166
2706 POPSTACK; 3167 POPSTACK;
2707 SPAGAIN; 3168 SPAGAIN;
2708 FREETMPS;
2709 LEAVE;
2710 PUTBACK;
2711 }
2712 3169
2713 if (!(coro->flags & CF_RUNNING)) 3170 if (current != coro)
2714 { 3171 {
3172 PUTBACK;
2715 save_perl (aTHX_ coro); 3173 save_perl (aTHX_ coro);
2716 load_perl (aTHX_ &temp); 3174 load_perl (aTHX_ current);
2717 SPAGAIN; 3175 SPAGAIN;
2718 } 3176 }
2719 } 3177 }
2720} 3178}
2721 3179
2725 ALIAS: 3183 ALIAS:
2726 is_ready = CF_READY 3184 is_ready = CF_READY
2727 is_running = CF_RUNNING 3185 is_running = CF_RUNNING
2728 is_new = CF_NEW 3186 is_new = CF_NEW
2729 is_destroyed = CF_DESTROYED 3187 is_destroyed = CF_DESTROYED
3188 is_suspended = CF_SUSPENDED
2730 CODE: 3189 CODE:
2731 RETVAL = boolSV (coro->flags & ix); 3190 RETVAL = boolSV (coro->flags & ix);
2732 OUTPUT: 3191 OUTPUT:
2733 RETVAL 3192 RETVAL
2734 3193
2738 CODE: 3197 CODE:
2739{ 3198{
2740 struct coro *current = SvSTATE_current; 3199 struct coro *current = SvSTATE_current;
2741 SV **throwp = self == current ? &CORO_THROW : &self->except; 3200 SV **throwp = self == current ? &CORO_THROW : &self->except;
2742 SvREFCNT_dec (*throwp); 3201 SvREFCNT_dec (*throwp);
3202 SvGETMAGIC (throw);
2743 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3203 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2744} 3204}
2745 3205
2746void 3206void
2747api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3207api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2750 3210
2751SV * 3211SV *
2752has_cctx (Coro::State coro) 3212has_cctx (Coro::State coro)
2753 PROTOTYPE: $ 3213 PROTOTYPE: $
2754 CODE: 3214 CODE:
2755 RETVAL = boolSV (!!coro->cctx); 3215 /* maybe manage the running flag differently */
3216 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2756 OUTPUT: 3217 OUTPUT:
2757 RETVAL 3218 RETVAL
2758 3219
2759int 3220int
2760is_traced (Coro::State coro) 3221is_traced (Coro::State coro)
2780 3241
2781void 3242void
2782force_cctx () 3243force_cctx ()
2783 PROTOTYPE: 3244 PROTOTYPE:
2784 CODE: 3245 CODE:
2785 SvSTATE_current->cctx->idle_sp = 0; 3246 cctx_current->idle_sp = 0;
2786 3247
2787void 3248void
2788swap_defsv (Coro::State self) 3249swap_defsv (Coro::State self)
2789 PROTOTYPE: $ 3250 PROTOTYPE: $
2790 ALIAS: 3251 ALIAS:
2798 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3259 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2799 3260
2800 SV *tmp = *src; *src = *dst; *dst = tmp; 3261 SV *tmp = *src; *src = *dst; *dst = tmp;
2801 } 3262 }
2802 3263
3264void
3265cancel (Coro::State self)
3266 CODE:
3267 coro_state_destroy (aTHX_ self);
3268 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3269
2803 3270
2804MODULE = Coro::State PACKAGE = Coro 3271MODULE = Coro::State PACKAGE = Coro
2805 3272
2806BOOT: 3273BOOT:
2807{ 3274{
2808 int i;
2809
2810 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2811 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3275 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2812 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3276 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2813 3277 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3278 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2814 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 3279 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2815 SvREADONLY_on (coro_current); 3280 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3281 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3282 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3283 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3284
3285 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3286 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3287 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3288 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2816 3289
2817 coro_stash = gv_stashpv ("Coro", TRUE); 3290 coro_stash = gv_stashpv ("Coro", TRUE);
2818 3291
2819 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3292 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
2820 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3293 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
2821 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3294 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
2822 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3295 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW));
2823 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3296 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE));
2824 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3297 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN));
2825
2826 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2827 coro_ready[i] = newAV ();
2828 3298
2829 { 3299 {
2830 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3300 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2831 3301
2832 coroapi.schedule = api_schedule; 3302 coroapi.schedule = api_schedule;
3303 coroapi.schedule_to = api_schedule_to;
2833 coroapi.cede = api_cede; 3304 coroapi.cede = api_cede;
2834 coroapi.cede_notself = api_cede_notself; 3305 coroapi.cede_notself = api_cede_notself;
2835 coroapi.ready = api_ready; 3306 coroapi.ready = api_ready;
2836 coroapi.is_ready = api_is_ready; 3307 coroapi.is_ready = api_is_ready;
2837 coroapi.nready = coro_nready; 3308 coroapi.nready = coro_nready;
2842 SvREADONLY_on (sv); 3313 SvREADONLY_on (sv);
2843 } 3314 }
2844} 3315}
2845 3316
2846void 3317void
3318terminate (...)
3319 CODE:
3320 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3321
3322void
2847schedule (...) 3323schedule (...)
2848 CODE: 3324 CODE:
2849 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3325 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3326
3327void
3328schedule_to (...)
3329 CODE:
3330 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3331
3332void
3333cede_to (...)
3334 CODE:
3335 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
2850 3336
2851void 3337void
2852cede (...) 3338cede (...)
2853 CODE: 3339 CODE:
2854 CORO_EXECUTE_SLF_XS (slf_init_cede); 3340 CORO_EXECUTE_SLF_XS (slf_init_cede);
2868void 3354void
2869_set_readyhook (SV *hook) 3355_set_readyhook (SV *hook)
2870 PROTOTYPE: $ 3356 PROTOTYPE: $
2871 CODE: 3357 CODE:
2872 SvREFCNT_dec (coro_readyhook); 3358 SvREFCNT_dec (coro_readyhook);
3359 SvGETMAGIC (hook);
2873 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3360 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2874 3361
2875int 3362int
2876prio (Coro::State coro, int newprio = 0) 3363prio (Coro::State coro, int newprio = 0)
2877 PROTOTYPE: $;$ 3364 PROTOTYPE: $;$
2884 if (items > 1) 3371 if (items > 1)
2885 { 3372 {
2886 if (ix) 3373 if (ix)
2887 newprio = coro->prio - newprio; 3374 newprio = coro->prio - newprio;
2888 3375
2889 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 3376 if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN;
2890 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 3377 if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX;
2891 3378
2892 coro->prio = newprio; 3379 coro->prio = newprio;
2893 } 3380 }
2894} 3381}
2895 OUTPUT: 3382 OUTPUT:
2909 CODE: 3396 CODE:
2910 RETVAL = coro_nready; 3397 RETVAL = coro_nready;
2911 OUTPUT: 3398 OUTPUT:
2912 RETVAL 3399 RETVAL
2913 3400
2914# for async_pool speedup
2915void 3401void
2916_pool_1 (SV *cb) 3402suspend (Coro::State self)
3403 PROTOTYPE: $
2917 CODE: 3404 CODE:
2918{ 3405 self->flags |= CF_SUSPENDED;
2919 HV *hv = (HV *)SvRV (coro_current);
2920 struct coro *coro = SvSTATE_hv ((SV *)hv);
2921 AV *defav = GvAV (PL_defgv);
2922 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2923 AV *invoke_av;
2924 int i, len;
2925 3406
2926 if (!invoke) 3407void
3408resume (Coro::State self)
3409 PROTOTYPE: $
3410 CODE:
3411 self->flags &= ~CF_SUSPENDED;
3412
3413void
3414_pool_handler (...)
3415 CODE:
3416 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3417
3418void
3419async_pool (SV *cv, ...)
3420 PROTOTYPE: &@
3421 PPCODE:
3422{
3423 HV *hv = (HV *)av_pop (av_async_pool);
3424 AV *av = newAV ();
3425 SV *cb = ST (0);
3426 int i;
3427
3428 av_extend (av, items - 2);
3429 for (i = 1; i < items; ++i)
3430 av_push (av, SvREFCNT_inc_NN (ST (i)));
3431
3432 if ((SV *)hv == &PL_sv_undef)
2927 { 3433 {
2928 SV *old = PL_diehook; 3434 PUSHMARK (SP);
2929 PL_diehook = 0; 3435 EXTEND (SP, 2);
2930 SvREFCNT_dec (old); 3436 PUSHs (sv_Coro);
2931 croak ("\3async_pool terminate\2\n"); 3437 PUSHs ((SV *)cv_pool_handler);
3438 PUTBACK;
3439 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3440 SPAGAIN;
3441
3442 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2932 } 3443 }
2933 3444
2934 SvREFCNT_dec (coro->saved_deffh);
2935 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2936
2937 hv_store (hv, "desc", sizeof ("desc") - 1,
2938 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2939
2940 invoke_av = (AV *)SvRV (invoke);
2941 len = av_len (invoke_av);
2942
2943 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2944
2945 if (len > 0)
2946 { 3445 {
2947 av_fill (defav, len - 1); 3446 struct coro *coro = SvSTATE_hv (hv);
2948 for (i = 0; i < len; ++i) 3447
2949 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3448 assert (!coro->invoke_cb);
3449 assert (!coro->invoke_av);
3450 coro->invoke_cb = SvREFCNT_inc (cb);
3451 coro->invoke_av = av;
2950 } 3452 }
2951}
2952 3453
2953void 3454 api_ready (aTHX_ (SV *)hv);
2954_pool_2 (SV *cb)
2955 CODE:
2956{
2957 HV *hv = (HV *)SvRV (coro_current);
2958 struct coro *coro = SvSTATE_hv ((SV *)hv);
2959 3455
2960 sv_setsv (cb, &PL_sv_undef); 3456 if (GIMME_V != G_VOID)
2961 3457 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
2962 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3458 else
2963 coro->saved_deffh = 0;
2964
2965 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2966 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2967 {
2968 SV *old = PL_diehook;
2969 PL_diehook = 0;
2970 SvREFCNT_dec (old); 3459 SvREFCNT_dec (hv);
2971 croak ("\3async_pool terminate\2\n");
2972 }
2973
2974 av_clear (GvAV (PL_defgv));
2975 hv_store (hv, "desc", sizeof ("desc") - 1,
2976 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2977
2978 coro->prio = 0;
2979
2980 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2981 api_trace (aTHX_ coro_current, 0);
2982
2983 av_push (av_async_pool, newSVsv (coro_current));
2984} 3460}
2985 3461
2986SV * 3462SV *
2987rouse_cb () 3463rouse_cb ()
2988 PROTOTYPE: 3464 PROTOTYPE:
2990 RETVAL = coro_new_rouse_cb (aTHX); 3466 RETVAL = coro_new_rouse_cb (aTHX);
2991 OUTPUT: 3467 OUTPUT:
2992 RETVAL 3468 RETVAL
2993 3469
2994void 3470void
2995rouse_wait (SV *cb = 0) 3471rouse_wait (...)
2996 PROTOTYPE: ;$ 3472 PROTOTYPE: ;$
2997 PPCODE: 3473 PPCODE:
2998 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3474 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2999 3475
3476void
3477on_enter (SV *block)
3478 ALIAS:
3479 on_leave = 1
3480 PROTOTYPE: &
3481 CODE:
3482{
3483 struct coro *coro = SvSTATE_current;
3484 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3485
3486 block = (SV *)coro_sv_2cv (aTHX_ block);
3487
3488 if (!*avp)
3489 *avp = newAV ();
3490
3491 av_push (*avp, SvREFCNT_inc (block));
3492
3493 if (!ix)
3494 on_enterleave_call (aTHX_ block);
3495
3496 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3497 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3498 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3499}
3500
3000 3501
3001MODULE = Coro::State PACKAGE = PerlIO::cede 3502MODULE = Coro::State PACKAGE = PerlIO::cede
3002 3503
3003BOOT: 3504BOOT:
3004 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3505 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3007MODULE = Coro::State PACKAGE = Coro::Semaphore 3508MODULE = Coro::State PACKAGE = Coro::Semaphore
3008 3509
3009SV * 3510SV *
3010new (SV *klass, SV *count = 0) 3511new (SV *klass, SV *count = 0)
3011 CODE: 3512 CODE:
3513{
3514 int semcnt = 1;
3515
3516 if (count)
3517 {
3518 SvGETMAGIC (count);
3519
3520 if (SvOK (count))
3521 semcnt = SvIV (count);
3522 }
3523
3012 RETVAL = sv_bless ( 3524 RETVAL = sv_bless (
3013 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), 3525 coro_waitarray_new (aTHX_ semcnt),
3014 GvSTASH (CvGV (cv)) 3526 GvSTASH (CvGV (cv))
3015 ); 3527 );
3528}
3016 OUTPUT: 3529 OUTPUT:
3017 RETVAL 3530 RETVAL
3018 3531
3019# helper for Coro::Channel 3532# helper for Coro::Channel and others
3020SV * 3533SV *
3021_alloc (int count) 3534_alloc (int count)
3022 CODE: 3535 CODE:
3023 RETVAL = coro_waitarray_new (aTHX_ count); 3536 RETVAL = coro_waitarray_new (aTHX_ count);
3024 OUTPUT: 3537 OUTPUT:
3037 adjust = 1 3550 adjust = 1
3038 CODE: 3551 CODE:
3039 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3552 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
3040 3553
3041void 3554void
3042down (SV *self) 3555down (...)
3043 CODE: 3556 CODE:
3044 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3557 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3045 3558
3046void 3559void
3047wait (SV *self) 3560wait (...)
3048 CODE: 3561 CODE:
3049 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); 3562 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
3050 3563
3051void 3564void
3052try (SV *self) 3565try (SV *self)
3082 for (i = 1; i <= wcount; ++i) 3595 for (i = 1; i <= wcount; ++i)
3083 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3596 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3084 } 3597 }
3085} 3598}
3086 3599
3600MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3601
3602void
3603_may_delete (SV *sem, int count, int extra_refs)
3604 PPCODE:
3605{
3606 AV *av = (AV *)SvRV (sem);
3607
3608 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3609 && AvFILLp (av) == 0 /* no waiters, just count */
3610 && SvIV (AvARRAY (av)[0]) == count)
3611 XSRETURN_YES;
3612
3613 XSRETURN_NO;
3614}
3615
3087MODULE = Coro::State PACKAGE = Coro::Signal 3616MODULE = Coro::State PACKAGE = Coro::Signal
3088 3617
3089SV * 3618SV *
3090new (SV *klass) 3619new (SV *klass)
3091 CODE: 3620 CODE:
3095 ); 3624 );
3096 OUTPUT: 3625 OUTPUT:
3097 RETVAL 3626 RETVAL
3098 3627
3099void 3628void
3100wait (SV *self) 3629wait (...)
3101 CODE: 3630 CODE:
3102 CORO_EXECUTE_SLF_XS (slf_init_signal_wait); 3631 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3103 3632
3104void 3633void
3105broadcast (SV *self) 3634broadcast (SV *self)
3162 3691
3163void 3692void
3164_register (char *target, char *proto, SV *req) 3693_register (char *target, char *proto, SV *req)
3165 CODE: 3694 CODE:
3166{ 3695{
3167 HV *st; 3696 CV *req_cv = coro_sv_2cv (aTHX_ req);
3168 GV *gvp;
3169 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
3170 /* newXSproto doesn't return the CV on 5.8 */ 3697 /* newXSproto doesn't return the CV on 5.8 */
3171 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3698 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3172 sv_setpv ((SV *)slf_cv, proto); 3699 sv_setpv ((SV *)slf_cv, proto);
3173 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3700 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3174} 3701}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines