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.302 by root, Wed Nov 19 04:48:24 2008 UTC vs.
Revision 1.346 by root, Tue Jun 9 18:56:45 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
167static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 174static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
181
172static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 184static SV *rv_diehook;
175static SV *rv_warnhook; 185static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 186static HV *hv_sig; /* %SIG */
177 187
178/* async_pool helper stuff */ 188/* async_pool helper stuff */
179static SV *sv_pool_rss; 189static SV *sv_pool_rss;
180static SV *sv_pool_size; 190static SV *sv_pool_size;
191static SV *sv_async_pool_idle; /* description string */
181static AV *av_async_pool; 192static AV *av_async_pool; /* idle pool */
193static SV *sv_Coro; /* class string */
194static CV *cv_pool_handler;
195static CV *cv_coro_state_new;
182 196
183/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
184static SV *sv_activity; 198static SV *sv_activity;
185 199
186static struct coro_cctx *cctx_first; 200static struct coro_cctx *cctx_first;
215 int valgrind_id; 229 int valgrind_id;
216#endif 230#endif
217 unsigned char flags; 231 unsigned char flags;
218} coro_cctx; 232} coro_cctx;
219 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
220enum { 238enum {
221 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
222 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
223 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
224 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
243 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
225}; 244};
226 245
227/* the structure where most of the perl state is stored, overlaid on the cxstack */ 246/* the structure where most of the perl state is stored, overlaid on the cxstack */
228typedef struct 247typedef struct
229{ 248{
230 SV *defsv; 249 SV *defsv;
231 AV *defav; 250 AV *defav;
232 SV *errsv; 251 SV *errsv;
233 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
234#define VAR(name,type) type name; 254#define VAR(name,type) type name;
235# include "state.h" 255# include "state.h"
236#undef VAR 256#undef VAR
237} perl_slots; 257} perl_slots;
238 258
246 /* state data */ 266 /* state data */
247 struct CoroSLF slf_frame; /* saved slf frame */ 267 struct CoroSLF slf_frame; /* saved slf frame */
248 AV *mainstack; 268 AV *mainstack;
249 perl_slots *slot; /* basically the saved sp */ 269 perl_slots *slot; /* basically the saved sp */
250 270
271 CV *startcv; /* the CV to execute */
251 AV *args; /* data associated with this coroutine (initial args) */ 272 AV *args; /* data associated with this coroutine (initial args) */
252 int refcnt; /* coroutines are refcounted, yes */ 273 int refcnt; /* coroutines are refcounted, yes */
253 int flags; /* CF_ flags */ 274 int flags; /* CF_ flags */
254 HV *hv; /* the perl hash associated with this coro, if any */ 275 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro); 276 void (*on_destroy)(pTHX_ struct coro *coro);
256 277
257 /* statistics */ 278 /* statistics */
258 int usecount; /* number of transfers to this coro */ 279 int usecount; /* number of transfers to this coro */
259 280
262 SV *except; /* exception to be thrown */ 283 SV *except; /* exception to be thrown */
263 SV *rouse_cb; 284 SV *rouse_cb;
264 285
265 /* async_pool */ 286 /* async_pool */
266 SV *saved_deffh; 287 SV *saved_deffh;
288 SV *invoke_cb;
289 AV *invoke_av;
290
291 /* on_enter/on_leave */
292 AV *on_enter;
293 AV *on_leave;
267 294
268 /* linked list */ 295 /* linked list */
269 struct coro *next, *prev; 296 struct coro *next, *prev;
270}; 297};
271 298
288 315
289/* for Coro.pm */ 316/* for Coro.pm */
290static SV *coro_current; 317static SV *coro_current;
291static SV *coro_readyhook; 318static SV *coro_readyhook;
292static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 319static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
320static CV *cv_coro_run, *cv_coro_terminate;
293static struct coro *coro_first; 321static struct coro *coro_first;
294#define coro_nready coroapi.nready 322#define coro_nready coroapi.nready
295 323
296/** lowlevel stuff **********************************************************/ 324/** lowlevel stuff **********************************************************/
297 325
323 get_hv (name, create); 351 get_hv (name, create);
324#endif 352#endif
325 return get_hv (name, create); 353 return get_hv (name, create);
326} 354}
327 355
356/* may croak */
357INLINE CV *
358coro_sv_2cv (pTHX_ SV *sv)
359{
360 HV *st;
361 GV *gvp;
362 CV *cv = sv_2cv (sv, &st, &gvp, 0);
363
364 if (!cv)
365 croak ("code reference expected");
366
367 return cv;
368}
369
370/*****************************************************************************/
371/* magic glue */
372
373#define CORO_MAGIC_type_cv 26
374#define CORO_MAGIC_type_state PERL_MAGIC_ext
375
376#define CORO_MAGIC_NN(sv, type) \
377 (expect_true (SvMAGIC (sv)->mg_type == type) \
378 ? SvMAGIC (sv) \
379 : mg_find (sv, type))
380
381#define CORO_MAGIC(sv, type) \
382 (expect_true (SvMAGIC (sv)) \
383 ? CORO_MAGIC_NN (sv, type) \
384 : 0)
385
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
388
389INLINE struct coro *
390SvSTATE_ (pTHX_ SV *coro)
391{
392 HV *stash;
393 MAGIC *mg;
394
395 if (SvROK (coro))
396 coro = SvRV (coro);
397
398 if (expect_false (SvTYPE (coro) != SVt_PVHV))
399 croak ("Coro::State object required");
400
401 stash = SvSTASH (coro);
402 if (expect_false (stash != coro_stash && stash != coro_state_stash))
403 {
404 /* very slow, but rare, check */
405 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
406 croak ("Coro::State object required");
407 }
408
409 mg = CORO_MAGIC_state (coro);
410 return (struct coro *)mg->mg_ptr;
411}
412
413#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
414
415/* faster than SvSTATE, but expects a coroutine hv */
416#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
417#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
418
419/*****************************************************************************/
420/* padlist management and caching */
421
328static AV * 422static AV *
329coro_clone_padlist (pTHX_ CV *cv) 423coro_derive_padlist (pTHX_ CV *cv)
330{ 424{
331 AV *padlist = CvPADLIST (cv); 425 AV *padlist = CvPADLIST (cv);
332 AV *newpadlist, *newpad; 426 AV *newpadlist, *newpad;
333 427
334 newpadlist = newAV (); 428 newpadlist = newAV ();
339 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 433 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
340#endif 434#endif
341 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 435 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
342 --AvFILLp (padlist); 436 --AvFILLp (padlist);
343 437
344 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 438 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
345 av_store (newpadlist, 1, (SV *)newpad); 439 av_store (newpadlist, 1, (SV *)newpad);
346 440
347 return newpadlist; 441 return newpadlist;
348} 442}
349 443
350static void 444static void
351free_padlist (pTHX_ AV *padlist) 445free_padlist (pTHX_ AV *padlist)
352{ 446{
353 /* may be during global destruction */ 447 /* may be during global destruction */
354 if (SvREFCNT (padlist)) 448 if (!IN_DESTRUCT)
355 { 449 {
356 I32 i = AvFILLp (padlist); 450 I32 i = AvFILLp (padlist);
357 while (i >= 0) 451
452 while (i > 0) /* special-case index 0 */
358 { 453 {
359 SV **svp = av_fetch (padlist, i--, FALSE); 454 /* we try to be extra-careful here */
360 if (svp) 455 AV *av = (AV *)AvARRAY (padlist)[i--];
361 { 456 I32 j = AvFILLp (av);
362 SV *sv; 457
363 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 458 while (j >= 0)
459 SvREFCNT_dec (AvARRAY (av)[j--]);
460
461 AvFILLp (av) = -1;
364 SvREFCNT_dec (sv); 462 SvREFCNT_dec (av);
365
366 SvREFCNT_dec (*svp);
367 }
368 } 463 }
369 464
465 SvREFCNT_dec (AvARRAY (padlist)[0]);
466
467 AvFILLp (padlist) = -1;
370 SvREFCNT_dec ((SV*)padlist); 468 SvREFCNT_dec ((SV*)padlist);
371 } 469 }
372} 470}
373 471
374static int 472static int
383 481
384 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 482 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
385 483
386 return 0; 484 return 0;
387} 485}
388
389#define CORO_MAGIC_type_cv 26
390#define CORO_MAGIC_type_state PERL_MAGIC_ext
391 486
392static MGVTBL coro_cv_vtbl = { 487static MGVTBL coro_cv_vtbl = {
393 0, 0, 0, 0, 488 0, 0, 0, 0,
394 coro_cv_free 489 coro_cv_free
395}; 490};
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 491
440/* the next two functions merely cache the padlists */ 492/* the next two functions merely cache the padlists */
441static void 493static void
442get_padlist (pTHX_ CV *cv) 494get_padlist (pTHX_ CV *cv)
443{ 495{
449 else 501 else
450 { 502 {
451#if CORO_PREFER_PERL_FUNCTIONS 503#if CORO_PREFER_PERL_FUNCTIONS
452 /* this is probably cleaner? but also slower! */ 504 /* this is probably cleaner? but also slower! */
453 /* in practise, it seems to be less stable */ 505 /* in practise, it seems to be less stable */
454 CV *cp = Perl_cv_clone (cv); 506 CV *cp = Perl_cv_clone (aTHX_ cv);
455 CvPADLIST (cv) = CvPADLIST (cp); 507 CvPADLIST (cv) = CvPADLIST (cp);
456 CvPADLIST (cp) = 0; 508 CvPADLIST (cp) = 0;
457 SvREFCNT_dec (cp); 509 SvREFCNT_dec (cp);
458#else 510#else
459 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 511 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
460#endif 512#endif
461 } 513 }
462} 514}
463 515
464static void 516static void
471 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 523 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
472 524
473 av = (AV *)mg->mg_obj; 525 av = (AV *)mg->mg_obj;
474 526
475 if (expect_false (AvFILLp (av) >= AvMAX (av))) 527 if (expect_false (AvFILLp (av) >= AvMAX (av)))
476 av_extend (av, AvMAX (av) + 1); 528 av_extend (av, AvFILLp (av) + 1);
477 529
478 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 530 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
479} 531}
480 532
481/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
534
535static void
536on_enterleave_call (pTHX_ SV *cb);
482 537
483static void 538static void
484load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
485{ 540{
486 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
487 c->slot = 0; 542 c->slot = 0;
488 543
489 PL_mainstack = c->mainstack; 544 PL_mainstack = c->mainstack;
490 545
491 GvSV (PL_defgv) = slot->defsv; 546 GvSV (PL_defgv) = slot->defsv;
492 GvAV (PL_defgv) = slot->defav; 547 GvAV (PL_defgv) = slot->defav;
493 GvSV (PL_errgv) = slot->errsv; 548 GvSV (PL_errgv) = slot->errsv;
494 GvSV (irsgv) = slot->irsgv; 549 GvSV (irsgv) = slot->irsgv;
550 GvHV (PL_hintgv) = slot->hinthv;
495 551
496 #define VAR(name,type) PL_ ## name = slot->name; 552 #define VAR(name,type) PL_ ## name = slot->name;
497 # include "state.h" 553 # include "state.h"
498 #undef VAR 554 #undef VAR
499 555
513 PUTBACK; 569 PUTBACK;
514 } 570 }
515 571
516 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
517 CORO_THROW = c->except; 573 CORO_THROW = c->except;
574
575 if (expect_false (c->on_enter))
576 {
577 int i;
578
579 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
580 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
581 }
518} 582}
519 583
520static void 584static void
521save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
522{ 586{
587 if (expect_false (c->on_leave))
588 {
589 int i;
590
591 for (i = AvFILLp (c->on_leave); i >= 0; --i)
592 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
593 }
594
523 c->except = CORO_THROW; 595 c->except = CORO_THROW;
524 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
525 597
526 { 598 {
527 dSP; 599 dSP;
585 c->mainstack = PL_mainstack; 657 c->mainstack = PL_mainstack;
586 658
587 { 659 {
588 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 660 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
589 661
590 slot->defav = GvAV (PL_defgv); 662 slot->defav = GvAV (PL_defgv);
591 slot->defsv = DEFSV; 663 slot->defsv = DEFSV;
592 slot->errsv = ERRSV; 664 slot->errsv = ERRSV;
593 slot->irsgv = GvSV (irsgv); 665 slot->irsgv = GvSV (irsgv);
666 slot->hinthv = GvHV (PL_hintgv);
594 667
595 #define VAR(name,type) slot->name = PL_ ## name; 668 #define VAR(name,type) slot->name = PL_ ## name;
596 # include "state.h" 669 # include "state.h"
597 #undef VAR 670 #undef VAR
598 } 671 }
603 * of perl.c:init_stacks, except that it uses less memory 676 * of perl.c:init_stacks, except that it uses less memory
604 * on the (sometimes correct) assumption that coroutines do 677 * on the (sometimes correct) assumption that coroutines do
605 * not usually need a lot of stackspace. 678 * not usually need a lot of stackspace.
606 */ 679 */
607#if CORO_PREFER_PERL_FUNCTIONS 680#if CORO_PREFER_PERL_FUNCTIONS
608# define coro_init_stacks init_stacks 681# define coro_init_stacks(thx) init_stacks ()
609#else 682#else
610static void 683static void
611coro_init_stacks (pTHX) 684coro_init_stacks (pTHX)
612{ 685{
613 PL_curstackinfo = new_stackinfo(32, 8); 686 PL_curstackinfo = new_stackinfo(32, 8);
676#if !PERL_VERSION_ATLEAST (5,10,0) 749#if !PERL_VERSION_ATLEAST (5,10,0)
677 Safefree (PL_retstack); 750 Safefree (PL_retstack);
678#endif 751#endif
679} 752}
680 753
754#define CORO_RSS \
755 rss += sizeof (SYM (curstackinfo)); \
756 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
757 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
758 rss += SYM (tmps_max) * sizeof (SV *); \
759 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
760 rss += SYM (scopestack_max) * sizeof (I32); \
761 rss += SYM (savestack_max) * sizeof (ANY);
762
681static size_t 763static size_t
682coro_rss (pTHX_ struct coro *coro) 764coro_rss (pTHX_ struct coro *coro)
683{ 765{
684 size_t rss = sizeof (*coro); 766 size_t rss = sizeof (*coro);
685 767
686 if (coro->mainstack) 768 if (coro->mainstack)
687 { 769 {
688 perl_slots tmp_slot;
689 perl_slots *slot;
690
691 if (coro->flags & CF_RUNNING) 770 if (coro->flags & CF_RUNNING)
692 { 771 {
693 slot = &tmp_slot; 772 #define SYM(sym) PL_ ## sym
694 773 CORO_RSS;
695 #define VAR(name,type) slot->name = PL_ ## name;
696 # include "state.h"
697 #undef VAR 774 #undef SYM
698 } 775 }
699 else 776 else
700 slot = coro->slot;
701
702 if (slot)
703 { 777 {
704 rss += sizeof (slot->curstackinfo); 778 #define SYM(sym) coro->slot->sym
705 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 779 CORO_RSS;
706 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 780 #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 } 781 }
716 } 782 }
717 783
718 return rss; 784 return rss;
719} 785}
732#endif 798#endif
733 799
734/* 800/*
735 * This overrides the default magic get method of %SIG elements. 801 * 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 802 * 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 803 * and instead of trying to save and restore the hash elements, we just provide
738 * readback here. 804 * 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 */ 805 */
743static int 806static int
744coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 807coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
745{ 808{
746 const char *s = MgPV_nolen_const (mg); 809 const char *s = MgPV_nolen_const (mg);
799 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 862 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
800 863
801 if (svp) 864 if (svp)
802 { 865 {
803 SV *old = *svp; 866 SV *old = *svp;
804 *svp = newSVsv (sv); 867 *svp = SvOK (sv) ? newSVsv (sv) : 0;
805 SvREFCNT_dec (old); 868 SvREFCNT_dec (old);
806 return 0; 869 return 0;
807 } 870 }
808 } 871 }
809 872
819 882
820static int 883static int
821slf_check_nop (pTHX_ struct CoroSLF *frame) 884slf_check_nop (pTHX_ struct CoroSLF *frame)
822{ 885{
823 return 0; 886 return 0;
887}
888
889static int
890slf_check_repeat (pTHX_ struct CoroSLF *frame)
891{
892 return 1;
824} 893}
825 894
826static UNOP coro_setup_op; 895static UNOP coro_setup_op;
827 896
828static void NOINLINE /* noinline to keep it out of the transfer fast path */ 897static void NOINLINE /* noinline to keep it out of the transfer fast path */
835 904
836 PL_runops = RUNOPS_DEFAULT; 905 PL_runops = RUNOPS_DEFAULT;
837 PL_curcop = &PL_compiling; 906 PL_curcop = &PL_compiling;
838 PL_in_eval = EVAL_NULL; 907 PL_in_eval = EVAL_NULL;
839 PL_comppad = 0; 908 PL_comppad = 0;
909 PL_comppad_name = 0;
910 PL_comppad_name_fill = 0;
911 PL_comppad_name_floor = 0;
840 PL_curpm = 0; 912 PL_curpm = 0;
841 PL_curpad = 0; 913 PL_curpad = 0;
842 PL_localizing = 0; 914 PL_localizing = 0;
843 PL_dirty = 0; 915 PL_dirty = 0;
844 PL_restartop = 0; 916 PL_restartop = 0;
845#if PERL_VERSION_ATLEAST (5,10,0) 917#if PERL_VERSION_ATLEAST (5,10,0)
846 PL_parser = 0; 918 PL_parser = 0;
847#endif 919#endif
920 PL_hints = 0;
848 921
849 /* recreate the die/warn hooks */ 922 /* recreate the die/warn hooks */
850 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 923 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); 924 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
852 925
853 GvSV (PL_defgv) = newSV (0); 926 GvSV (PL_defgv) = newSV (0);
854 GvAV (PL_defgv) = coro->args; coro->args = 0; 927 GvAV (PL_defgv) = coro->args; coro->args = 0;
855 GvSV (PL_errgv) = newSV (0); 928 GvSV (PL_errgv) = newSV (0);
856 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 929 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
930 GvHV (PL_hintgv) = 0;
857 PL_rs = newSVsv (GvSV (irsgv)); 931 PL_rs = newSVsv (GvSV (irsgv));
858 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 932 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
859 933
860 { 934 {
861 dSP; 935 dSP;
862 UNOP myop; 936 UNOP myop;
863 937
864 Zero (&myop, 1, UNOP); 938 Zero (&myop, 1, UNOP);
865 myop.op_next = Nullop; 939 myop.op_next = Nullop;
940 myop.op_type = OP_ENTERSUB;
866 myop.op_flags = OPf_WANT_VOID; 941 myop.op_flags = OPf_WANT_VOID;
867 942
868 PUSHMARK (SP); 943 PUSHMARK (SP);
869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 944 PUSHs ((SV *)coro->startcv);
870 PUTBACK; 945 PUTBACK;
871 PL_op = (OP *)&myop; 946 PL_op = (OP *)&myop;
872 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 947 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
873 SPAGAIN;
874 } 948 }
875 949
876 /* this newly created coroutine might be run on an existing cctx which most 950 /* 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. 951 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
878 */ 952 */
879 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 953 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 */ 954 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
881 955
882 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 956 /* 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; 957 coro_setup_op.op_next = PL_op;
884 coro_setup_op.op_type = OP_CUSTOM; 958 coro_setup_op.op_type = OP_ENTERSUB;
885 coro_setup_op.op_ppaddr = pp_slf; 959 coro_setup_op.op_ppaddr = pp_slf;
886 /* no flags required, as an init function won't be called */ 960 /* no flags etc. required, as an init function won't be called */
887 961
888 PL_op = (OP *)&coro_setup_op; 962 PL_op = (OP *)&coro_setup_op;
889 963
890 /* copy throw, in case it was set before coro_setup */ 964 /* copy throw, in case it was set before coro_setup */
891 CORO_THROW = coro->except; 965 CORO_THROW = coro->except;
892} 966}
893 967
894static void 968static void
895coro_destruct (pTHX_ struct coro *coro) 969coro_unwind_stacks (pTHX)
896{ 970{
897 if (!IN_DESTRUCT) 971 if (!IN_DESTRUCT)
898 { 972 {
899 /* restore all saved variables and stuff */ 973 /* restore all saved variables and stuff */
900 LEAVE_SCOPE (0); 974 LEAVE_SCOPE (0);
908 POPSTACK_TO (PL_mainstack); 982 POPSTACK_TO (PL_mainstack);
909 983
910 /* unwind main stack */ 984 /* unwind main stack */
911 dounwind (-1); 985 dounwind (-1);
912 } 986 }
987}
988
989static void
990coro_destruct_perl (pTHX_ struct coro *coro)
991{
992 coro_unwind_stacks (aTHX);
913 993
914 SvREFCNT_dec (GvSV (PL_defgv)); 994 SvREFCNT_dec (GvSV (PL_defgv));
915 SvREFCNT_dec (GvAV (PL_defgv)); 995 SvREFCNT_dec (GvAV (PL_defgv));
916 SvREFCNT_dec (GvSV (PL_errgv)); 996 SvREFCNT_dec (GvSV (PL_errgv));
917 SvREFCNT_dec (PL_defoutgv); 997 SvREFCNT_dec (PL_defoutgv);
918 SvREFCNT_dec (PL_rs); 998 SvREFCNT_dec (PL_rs);
919 SvREFCNT_dec (GvSV (irsgv)); 999 SvREFCNT_dec (GvSV (irsgv));
1000 SvREFCNT_dec (GvHV (PL_hintgv));
920 1001
921 SvREFCNT_dec (PL_diehook); 1002 SvREFCNT_dec (PL_diehook);
922 SvREFCNT_dec (PL_warnhook); 1003 SvREFCNT_dec (PL_warnhook);
923 1004
924 SvREFCNT_dec (CORO_THROW);
925 SvREFCNT_dec (coro->saved_deffh); 1005 SvREFCNT_dec (coro->saved_deffh);
926 SvREFCNT_dec (coro->rouse_cb); 1006 SvREFCNT_dec (coro->rouse_cb);
1007 SvREFCNT_dec (coro->invoke_cb);
1008 SvREFCNT_dec (coro->invoke_av);
927 1009
928 coro_destruct_stacks (aTHX); 1010 coro_destruct_stacks (aTHX);
929} 1011}
930 1012
931INLINE void 1013INLINE void
941static int 1023static int
942runops_trace (pTHX) 1024runops_trace (pTHX)
943{ 1025{
944 COP *oldcop = 0; 1026 COP *oldcop = 0;
945 int oldcxix = -2; 1027 int oldcxix = -2;
946 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
947 coro_cctx *cctx = coro->cctx;
948 1028
949 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1029 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
950 { 1030 {
951 PERL_ASYNC_CHECK (); 1031 PERL_ASYNC_CHECK ();
952 1032
953 if (cctx->flags & CC_TRACE_ALL) 1033 if (cctx_current->flags & CC_TRACE_ALL)
954 { 1034 {
955 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1035 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
956 { 1036 {
957 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1037 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
958 SV **bot, **top; 1038 SV **bot, **top;
959 AV *av = newAV (); /* return values */ 1039 AV *av = newAV (); /* return values */
960 SV **cb; 1040 SV **cb;
997 1077
998 if (PL_curcop != &PL_compiling) 1078 if (PL_curcop != &PL_compiling)
999 { 1079 {
1000 SV **cb; 1080 SV **cb;
1001 1081
1002 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1082 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1003 { 1083 {
1004 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1084 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1005 1085
1006 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1086 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1007 { 1087 {
1008 runops_proc_t old_runops = PL_runops;
1009 dSP; 1088 dSP;
1010 GV *gv = CvGV (cx->blk_sub.cv); 1089 GV *gv = CvGV (cx->blk_sub.cv);
1011 SV *fullname = sv_2mortal (newSV (0)); 1090 SV *fullname = sv_2mortal (newSV (0));
1012 1091
1013 if (isGV (gv)) 1092 if (isGV (gv))
1018 SAVETMPS; 1097 SAVETMPS;
1019 EXTEND (SP, 3); 1098 EXTEND (SP, 3);
1020 PUSHMARK (SP); 1099 PUSHMARK (SP);
1021 PUSHs (&PL_sv_yes); 1100 PUSHs (&PL_sv_yes);
1022 PUSHs (fullname); 1101 PUSHs (fullname);
1023 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1102 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1024 PUTBACK; 1103 PUTBACK;
1025 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1104 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); 1105 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1027 SPAGAIN; 1106 SPAGAIN;
1028 FREETMPS; 1107 FREETMPS;
1031 } 1110 }
1032 1111
1033 oldcxix = cxstack_ix; 1112 oldcxix = cxstack_ix;
1034 } 1113 }
1035 1114
1036 if (cctx->flags & CC_TRACE_LINE) 1115 if (cctx_current->flags & CC_TRACE_LINE)
1037 { 1116 {
1038 dSP; 1117 dSP;
1039 1118
1040 PL_runops = RUNOPS_DEFAULT; 1119 PL_runops = RUNOPS_DEFAULT;
1041 ENTER; 1120 ENTER;
1060 1139
1061 TAINT_NOT; 1140 TAINT_NOT;
1062 return 0; 1141 return 0;
1063} 1142}
1064 1143
1065static struct coro_cctx *cctx_ssl_cctx;
1066static struct CoroSLF cctx_ssl_frame; 1144static struct CoroSLF cctx_ssl_frame;
1067 1145
1068static void 1146static void
1069slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1147slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1070{ 1148{
1071 ta->prev = (struct coro *)cctx_ssl_cctx;
1072 ta->next = 0; 1149 ta->prev = 0;
1073} 1150}
1074 1151
1075static int 1152static int
1076slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1153slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1077{ 1154{
1080 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1157 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1081} 1158}
1082 1159
1083/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1160/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1084static void NOINLINE 1161static void NOINLINE
1085cctx_prepare (pTHX_ coro_cctx *cctx) 1162cctx_prepare (pTHX)
1086{ 1163{
1087 PL_top_env = &PL_start_env; 1164 PL_top_env = &PL_start_env;
1088 1165
1089 if (cctx->flags & CC_TRACE) 1166 if (cctx_current->flags & CC_TRACE)
1090 PL_runops = runops_trace; 1167 PL_runops = runops_trace;
1091 1168
1092 /* we already must be executing an SLF op, there is no other valid way 1169 /* we already must be executing an SLF op, there is no other valid way
1093 * that can lead to creation of a new cctx */ 1170 * that can lead to creation of a new cctx */
1094 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1171 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1095 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1172 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1096 1173
1097 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1174 /* 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; 1175 cctx_ssl_frame = slf_frame;
1100 1176
1101 slf_frame.prepare = slf_prepare_set_stacklevel; 1177 slf_frame.prepare = slf_prepare_set_stacklevel;
1102 slf_frame.check = slf_check_set_stacklevel; 1178 slf_frame.check = slf_check_set_stacklevel;
1103} 1179}
1126 /* normally we would need to skip the entersub here */ 1202 /* normally we would need to skip the entersub here */
1127 /* not doing so will re-execute it, which is exactly what we want */ 1203 /* not doing so will re-execute it, which is exactly what we want */
1128 /* PL_nop = PL_nop->op_next */ 1204 /* PL_nop = PL_nop->op_next */
1129 1205
1130 /* inject a fake subroutine call to cctx_init */ 1206 /* inject a fake subroutine call to cctx_init */
1131 cctx_prepare (aTHX_ (coro_cctx *)arg); 1207 cctx_prepare (aTHX);
1132 1208
1133 /* cctx_run is the alternative tail of transfer() */ 1209 /* cctx_run is the alternative tail of transfer() */
1134 transfer_tail (aTHX); 1210 transfer_tail (aTHX);
1135 1211
1136 /* somebody or something will hit me for both perl_run and PL_restartop */ 1212 /* somebody or something will hit me for both perl_run and PL_restartop */
1137 PL_restartop = PL_op; 1213 PL_restartop = PL_op;
1138 perl_run (PL_curinterp); 1214 perl_run (PL_curinterp);
1215 /*
1216 * Unfortunately, there is no way to get at the return values of the
1217 * coro body here, as perl_run destroys these
1218 */
1139 1219
1140 /* 1220 /*
1141 * If perl-run returns we assume exit() was being called or the coro 1221 * 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) 1222 * 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 1223 * 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" 1224 * bootstrap-time "top" top_env, as we cannot restore the "main"
1145 * coroutine as Coro has no such concept 1225 * coroutine as Coro has no such concept.
1226 * This actually isn't valid with the pthread backend, but OSes requiring
1227 * that backend are too broken to do it in a standards-compliant way.
1146 */ 1228 */
1147 PL_top_env = main_top_env; 1229 PL_top_env = main_top_env;
1148 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1230 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1149 } 1231 }
1150} 1232}
1227cctx_destroy (coro_cctx *cctx) 1309cctx_destroy (coro_cctx *cctx)
1228{ 1310{
1229 if (!cctx) 1311 if (!cctx)
1230 return; 1312 return;
1231 1313
1314 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1315
1232 --cctx_count; 1316 --cctx_count;
1233 coro_destroy (&cctx->cctx); 1317 coro_destroy (&cctx->cctx);
1234 1318
1235 /* coro_transfer creates new, empty cctx's */ 1319 /* coro_transfer creates new, empty cctx's */
1236 if (cctx->sptr) 1320 if (cctx->sptr)
1299 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1300 1384
1301 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1302 { 1386 {
1303 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 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,"); 1388 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1305 1389
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)) 1390 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,"); 1391 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1311 1392
1312#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1313 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 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,"); 1395 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1315#endif 1396#endif
1321transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1402transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1322{ 1403{
1323 dSTACKLEVEL; 1404 dSTACKLEVEL;
1324 1405
1325 /* sometimes transfer is only called to set idle_sp */ 1406 /* sometimes transfer is only called to set idle_sp */
1326 if (expect_false (!next)) 1407 if (expect_false (!prev))
1327 { 1408 {
1328 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1409 cctx_current->idle_sp = STACKLEVEL;
1329 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1410 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1330 } 1411 }
1331 else if (expect_true (prev != next)) 1412 else if (expect_true (prev != next))
1332 { 1413 {
1333 coro_cctx *prev__cctx; 1414 coro_cctx *cctx_prev;
1334 1415
1335 if (expect_false (prev->flags & CF_NEW)) 1416 if (expect_false (prev->flags & CF_NEW))
1336 { 1417 {
1337 /* create a new empty/source context */ 1418 /* create a new empty/source context */
1338 prev->cctx = cctx_new_empty ();
1339 prev->flags &= ~CF_NEW; 1419 prev->flags &= ~CF_NEW;
1340 prev->flags |= CF_RUNNING; 1420 prev->flags |= CF_RUNNING;
1341 } 1421 }
1342 1422
1343 prev->flags &= ~CF_RUNNING; 1423 prev->flags &= ~CF_RUNNING;
1354 coro_setup (aTHX_ next); 1434 coro_setup (aTHX_ next);
1355 } 1435 }
1356 else 1436 else
1357 load_perl (aTHX_ next); 1437 load_perl (aTHX_ next);
1358 1438
1359 prev__cctx = prev->cctx;
1360
1361 /* possibly untie and reuse the cctx */ 1439 /* possibly untie and reuse the cctx */
1362 if (expect_true ( 1440 if (expect_true (
1363 prev__cctx->idle_sp == STACKLEVEL 1441 cctx_current->idle_sp == STACKLEVEL
1364 && !(prev__cctx->flags & CC_TRACE) 1442 && !(cctx_current->flags & CC_TRACE)
1365 && !force_cctx 1443 && !force_cctx
1366 )) 1444 ))
1367 { 1445 {
1368 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1446 /* 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)); 1447 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1370 1448
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 */ 1449 /* 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 */ 1450 /* without this the next cctx_get might destroy the running cctx while still in use */
1375 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1451 if (expect_false (CCTX_EXPIRED (cctx_current)))
1376 if (!next->cctx) 1452 if (expect_true (!next->cctx))
1377 next->cctx = cctx_get (aTHX); 1453 next->cctx = cctx_get (aTHX);
1378 1454
1379 cctx_put (prev__cctx); 1455 cctx_put (cctx_current);
1380 } 1456 }
1457 else
1458 prev->cctx = cctx_current;
1381 1459
1382 ++next->usecount; 1460 ++next->usecount;
1383 1461
1384 if (expect_true (!next->cctx)) 1462 cctx_prev = cctx_current;
1385 next->cctx = cctx_get (aTHX); 1463 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1386 1464
1387 if (expect_false (prev__cctx != next->cctx)) 1465 next->cctx = 0;
1466
1467 if (expect_false (cctx_prev != cctx_current))
1388 { 1468 {
1389 prev__cctx->top_env = PL_top_env; 1469 cctx_prev->top_env = PL_top_env;
1390 PL_top_env = next->cctx->top_env; 1470 PL_top_env = cctx_current->top_env;
1391 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1471 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1392 } 1472 }
1393 1473
1394 transfer_tail (aTHX); 1474 transfer_tail (aTHX);
1395 } 1475 }
1396} 1476}
1404coro_state_destroy (pTHX_ struct coro *coro) 1484coro_state_destroy (pTHX_ struct coro *coro)
1405{ 1485{
1406 if (coro->flags & CF_DESTROYED) 1486 if (coro->flags & CF_DESTROYED)
1407 return 0; 1487 return 0;
1408 1488
1409 if (coro->on_destroy) 1489 if (coro->on_destroy && !PL_dirty)
1410 coro->on_destroy (aTHX_ coro); 1490 coro->on_destroy (aTHX_ coro);
1411 1491
1412 coro->flags |= CF_DESTROYED; 1492 coro->flags |= CF_DESTROYED;
1413 1493
1414 if (coro->flags & CF_READY) 1494 if (coro->flags & CF_READY)
1418 --coro_nready; 1498 --coro_nready;
1419 } 1499 }
1420 else 1500 else
1421 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1501 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1422 1502
1423 if (coro->mainstack && coro->mainstack != main_mainstack) 1503 if (coro->mainstack
1504 && coro->mainstack != main_mainstack
1505 && coro->slot
1506 && !PL_dirty)
1424 { 1507 {
1425 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1426 1509
1427 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1428 1511
1429 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1430 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1431 1514
1432 coro_destruct (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1433 1516
1434 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1435 1518
1436 coro->slot = 0; 1519 coro->slot = 0;
1437 } 1520 }
1438 1521
1439 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1523 SvREFCNT_dec (coro->startcv);
1440 SvREFCNT_dec (coro->args); 1524 SvREFCNT_dec (coro->args);
1525 SvREFCNT_dec (CORO_THROW);
1441 1526
1442 if (coro->next) coro->next->prev = coro->prev; 1527 if (coro->next) coro->next->prev = coro->prev;
1443 if (coro->prev) coro->prev->next = coro->next; 1528 if (coro->prev) coro->prev->next = coro->next;
1444 if (coro == coro_first) coro_first = coro->next; 1529 if (coro == coro_first) coro_first = coro->next;
1445 1530
1549{ 1634{
1550 struct coro *coro; 1635 struct coro *coro;
1551 SV *sv_hook; 1636 SV *sv_hook;
1552 void (*xs_hook)(void); 1637 void (*xs_hook)(void);
1553 1638
1554 if (SvROK (coro_sv))
1555 coro_sv = SvRV (coro_sv);
1556
1557 coro = SvSTATE (coro_sv); 1639 coro = SvSTATE (coro_sv);
1558 1640
1559 if (coro->flags & CF_READY) 1641 if (coro->flags & CF_READY)
1560 return 0; 1642 return 0;
1561 1643
1592api_is_ready (pTHX_ SV *coro_sv) 1674api_is_ready (pTHX_ SV *coro_sv)
1593{ 1675{
1594 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1676 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1595} 1677}
1596 1678
1679/* expects to own a reference to next->hv */
1597INLINE void 1680INLINE void
1598prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1681prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1599{ 1682{
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); 1683 SV *prev_sv = SvRV (coro_current);
1684
1639 ta->prev = SvSTATE_hv (prev_sv); 1685 ta->prev = SvSTATE_hv (prev_sv);
1686 ta->next = next;
1687
1640 TRANSFER_CHECK (*ta); 1688 TRANSFER_CHECK (*ta);
1641 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1689
1642 ta->next->flags &= ~CF_READY;
1643 SvRV_set (coro_current, next_sv); 1690 SvRV_set (coro_current, (SV *)next->hv);
1644 1691
1645 free_coro_mortal (aTHX); 1692 free_coro_mortal (aTHX);
1646 coro_mortal = prev_sv; 1693 coro_mortal = prev_sv;
1647} 1694}
1648 1695
1696static void
1697prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1698{
1699 for (;;)
1700 {
1701 SV *next_sv = coro_deq (aTHX);
1702
1703 if (expect_true (next_sv))
1704 {
1705 struct coro *next = SvSTATE_hv (next_sv);
1706
1707 /* cannot transfer to destroyed coros, skip and look for next */
1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1710 else
1711 {
1712 next->flags &= ~CF_READY;
1713 --coro_nready;
1714
1715 prepare_schedule_to (aTHX_ ta, next);
1716 break;
1717 }
1718 }
1719 else
1720 {
1721 /* nothing to schedule: call the idle handler */
1722 if (SvROK (sv_idle)
1723 && SvOBJECT (SvRV (sv_idle)))
1724 {
1725 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1726 api_ready (aTHX_ SvRV (sv_idle));
1727 --coro_nready;
1728 }
1729 else
1730 {
1731 dSP;
1732
1733 ENTER;
1734 SAVETMPS;
1735
1736 PUSHMARK (SP);
1737 PUTBACK;
1738 call_sv (sv_idle, G_VOID | G_DISCARD);
1739
1740 FREETMPS;
1741 LEAVE;
1742 }
1743 }
1744 }
1745}
1746
1649INLINE void 1747INLINE void
1650prepare_cede (pTHX_ struct coro_transfer_args *ta) 1748prepare_cede (pTHX_ struct coro_transfer_args *ta)
1651{ 1749{
1652 api_ready (aTHX_ coro_current); 1750 api_ready (aTHX_ coro_current);
1653 prepare_schedule (aTHX_ ta); 1751 prepare_schedule (aTHX_ ta);
1672{ 1770{
1673 struct coro_transfer_args ta; 1771 struct coro_transfer_args ta;
1674 1772
1675 prepare_schedule (aTHX_ &ta); 1773 prepare_schedule (aTHX_ &ta);
1676 TRANSFER (ta, 1); 1774 TRANSFER (ta, 1);
1775}
1776
1777static void
1778api_schedule_to (pTHX_ SV *coro_sv)
1779{
1780 struct coro_transfer_args ta;
1781 struct coro *next = SvSTATE (coro_sv);
1782
1783 SvREFCNT_inc_NN (coro_sv);
1784 prepare_schedule_to (aTHX_ &ta, next);
1677} 1785}
1678 1786
1679static int 1787static int
1680api_cede (pTHX) 1788api_cede (pTHX)
1681{ 1789{
1710static void 1818static void
1711api_trace (pTHX_ SV *coro_sv, int flags) 1819api_trace (pTHX_ SV *coro_sv, int flags)
1712{ 1820{
1713 struct coro *coro = SvSTATE (coro_sv); 1821 struct coro *coro = SvSTATE (coro_sv);
1714 1822
1823 if (coro->flags & CF_RUNNING)
1824 croak ("cannot enable tracing on a running coroutine, caught");
1825
1715 if (flags & CC_TRACE) 1826 if (flags & CC_TRACE)
1716 { 1827 {
1717 if (!coro->cctx) 1828 if (!coro->cctx)
1718 coro->cctx = cctx_new_run (); 1829 coro->cctx = cctx_new_run ();
1719 else if (!(coro->cctx->flags & CC_TRACE)) 1830 else if (!(coro->cctx->flags & CC_TRACE))
1720 croak ("cannot enable tracing on coroutine with custom stack,"); 1831 croak ("cannot enable tracing on coroutine with custom stack, caught");
1721 1832
1722 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1833 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1723 } 1834 }
1724 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1835 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1725 { 1836 {
1730 else 1841 else
1731 coro->slot->runops = RUNOPS_DEFAULT; 1842 coro->slot->runops = RUNOPS_DEFAULT;
1732 } 1843 }
1733} 1844}
1734 1845
1846static void
1847coro_call_on_destroy (pTHX_ struct coro *coro)
1848{
1849 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1850 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1851
1852 if (on_destroyp)
1853 {
1854 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1855
1856 while (AvFILLp (on_destroy) >= 0)
1857 {
1858 dSP; /* don't disturb outer sp */
1859 SV *cb = av_pop (on_destroy);
1860
1861 PUSHMARK (SP);
1862
1863 if (statusp)
1864 {
1865 int i;
1866 AV *status = (AV *)SvRV (*statusp);
1867 EXTEND (SP, AvFILLp (status) + 1);
1868
1869 for (i = 0; i <= AvFILLp (status); ++i)
1870 PUSHs (AvARRAY (status)[i]);
1871 }
1872
1873 PUTBACK;
1874 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1875 }
1876 }
1877}
1878
1879static void
1880slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1881{
1882 int i;
1883 HV *hv = (HV *)SvRV (coro_current);
1884 AV *av = newAV ();
1885
1886 av_extend (av, items - 1);
1887 for (i = 0; i < items; ++i)
1888 av_push (av, SvREFCNT_inc_NN (arg [i]));
1889
1890 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1891
1892 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1893 api_ready (aTHX_ sv_manager);
1894
1895 frame->prepare = prepare_schedule;
1896 frame->check = slf_check_repeat;
1897
1898 /* as a minor optimisation, we could unwind all stacks here */
1899 /* but that puts extra pressure on pp_slf, and is not worth much */
1900 /*coro_unwind_stacks (aTHX);*/
1901}
1902
1903/*****************************************************************************/
1904/* async pool handler */
1905
1906static int
1907slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1908{
1909 HV *hv = (HV *)SvRV (coro_current);
1910 struct coro *coro = (struct coro *)frame->data;
1911
1912 if (!coro->invoke_cb)
1913 return 1; /* loop till we have invoke */
1914 else
1915 {
1916 hv_store (hv, "desc", sizeof ("desc") - 1,
1917 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1918
1919 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1920
1921 {
1922 dSP;
1923 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1924 PUTBACK;
1925 }
1926
1927 SvREFCNT_dec (GvAV (PL_defgv));
1928 GvAV (PL_defgv) = coro->invoke_av;
1929 coro->invoke_av = 0;
1930
1931 return 0;
1932 }
1933}
1934
1935static void
1936slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1937{
1938 HV *hv = (HV *)SvRV (coro_current);
1939 struct coro *coro = SvSTATE_hv ((SV *)hv);
1940
1941 if (expect_true (coro->saved_deffh))
1942 {
1943 /* subsequent iteration */
1944 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1945 coro->saved_deffh = 0;
1946
1947 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1948 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1949 {
1950 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1951 coro->invoke_av = newAV ();
1952
1953 frame->prepare = prepare_nop;
1954 }
1955 else
1956 {
1957 av_clear (GvAV (PL_defgv));
1958 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1959
1960 coro->prio = 0;
1961
1962 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1963 api_trace (aTHX_ coro_current, 0);
1964
1965 frame->prepare = prepare_schedule;
1966 av_push (av_async_pool, SvREFCNT_inc (hv));
1967 }
1968 }
1969 else
1970 {
1971 /* first iteration, simply fall through */
1972 frame->prepare = prepare_nop;
1973 }
1974
1975 frame->check = slf_check_pool_handler;
1976 frame->data = (void *)coro;
1977}
1978
1735/*****************************************************************************/ 1979/*****************************************************************************/
1736/* rouse callback */ 1980/* rouse callback */
1737 1981
1738#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 1982#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1739 1983
1744 SV *data = (SV *)GENSUB_ARG; 1988 SV *data = (SV *)GENSUB_ARG;
1745 1989
1746 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1990 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1747 { 1991 {
1748 /* first call, set args */ 1992 /* first call, set args */
1749 int i; 1993 SV *coro = SvRV (data);
1750 AV *av = newAV (); 1994 AV *av = newAV ();
1751 SV *coro = SvRV (data);
1752 1995
1753 SvRV_set (data, (SV *)av); 1996 SvRV_set (data, (SV *)av);
1997
1998 /* better take a full copy of the arguments */
1999 while (items--)
2000 av_store (av, items, newSVsv (ST (items)));
2001
1754 api_ready (aTHX_ coro); 2002 api_ready (aTHX_ coro);
1755 SvREFCNT_dec (coro); 2003 SvREFCNT_dec (coro);
1756
1757 while (items--)
1758 av_store (av, items, SvREFCNT_inc_NN (ST (items)));
1759 } 2004 }
1760 2005
1761 XSRETURN_EMPTY; 2006 XSRETURN_EMPTY;
1762} 2007}
1763 2008
1778 AV *av = (AV *)SvRV (data); 2023 AV *av = (AV *)SvRV (data);
1779 int i; 2024 int i;
1780 2025
1781 EXTEND (SP, AvFILLp (av) + 1); 2026 EXTEND (SP, AvFILLp (av) + 1);
1782 for (i = 0; i <= AvFILLp (av); ++i) 2027 for (i = 0; i <= AvFILLp (av); ++i)
1783 PUSHs (AvARRAY (av)[i]); 2028 PUSHs (sv_2mortal (AvARRAY (av)[i]));
2029
2030 /* we have stolen the elements, so set length to zero and free */
2031 AvFILLp (av) = -1;
2032 av_undef (av);
1784 2033
1785 PUTBACK; 2034 PUTBACK;
1786 } 2035 }
1787 2036
1788 return 0; 2037 return 0;
1828 struct coro *coro = SvSTATE_hv (hv); 2077 struct coro *coro = SvSTATE_hv (hv);
1829 SV *data = newRV_inc ((SV *)hv); 2078 SV *data = newRV_inc ((SV *)hv);
1830 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); 2079 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1831 2080
1832 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); 2081 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
2082 SvREFCNT_dec (data); /* magicext increases the refcount */
1833 2083
1834 SvREFCNT_dec (coro->rouse_cb); 2084 SvREFCNT_dec (coro->rouse_cb);
1835 coro->rouse_cb = SvREFCNT_inc_NN (cb); 2085 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1836 2086
1837 return cb; 2087 return cb;
1889static void 2139static void
1890slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2140slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1891{ 2141{
1892 frame->prepare = prepare_schedule; 2142 frame->prepare = prepare_schedule;
1893 frame->check = slf_check_nop; 2143 frame->check = slf_check_nop;
2144}
2145
2146static void
2147slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2148{
2149 struct coro *next = (struct coro *)slf_frame.data;
2150
2151 SvREFCNT_inc_NN (next->hv);
2152 prepare_schedule_to (aTHX_ ta, next);
2153}
2154
2155static void
2156slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2157{
2158 if (!items)
2159 croak ("Coro::schedule_to expects a coroutine argument, caught");
2160
2161 frame->data = (void *)SvSTATE (arg [0]);
2162 frame->prepare = slf_prepare_schedule_to;
2163 frame->check = slf_check_nop;
2164}
2165
2166static void
2167slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2168{
2169 api_ready (aTHX_ SvRV (coro_current));
2170
2171 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1894} 2172}
1895 2173
1896static void 2174static void
1897slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2175slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1898{ 2176{
2044 } 2322 }
2045 else 2323 else
2046 slf_argc = 0; 2324 slf_argc = 0;
2047 2325
2048 PL_op->op_ppaddr = pp_slf; 2326 PL_op->op_ppaddr = pp_slf;
2049 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2327 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2050 2328
2051 PL_op = (OP *)&slf_restore; 2329 PL_op = (OP *)&slf_restore;
2330}
2331
2332/*****************************************************************************/
2333/* dynamic wind */
2334
2335static void
2336on_enterleave_call (pTHX_ SV *cb)
2337{
2338 dSP;
2339
2340 PUSHSTACK;
2341
2342 PUSHMARK (SP);
2343 PUTBACK;
2344 call_sv (cb, G_VOID | G_DISCARD);
2345 SPAGAIN;
2346
2347 POPSTACK;
2348}
2349
2350static SV *
2351coro_avp_pop_and_free (pTHX_ AV **avp)
2352{
2353 AV *av = *avp;
2354 SV *res = av_pop (av);
2355
2356 if (AvFILLp (av) < 0)
2357 {
2358 *avp = 0;
2359 SvREFCNT_dec (av);
2360 }
2361
2362 return res;
2363}
2364
2365static void
2366coro_pop_on_enter (pTHX_ void *coro)
2367{
2368 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2369 SvREFCNT_dec (cb);
2370}
2371
2372static void
2373coro_pop_on_leave (pTHX_ void *coro)
2374{
2375 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2376 on_enterleave_call (aTHX_ sv_2mortal (cb));
2052} 2377}
2053 2378
2054/*****************************************************************************/ 2379/*****************************************************************************/
2055/* PerlIO::cede */ 2380/* PerlIO::cede */
2056 2381
2137 SV **ary; 2462 SV **ary;
2138 2463
2139 /* unfortunately, building manually saves memory */ 2464 /* unfortunately, building manually saves memory */
2140 Newx (ary, 2, SV *); 2465 Newx (ary, 2, SV *);
2141 AvALLOC (av) = ary; 2466 AvALLOC (av) = ary;
2467#if PERL_VERSION_ATLEAST (5,10,0)
2142 AvARRAY (av) = ary; 2468 AvARRAY (av) = ary;
2469#else
2470 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2471 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2472 SvPVX ((SV *)av) = (char *)ary;
2473#endif
2143 AvMAX (av) = 1; 2474 AvMAX (av) = 1;
2144 AvFILLp (av) = 0; 2475 AvFILLp (av) = 0;
2145 ary [0] = newSViv (count); 2476 ary [0] = newSViv (count);
2146 2477
2147 return newRV_noinc ((SV *)av); 2478 return newRV_noinc ((SV *)av);
2167 AvARRAY (av)[0] = AvARRAY (av)[1]; 2498 AvARRAY (av)[0] = AvARRAY (av)[1];
2168 AvARRAY (av)[1] = count_sv; 2499 AvARRAY (av)[1] = count_sv;
2169 cb = av_shift (av); 2500 cb = av_shift (av);
2170 2501
2171 if (SvOBJECT (cb)) 2502 if (SvOBJECT (cb))
2503 {
2172 api_ready (aTHX_ cb); 2504 api_ready (aTHX_ cb);
2173 else 2505 --count;
2174 croak ("callbacks not yet supported"); 2506 }
2507 else if (SvTYPE (cb) == SVt_PVCV)
2508 {
2509 dSP;
2510 PUSHMARK (SP);
2511 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2512 PUTBACK;
2513 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2514 }
2175 2515
2176 SvREFCNT_dec (cb); 2516 SvREFCNT_dec (cb);
2177
2178 --count;
2179 } 2517 }
2180} 2518}
2181 2519
2182static void 2520static void
2183coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2521coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2265} 2603}
2266 2604
2267static void 2605static void
2268slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2606slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2269{ 2607{
2608 if (items >= 2)
2609 {
2610 /* callback form */
2611 AV *av = (AV *)SvRV (arg [0]);
2612 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2613
2614 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2615
2616 if (SvIVX (AvARRAY (av)[0]) > 0)
2617 coro_semaphore_adjust (aTHX_ av, 0);
2618
2619 frame->prepare = prepare_nop;
2620 frame->check = slf_check_nop;
2621 }
2622 else
2623 {
2270 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); 2624 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2271 frame->check = slf_check_semaphore_wait; 2625 frame->check = slf_check_semaphore_wait;
2626 }
2272} 2627}
2273 2628
2274/* signal */ 2629/* signal */
2275 2630
2276static void 2631static void
2348 dXSARGS; 2703 dXSARGS;
2349 AV *state = (AV *)GENSUB_ARG; 2704 AV *state = (AV *)GENSUB_ARG;
2350 SV *coro = av_pop (state); 2705 SV *coro = av_pop (state);
2351 SV *data_sv = newSV (sizeof (struct io_state)); 2706 SV *data_sv = newSV (sizeof (struct io_state));
2352 2707
2353 av_extend (state, items); 2708 av_extend (state, items - 1);
2354 2709
2355 sv_upgrade (data_sv, SVt_PV); 2710 sv_upgrade (data_sv, SVt_PV);
2356 SvCUR_set (data_sv, sizeof (struct io_state)); 2711 SvCUR_set (data_sv, sizeof (struct io_state));
2357 SvPOK_only (data_sv); 2712 SvPOK_only (data_sv);
2358 2713
2493 XSRETURN_EMPTY; 2848 XSRETURN_EMPTY;
2494} 2849}
2495 2850
2496/*****************************************************************************/ 2851/*****************************************************************************/
2497 2852
2853#if CORO_CLONE
2854# include "clone.c"
2855#endif
2856
2498MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2857MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2499 2858
2500PROTOTYPES: DISABLE 2859PROTOTYPES: DISABLE
2501 2860
2502BOOT: 2861BOOT:
2506 coro_thx = PERL_GET_CONTEXT; 2865 coro_thx = PERL_GET_CONTEXT;
2507# endif 2866# endif
2508#endif 2867#endif
2509 BOOT_PAGESIZE; 2868 BOOT_PAGESIZE;
2510 2869
2870 cctx_current = cctx_new_empty ();
2871
2511 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2872 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2512 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2873 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2513 2874
2514 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2875 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2515 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2876 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2534 2895
2535 { 2896 {
2536 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2897 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2537 2898
2538 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2899 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2539 hv_store_ent (PL_custom_op_names, slf, 2900 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2540 newSVpv ("coro_slf", 0), 0);
2541 2901
2542 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2902 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2543 hv_store_ent (PL_custom_op_descs, slf, 2903 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2544 newSVpv ("coro schedule like function", 0), 0);
2545 } 2904 }
2546 2905
2547 coroapi.ver = CORO_API_VERSION; 2906 coroapi.ver = CORO_API_VERSION;
2548 coroapi.rev = CORO_API_REVISION; 2907 coroapi.rev = CORO_API_REVISION;
2549 2908
2568 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2927 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2569} 2928}
2570 2929
2571SV * 2930SV *
2572new (char *klass, ...) 2931new (char *klass, ...)
2932 ALIAS:
2933 Coro::new = 1
2573 CODE: 2934 CODE:
2574{ 2935{
2575 struct coro *coro; 2936 struct coro *coro;
2576 MAGIC *mg; 2937 MAGIC *mg;
2577 HV *hv; 2938 HV *hv;
2939 CV *cb;
2578 int i; 2940 int i;
2941
2942 if (items > 1)
2943 {
2944 cb = coro_sv_2cv (aTHX_ ST (1));
2945
2946 if (!ix)
2947 {
2948 if (CvISXSUB (cb))
2949 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2950
2951 if (!CvROOT (cb))
2952 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2953 }
2954 }
2579 2955
2580 Newz (0, coro, 1, struct coro); 2956 Newz (0, coro, 1, struct coro);
2581 coro->args = newAV (); 2957 coro->args = newAV ();
2582 coro->flags = CF_NEW; 2958 coro->flags = CF_NEW;
2583 2959
2588 coro->hv = hv = newHV (); 2964 coro->hv = hv = newHV ();
2589 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2965 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2590 mg->mg_flags |= MGf_DUP; 2966 mg->mg_flags |= MGf_DUP;
2591 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2967 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2592 2968
2969 if (items > 1)
2970 {
2593 av_extend (coro->args, items - 1); 2971 av_extend (coro->args, items - 1 + ix - 1);
2972
2973 if (ix)
2974 {
2975 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2976 cb = cv_coro_run;
2977 }
2978
2979 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2980
2594 for (i = 1; i < items; i++) 2981 for (i = 2; i < items; i++)
2595 av_push (coro->args, newSVsv (ST (i))); 2982 av_push (coro->args, newSVsv (ST (i)));
2983 }
2596} 2984}
2597 OUTPUT: 2985 OUTPUT:
2598 RETVAL 2986 RETVAL
2599 2987
2600void 2988void
2613void 3001void
2614_exit (int code) 3002_exit (int code)
2615 PROTOTYPE: $ 3003 PROTOTYPE: $
2616 CODE: 3004 CODE:
2617 _exit (code); 3005 _exit (code);
3006
3007SV *
3008clone (Coro::State coro)
3009 CODE:
3010{
3011#if CORO_CLONE
3012 struct coro *ncoro = coro_clone (aTHX_ coro);
3013 MAGIC *mg;
3014 /* TODO: too much duplication */
3015 ncoro->hv = newHV ();
3016 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3017 mg->mg_flags |= MGf_DUP;
3018 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3019#else
3020 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3021#endif
3022}
3023 OUTPUT:
3024 RETVAL
2618 3025
2619int 3026int
2620cctx_stacksize (int new_stacksize = 0) 3027cctx_stacksize (int new_stacksize = 0)
2621 PROTOTYPE: ;$ 3028 PROTOTYPE: ;$
2622 CODE: 3029 CODE:
2672 eval = 1 3079 eval = 1
2673 CODE: 3080 CODE:
2674{ 3081{
2675 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3082 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2676 { 3083 {
2677 struct coro temp; 3084 struct coro *current = SvSTATE_current;
2678 3085
2679 if (!(coro->flags & CF_RUNNING)) 3086 if (current != coro)
2680 { 3087 {
2681 PUTBACK; 3088 PUTBACK;
2682 save_perl (aTHX_ &temp); 3089 save_perl (aTHX_ current);
2683 load_perl (aTHX_ coro); 3090 load_perl (aTHX_ coro);
3091 SPAGAIN;
2684 } 3092 }
2685 3093
2686 {
2687 dSP;
2688 ENTER;
2689 SAVETMPS;
2690 PUTBACK;
2691 PUSHSTACK; 3094 PUSHSTACK;
3095
2692 PUSHMARK (SP); 3096 PUSHMARK (SP);
3097 PUTBACK;
2693 3098
2694 if (ix) 3099 if (ix)
2695 eval_sv (coderef, 0); 3100 eval_sv (coderef, 0);
2696 else 3101 else
2697 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3102 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2698 3103
2699 POPSTACK; 3104 POPSTACK;
2700 SPAGAIN; 3105 SPAGAIN;
2701 FREETMPS;
2702 LEAVE;
2703 PUTBACK;
2704 }
2705 3106
2706 if (!(coro->flags & CF_RUNNING)) 3107 if (current != coro)
2707 { 3108 {
3109 PUTBACK;
2708 save_perl (aTHX_ coro); 3110 save_perl (aTHX_ coro);
2709 load_perl (aTHX_ &temp); 3111 load_perl (aTHX_ current);
2710 SPAGAIN; 3112 SPAGAIN;
2711 } 3113 }
2712 } 3114 }
2713} 3115}
2714 3116
2718 ALIAS: 3120 ALIAS:
2719 is_ready = CF_READY 3121 is_ready = CF_READY
2720 is_running = CF_RUNNING 3122 is_running = CF_RUNNING
2721 is_new = CF_NEW 3123 is_new = CF_NEW
2722 is_destroyed = CF_DESTROYED 3124 is_destroyed = CF_DESTROYED
3125 is_suspended = CF_SUSPENDED
2723 CODE: 3126 CODE:
2724 RETVAL = boolSV (coro->flags & ix); 3127 RETVAL = boolSV (coro->flags & ix);
2725 OUTPUT: 3128 OUTPUT:
2726 RETVAL 3129 RETVAL
2727 3130
2743 3146
2744SV * 3147SV *
2745has_cctx (Coro::State coro) 3148has_cctx (Coro::State coro)
2746 PROTOTYPE: $ 3149 PROTOTYPE: $
2747 CODE: 3150 CODE:
2748 RETVAL = boolSV (!!coro->cctx); 3151 /* maybe manage the running flag differently */
3152 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2749 OUTPUT: 3153 OUTPUT:
2750 RETVAL 3154 RETVAL
2751 3155
2752int 3156int
2753is_traced (Coro::State coro) 3157is_traced (Coro::State coro)
2773 3177
2774void 3178void
2775force_cctx () 3179force_cctx ()
2776 PROTOTYPE: 3180 PROTOTYPE:
2777 CODE: 3181 CODE:
2778 SvSTATE_current->cctx->idle_sp = 0; 3182 cctx_current->idle_sp = 0;
2779 3183
2780void 3184void
2781swap_defsv (Coro::State self) 3185swap_defsv (Coro::State self)
2782 PROTOTYPE: $ 3186 PROTOTYPE: $
2783 ALIAS: 3187 ALIAS:
2791 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3195 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2792 3196
2793 SV *tmp = *src; *src = *dst; *dst = tmp; 3197 SV *tmp = *src; *src = *dst; *dst = tmp;
2794 } 3198 }
2795 3199
3200void
3201cancel (Coro::State self)
3202 CODE:
3203 coro_state_destroy (aTHX_ self);
3204 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3205
2796 3206
2797MODULE = Coro::State PACKAGE = Coro 3207MODULE = Coro::State PACKAGE = Coro
2798 3208
2799BOOT: 3209BOOT:
2800{ 3210{
2801 int i; 3211 int i;
2802 3212
2803 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2804 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3213 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2805 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3214 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2806 3215 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3216 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2807 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 3217 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2808 SvREADONLY_on (coro_current); 3218 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3219 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3220 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3221 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3222
3223 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3224 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3225 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3226 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2809 3227
2810 coro_stash = gv_stashpv ("Coro", TRUE); 3228 coro_stash = gv_stashpv ("Coro", TRUE);
2811 3229
2812 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3230 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2813 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3231 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2821 3239
2822 { 3240 {
2823 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3241 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2824 3242
2825 coroapi.schedule = api_schedule; 3243 coroapi.schedule = api_schedule;
3244 coroapi.schedule_to = api_schedule_to;
2826 coroapi.cede = api_cede; 3245 coroapi.cede = api_cede;
2827 coroapi.cede_notself = api_cede_notself; 3246 coroapi.cede_notself = api_cede_notself;
2828 coroapi.ready = api_ready; 3247 coroapi.ready = api_ready;
2829 coroapi.is_ready = api_is_ready; 3248 coroapi.is_ready = api_is_ready;
2830 coroapi.nready = coro_nready; 3249 coroapi.nready = coro_nready;
2835 SvREADONLY_on (sv); 3254 SvREADONLY_on (sv);
2836 } 3255 }
2837} 3256}
2838 3257
2839void 3258void
3259terminate (...)
3260 CODE:
3261 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3262
3263void
2840schedule (...) 3264schedule (...)
2841 CODE: 3265 CODE:
2842 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3266 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3267
3268void
3269schedule_to (...)
3270 CODE:
3271 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3272
3273void
3274cede_to (...)
3275 CODE:
3276 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
2843 3277
2844void 3278void
2845cede (...) 3279cede (...)
2846 CODE: 3280 CODE:
2847 CORO_EXECUTE_SLF_XS (slf_init_cede); 3281 CORO_EXECUTE_SLF_XS (slf_init_cede);
2902 CODE: 3336 CODE:
2903 RETVAL = coro_nready; 3337 RETVAL = coro_nready;
2904 OUTPUT: 3338 OUTPUT:
2905 RETVAL 3339 RETVAL
2906 3340
2907# for async_pool speedup
2908void 3341void
2909_pool_1 (SV *cb) 3342suspend (Coro::State self)
3343 PROTOTYPE: $
2910 CODE: 3344 CODE:
2911{ 3345 self->flags |= CF_SUSPENDED;
2912 HV *hv = (HV *)SvRV (coro_current);
2913 struct coro *coro = SvSTATE_hv ((SV *)hv);
2914 AV *defav = GvAV (PL_defgv);
2915 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2916 AV *invoke_av;
2917 int i, len;
2918 3346
2919 if (!invoke) 3347void
3348resume (Coro::State self)
3349 PROTOTYPE: $
3350 CODE:
3351 self->flags &= ~CF_SUSPENDED;
3352
3353void
3354_pool_handler (...)
3355 CODE:
3356 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3357
3358void
3359async_pool (SV *cv, ...)
3360 PROTOTYPE: &@
3361 PPCODE:
3362{
3363 HV *hv = (HV *)av_pop (av_async_pool);
3364 AV *av = newAV ();
3365 SV *cb = ST (0);
3366 int i;
3367
3368 av_extend (av, items - 2);
3369 for (i = 1; i < items; ++i)
3370 av_push (av, SvREFCNT_inc_NN (ST (i)));
3371
3372 if ((SV *)hv == &PL_sv_undef)
2920 { 3373 {
2921 SV *old = PL_diehook; 3374 PUSHMARK (SP);
2922 PL_diehook = 0; 3375 EXTEND (SP, 2);
2923 SvREFCNT_dec (old); 3376 PUSHs (sv_Coro);
2924 croak ("\3async_pool terminate\2\n"); 3377 PUSHs ((SV *)cv_pool_handler);
3378 PUTBACK;
3379 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3380 SPAGAIN;
3381
3382 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2925 } 3383 }
2926 3384
2927 SvREFCNT_dec (coro->saved_deffh);
2928 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2929
2930 hv_store (hv, "desc", sizeof ("desc") - 1,
2931 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2932
2933 invoke_av = (AV *)SvRV (invoke);
2934 len = av_len (invoke_av);
2935
2936 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2937
2938 if (len > 0)
2939 { 3385 {
2940 av_fill (defav, len - 1); 3386 struct coro *coro = SvSTATE_hv (hv);
2941 for (i = 0; i < len; ++i) 3387
2942 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3388 assert (!coro->invoke_cb);
3389 assert (!coro->invoke_av);
3390 coro->invoke_cb = SvREFCNT_inc (cb);
3391 coro->invoke_av = av;
2943 } 3392 }
2944}
2945 3393
2946void 3394 api_ready (aTHX_ (SV *)hv);
2947_pool_2 (SV *cb)
2948 CODE:
2949{
2950 HV *hv = (HV *)SvRV (coro_current);
2951 struct coro *coro = SvSTATE_hv ((SV *)hv);
2952 3395
2953 sv_setsv (cb, &PL_sv_undef); 3396 if (GIMME_V != G_VOID)
2954 3397 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
2955 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3398 else
2956 coro->saved_deffh = 0;
2957
2958 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2959 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2960 {
2961 SV *old = PL_diehook;
2962 PL_diehook = 0;
2963 SvREFCNT_dec (old); 3399 SvREFCNT_dec (hv);
2964 croak ("\3async_pool terminate\2\n");
2965 }
2966
2967 av_clear (GvAV (PL_defgv));
2968 hv_store (hv, "desc", sizeof ("desc") - 1,
2969 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2970
2971 coro->prio = 0;
2972
2973 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2974 api_trace (aTHX_ coro_current, 0);
2975
2976 av_push (av_async_pool, newSVsv (coro_current));
2977} 3400}
2978 3401
2979SV * 3402SV *
2980rouse_cb () 3403rouse_cb ()
2981 PROTOTYPE: 3404 PROTOTYPE:
2983 RETVAL = coro_new_rouse_cb (aTHX); 3406 RETVAL = coro_new_rouse_cb (aTHX);
2984 OUTPUT: 3407 OUTPUT:
2985 RETVAL 3408 RETVAL
2986 3409
2987void 3410void
2988rouse_wait (SV *cb = 0) 3411rouse_wait (...)
2989 PROTOTYPE: ;$ 3412 PROTOTYPE: ;$
2990 PPCODE: 3413 PPCODE:
2991 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3414 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3415
3416void
3417on_enter (SV *block)
3418 ALIAS:
3419 on_leave = 1
3420 PROTOTYPE: &
3421 CODE:
3422{
3423 struct coro *coro = SvSTATE_current;
3424 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3425
3426 block = (SV *)coro_sv_2cv (aTHX_ block);
3427
3428 if (!*avp)
3429 *avp = newAV ();
3430
3431 av_push (*avp, SvREFCNT_inc (block));
3432
3433 if (!ix)
3434 on_enterleave_call (aTHX_ block);
3435
3436 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3437 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3438 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3439}
2992 3440
2993 3441
2994MODULE = Coro::State PACKAGE = PerlIO::cede 3442MODULE = Coro::State PACKAGE = PerlIO::cede
2995 3443
2996BOOT: 3444BOOT:
3007 GvSTASH (CvGV (cv)) 3455 GvSTASH (CvGV (cv))
3008 ); 3456 );
3009 OUTPUT: 3457 OUTPUT:
3010 RETVAL 3458 RETVAL
3011 3459
3012# helper for Coro::Channel 3460# helper for Coro::Channel and others
3013SV * 3461SV *
3014_alloc (int count) 3462_alloc (int count)
3015 CODE: 3463 CODE:
3016 RETVAL = coro_waitarray_new (aTHX_ count); 3464 RETVAL = coro_waitarray_new (aTHX_ count);
3017 OUTPUT: 3465 OUTPUT:
3030 adjust = 1 3478 adjust = 1
3031 CODE: 3479 CODE:
3032 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3480 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
3033 3481
3034void 3482void
3035down (SV *self) 3483down (...)
3036 CODE: 3484 CODE:
3037 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3485 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3038 3486
3039void 3487void
3040wait (SV *self) 3488wait (...)
3041 CODE: 3489 CODE:
3042 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); 3490 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
3043 3491
3044void 3492void
3045try (SV *self) 3493try (SV *self)
3075 for (i = 1; i <= wcount; ++i) 3523 for (i = 1; i <= wcount; ++i)
3076 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3524 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3077 } 3525 }
3078} 3526}
3079 3527
3528MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3529
3530void
3531_may_delete (SV *sem, int count, int extra_refs)
3532 PPCODE:
3533{
3534 AV *av = (AV *)SvRV (sem);
3535
3536 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3537 && AvFILLp (av) == 0 /* no waiters, just count */
3538 && SvIV (AvARRAY (av)[0]) == count)
3539 XSRETURN_YES;
3540
3541 XSRETURN_NO;
3542}
3543
3080MODULE = Coro::State PACKAGE = Coro::Signal 3544MODULE = Coro::State PACKAGE = Coro::Signal
3081 3545
3082SV * 3546SV *
3083new (SV *klass) 3547new (SV *klass)
3084 CODE: 3548 CODE:
3088 ); 3552 );
3089 OUTPUT: 3553 OUTPUT:
3090 RETVAL 3554 RETVAL
3091 3555
3092void 3556void
3093wait (SV *self) 3557wait (...)
3094 CODE: 3558 CODE:
3095 CORO_EXECUTE_SLF_XS (slf_init_signal_wait); 3559 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3096 3560
3097void 3561void
3098broadcast (SV *self) 3562broadcast (SV *self)
3155 3619
3156void 3620void
3157_register (char *target, char *proto, SV *req) 3621_register (char *target, char *proto, SV *req)
3158 CODE: 3622 CODE:
3159{ 3623{
3160 HV *st; 3624 CV *req_cv = coro_sv_2cv (aTHX_ req);
3161 GV *gvp;
3162 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
3163 /* newXSproto doesn't return the CV on 5.8 */ 3625 /* newXSproto doesn't return the CV on 5.8 */
3164 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3626 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3165 sv_setpv ((SV *)slf_cv, proto); 3627 sv_setpv ((SV *)slf_cv, proto);
3166 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3628 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3167} 3629}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines