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.304 by root, Wed Nov 19 08:22:48 2008 UTC vs.
Revision 1.345 by root, Wed Dec 17 04:57:07 2008 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;
1750 AV *av = newAV (); 1993 AV *av = newAV ();
1751 SV *coro = SvRV (data); 1994 SV *coro = SvRV (data);
1752 1995
1753 SvRV_set (data, (SV *)av); 1996 SvRV_set (data, (SV *)av);
1754 api_ready (aTHX_ coro); 1997 api_ready (aTHX_ coro);
1895static void 2138static void
1896slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2139slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1897{ 2140{
1898 frame->prepare = prepare_schedule; 2141 frame->prepare = prepare_schedule;
1899 frame->check = slf_check_nop; 2142 frame->check = slf_check_nop;
2143}
2144
2145static void
2146slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2147{
2148 struct coro *next = (struct coro *)slf_frame.data;
2149
2150 SvREFCNT_inc_NN (next->hv);
2151 prepare_schedule_to (aTHX_ ta, next);
2152}
2153
2154static void
2155slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2156{
2157 if (!items)
2158 croak ("Coro::schedule_to expects a coroutine argument, caught");
2159
2160 frame->data = (void *)SvSTATE (arg [0]);
2161 frame->prepare = slf_prepare_schedule_to;
2162 frame->check = slf_check_nop;
2163}
2164
2165static void
2166slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2167{
2168 api_ready (aTHX_ SvRV (coro_current));
2169
2170 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1900} 2171}
1901 2172
1902static void 2173static void
1903slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2174slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1904{ 2175{
2050 } 2321 }
2051 else 2322 else
2052 slf_argc = 0; 2323 slf_argc = 0;
2053 2324
2054 PL_op->op_ppaddr = pp_slf; 2325 PL_op->op_ppaddr = pp_slf;
2055 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2326 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2056 2327
2057 PL_op = (OP *)&slf_restore; 2328 PL_op = (OP *)&slf_restore;
2329}
2330
2331/*****************************************************************************/
2332/* dynamic wind */
2333
2334static void
2335on_enterleave_call (pTHX_ SV *cb)
2336{
2337 dSP;
2338
2339 PUSHSTACK;
2340
2341 PUSHMARK (SP);
2342 PUTBACK;
2343 call_sv (cb, G_VOID | G_DISCARD);
2344 SPAGAIN;
2345
2346 POPSTACK;
2347}
2348
2349static SV *
2350coro_avp_pop_and_free (pTHX_ AV **avp)
2351{
2352 AV *av = *avp;
2353 SV *res = av_pop (av);
2354
2355 if (AvFILLp (av) < 0)
2356 {
2357 *avp = 0;
2358 SvREFCNT_dec (av);
2359 }
2360
2361 return res;
2362}
2363
2364static void
2365coro_pop_on_enter (pTHX_ void *coro)
2366{
2367 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2368 SvREFCNT_dec (cb);
2369}
2370
2371static void
2372coro_pop_on_leave (pTHX_ void *coro)
2373{
2374 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2375 on_enterleave_call (aTHX_ sv_2mortal (cb));
2058} 2376}
2059 2377
2060/*****************************************************************************/ 2378/*****************************************************************************/
2061/* PerlIO::cede */ 2379/* PerlIO::cede */
2062 2380
2143 SV **ary; 2461 SV **ary;
2144 2462
2145 /* unfortunately, building manually saves memory */ 2463 /* unfortunately, building manually saves memory */
2146 Newx (ary, 2, SV *); 2464 Newx (ary, 2, SV *);
2147 AvALLOC (av) = ary; 2465 AvALLOC (av) = ary;
2148 SvPV_set ((SV *)av, (char *)ary); /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2466#if PERL_VERSION_ATLEAST (5,10,0)
2467 AvARRAY (av) = ary;
2468#else
2469 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2470 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2471 SvPVX ((SV *)av) = (char *)ary;
2472#endif
2149 AvMAX (av) = 1; 2473 AvMAX (av) = 1;
2150 AvFILLp (av) = 0; 2474 AvFILLp (av) = 0;
2151 ary [0] = newSViv (count); 2475 ary [0] = newSViv (count);
2152 2476
2153 return newRV_noinc ((SV *)av); 2477 return newRV_noinc ((SV *)av);
2173 AvARRAY (av)[0] = AvARRAY (av)[1]; 2497 AvARRAY (av)[0] = AvARRAY (av)[1];
2174 AvARRAY (av)[1] = count_sv; 2498 AvARRAY (av)[1] = count_sv;
2175 cb = av_shift (av); 2499 cb = av_shift (av);
2176 2500
2177 if (SvOBJECT (cb)) 2501 if (SvOBJECT (cb))
2502 {
2178 api_ready (aTHX_ cb); 2503 api_ready (aTHX_ cb);
2179 else 2504 --count;
2180 croak ("callbacks not yet supported"); 2505 }
2506 else if (SvTYPE (cb) == SVt_PVCV)
2507 {
2508 dSP;
2509 PUSHMARK (SP);
2510 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2511 PUTBACK;
2512 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2513 }
2181 2514
2182 SvREFCNT_dec (cb); 2515 SvREFCNT_dec (cb);
2183
2184 --count;
2185 } 2516 }
2186} 2517}
2187 2518
2188static void 2519static void
2189coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2520coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2271} 2602}
2272 2603
2273static void 2604static void
2274slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2605slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2275{ 2606{
2607 if (items >= 2)
2608 {
2609 /* callback form */
2610 AV *av = (AV *)SvRV (arg [0]);
2611 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2612
2613 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2614
2615 if (SvIVX (AvARRAY (av)[0]) > 0)
2616 coro_semaphore_adjust (aTHX_ av, 0);
2617
2618 frame->prepare = prepare_nop;
2619 frame->check = slf_check_nop;
2620 }
2621 else
2622 {
2276 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); 2623 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2277 frame->check = slf_check_semaphore_wait; 2624 frame->check = slf_check_semaphore_wait;
2625 }
2278} 2626}
2279 2627
2280/* signal */ 2628/* signal */
2281 2629
2282static void 2630static void
2354 dXSARGS; 2702 dXSARGS;
2355 AV *state = (AV *)GENSUB_ARG; 2703 AV *state = (AV *)GENSUB_ARG;
2356 SV *coro = av_pop (state); 2704 SV *coro = av_pop (state);
2357 SV *data_sv = newSV (sizeof (struct io_state)); 2705 SV *data_sv = newSV (sizeof (struct io_state));
2358 2706
2359 av_extend (state, items); 2707 av_extend (state, items - 1);
2360 2708
2361 sv_upgrade (data_sv, SVt_PV); 2709 sv_upgrade (data_sv, SVt_PV);
2362 SvCUR_set (data_sv, sizeof (struct io_state)); 2710 SvCUR_set (data_sv, sizeof (struct io_state));
2363 SvPOK_only (data_sv); 2711 SvPOK_only (data_sv);
2364 2712
2499 XSRETURN_EMPTY; 2847 XSRETURN_EMPTY;
2500} 2848}
2501 2849
2502/*****************************************************************************/ 2850/*****************************************************************************/
2503 2851
2852#if CORO_CLONE
2853# include "clone.c"
2854#endif
2855
2504MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2856MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2505 2857
2506PROTOTYPES: DISABLE 2858PROTOTYPES: DISABLE
2507 2859
2508BOOT: 2860BOOT:
2512 coro_thx = PERL_GET_CONTEXT; 2864 coro_thx = PERL_GET_CONTEXT;
2513# endif 2865# endif
2514#endif 2866#endif
2515 BOOT_PAGESIZE; 2867 BOOT_PAGESIZE;
2516 2868
2869 cctx_current = cctx_new_empty ();
2870
2517 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2871 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2518 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2872 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2519 2873
2520 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2874 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2521 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2875 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2540 2894
2541 { 2895 {
2542 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2896 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2543 2897
2544 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2898 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2545 hv_store_ent (PL_custom_op_names, slf, 2899 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2546 newSVpv ("coro_slf", 0), 0);
2547 2900
2548 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2901 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2549 hv_store_ent (PL_custom_op_descs, slf, 2902 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2550 newSVpv ("coro schedule like function", 0), 0);
2551 } 2903 }
2552 2904
2553 coroapi.ver = CORO_API_VERSION; 2905 coroapi.ver = CORO_API_VERSION;
2554 coroapi.rev = CORO_API_REVISION; 2906 coroapi.rev = CORO_API_REVISION;
2555 2907
2574 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2926 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2575} 2927}
2576 2928
2577SV * 2929SV *
2578new (char *klass, ...) 2930new (char *klass, ...)
2931 ALIAS:
2932 Coro::new = 1
2579 CODE: 2933 CODE:
2580{ 2934{
2581 struct coro *coro; 2935 struct coro *coro;
2582 MAGIC *mg; 2936 MAGIC *mg;
2583 HV *hv; 2937 HV *hv;
2938 CV *cb;
2584 int i; 2939 int i;
2940
2941 if (items > 1)
2942 {
2943 cb = coro_sv_2cv (aTHX_ ST (1));
2944
2945 if (!ix)
2946 {
2947 if (CvISXSUB (cb))
2948 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2949
2950 if (!CvROOT (cb))
2951 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2952 }
2953 }
2585 2954
2586 Newz (0, coro, 1, struct coro); 2955 Newz (0, coro, 1, struct coro);
2587 coro->args = newAV (); 2956 coro->args = newAV ();
2588 coro->flags = CF_NEW; 2957 coro->flags = CF_NEW;
2589 2958
2594 coro->hv = hv = newHV (); 2963 coro->hv = hv = newHV ();
2595 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2964 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2596 mg->mg_flags |= MGf_DUP; 2965 mg->mg_flags |= MGf_DUP;
2597 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2966 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2598 2967
2968 if (items > 1)
2969 {
2599 av_extend (coro->args, items - 1); 2970 av_extend (coro->args, items - 1 + ix - 1);
2971
2972 if (ix)
2973 {
2974 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2975 cb = cv_coro_run;
2976 }
2977
2978 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2979
2600 for (i = 1; i < items; i++) 2980 for (i = 2; i < items; i++)
2601 av_push (coro->args, newSVsv (ST (i))); 2981 av_push (coro->args, newSVsv (ST (i)));
2982 }
2602} 2983}
2603 OUTPUT: 2984 OUTPUT:
2604 RETVAL 2985 RETVAL
2605 2986
2606void 2987void
2619void 3000void
2620_exit (int code) 3001_exit (int code)
2621 PROTOTYPE: $ 3002 PROTOTYPE: $
2622 CODE: 3003 CODE:
2623 _exit (code); 3004 _exit (code);
3005
3006SV *
3007clone (Coro::State coro)
3008 CODE:
3009{
3010#if CORO_CLONE
3011 struct coro *ncoro = coro_clone (aTHX_ coro);
3012 MAGIC *mg;
3013 /* TODO: too much duplication */
3014 ncoro->hv = newHV ();
3015 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3016 mg->mg_flags |= MGf_DUP;
3017 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3018#else
3019 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3020#endif
3021}
3022 OUTPUT:
3023 RETVAL
2624 3024
2625int 3025int
2626cctx_stacksize (int new_stacksize = 0) 3026cctx_stacksize (int new_stacksize = 0)
2627 PROTOTYPE: ;$ 3027 PROTOTYPE: ;$
2628 CODE: 3028 CODE:
2678 eval = 1 3078 eval = 1
2679 CODE: 3079 CODE:
2680{ 3080{
2681 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3081 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2682 { 3082 {
2683 struct coro temp; 3083 struct coro *current = SvSTATE_current;
2684 3084
2685 if (!(coro->flags & CF_RUNNING)) 3085 if (current != coro)
2686 { 3086 {
2687 PUTBACK; 3087 PUTBACK;
2688 save_perl (aTHX_ &temp); 3088 save_perl (aTHX_ current);
2689 load_perl (aTHX_ coro); 3089 load_perl (aTHX_ coro);
3090 SPAGAIN;
2690 } 3091 }
2691 3092
2692 {
2693 dSP;
2694 ENTER;
2695 SAVETMPS;
2696 PUTBACK;
2697 PUSHSTACK; 3093 PUSHSTACK;
3094
2698 PUSHMARK (SP); 3095 PUSHMARK (SP);
3096 PUTBACK;
2699 3097
2700 if (ix) 3098 if (ix)
2701 eval_sv (coderef, 0); 3099 eval_sv (coderef, 0);
2702 else 3100 else
2703 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3101 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2704 3102
2705 POPSTACK; 3103 POPSTACK;
2706 SPAGAIN; 3104 SPAGAIN;
2707 FREETMPS;
2708 LEAVE;
2709 PUTBACK;
2710 }
2711 3105
2712 if (!(coro->flags & CF_RUNNING)) 3106 if (current != coro)
2713 { 3107 {
3108 PUTBACK;
2714 save_perl (aTHX_ coro); 3109 save_perl (aTHX_ coro);
2715 load_perl (aTHX_ &temp); 3110 load_perl (aTHX_ current);
2716 SPAGAIN; 3111 SPAGAIN;
2717 } 3112 }
2718 } 3113 }
2719} 3114}
2720 3115
2724 ALIAS: 3119 ALIAS:
2725 is_ready = CF_READY 3120 is_ready = CF_READY
2726 is_running = CF_RUNNING 3121 is_running = CF_RUNNING
2727 is_new = CF_NEW 3122 is_new = CF_NEW
2728 is_destroyed = CF_DESTROYED 3123 is_destroyed = CF_DESTROYED
3124 is_suspended = CF_SUSPENDED
2729 CODE: 3125 CODE:
2730 RETVAL = boolSV (coro->flags & ix); 3126 RETVAL = boolSV (coro->flags & ix);
2731 OUTPUT: 3127 OUTPUT:
2732 RETVAL 3128 RETVAL
2733 3129
2749 3145
2750SV * 3146SV *
2751has_cctx (Coro::State coro) 3147has_cctx (Coro::State coro)
2752 PROTOTYPE: $ 3148 PROTOTYPE: $
2753 CODE: 3149 CODE:
2754 RETVAL = boolSV (!!coro->cctx); 3150 /* maybe manage the running flag differently */
3151 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2755 OUTPUT: 3152 OUTPUT:
2756 RETVAL 3153 RETVAL
2757 3154
2758int 3155int
2759is_traced (Coro::State coro) 3156is_traced (Coro::State coro)
2779 3176
2780void 3177void
2781force_cctx () 3178force_cctx ()
2782 PROTOTYPE: 3179 PROTOTYPE:
2783 CODE: 3180 CODE:
2784 SvSTATE_current->cctx->idle_sp = 0; 3181 cctx_current->idle_sp = 0;
2785 3182
2786void 3183void
2787swap_defsv (Coro::State self) 3184swap_defsv (Coro::State self)
2788 PROTOTYPE: $ 3185 PROTOTYPE: $
2789 ALIAS: 3186 ALIAS:
2797 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3194 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2798 3195
2799 SV *tmp = *src; *src = *dst; *dst = tmp; 3196 SV *tmp = *src; *src = *dst; *dst = tmp;
2800 } 3197 }
2801 3198
3199void
3200cancel (Coro::State self)
3201 CODE:
3202 coro_state_destroy (aTHX_ self);
3203 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3204
2802 3205
2803MODULE = Coro::State PACKAGE = Coro 3206MODULE = Coro::State PACKAGE = Coro
2804 3207
2805BOOT: 3208BOOT:
2806{ 3209{
2807 int i; 3210 int i;
2808 3211
2809 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2810 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3212 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2811 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3213 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2812 3214 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3215 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2813 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 3216 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2814 SvREADONLY_on (coro_current); 3217 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3218 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3219 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3220 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3221
3222 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3223 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3224 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3225 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2815 3226
2816 coro_stash = gv_stashpv ("Coro", TRUE); 3227 coro_stash = gv_stashpv ("Coro", TRUE);
2817 3228
2818 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3229 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2819 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3230 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2827 3238
2828 { 3239 {
2829 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3240 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2830 3241
2831 coroapi.schedule = api_schedule; 3242 coroapi.schedule = api_schedule;
3243 coroapi.schedule_to = api_schedule_to;
2832 coroapi.cede = api_cede; 3244 coroapi.cede = api_cede;
2833 coroapi.cede_notself = api_cede_notself; 3245 coroapi.cede_notself = api_cede_notself;
2834 coroapi.ready = api_ready; 3246 coroapi.ready = api_ready;
2835 coroapi.is_ready = api_is_ready; 3247 coroapi.is_ready = api_is_ready;
2836 coroapi.nready = coro_nready; 3248 coroapi.nready = coro_nready;
2841 SvREADONLY_on (sv); 3253 SvREADONLY_on (sv);
2842 } 3254 }
2843} 3255}
2844 3256
2845void 3257void
3258terminate (...)
3259 CODE:
3260 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3261
3262void
2846schedule (...) 3263schedule (...)
2847 CODE: 3264 CODE:
2848 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3265 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3266
3267void
3268schedule_to (...)
3269 CODE:
3270 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3271
3272void
3273cede_to (...)
3274 CODE:
3275 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
2849 3276
2850void 3277void
2851cede (...) 3278cede (...)
2852 CODE: 3279 CODE:
2853 CORO_EXECUTE_SLF_XS (slf_init_cede); 3280 CORO_EXECUTE_SLF_XS (slf_init_cede);
2908 CODE: 3335 CODE:
2909 RETVAL = coro_nready; 3336 RETVAL = coro_nready;
2910 OUTPUT: 3337 OUTPUT:
2911 RETVAL 3338 RETVAL
2912 3339
2913# for async_pool speedup
2914void 3340void
2915_pool_1 (SV *cb) 3341suspend (Coro::State self)
3342 PROTOTYPE: $
2916 CODE: 3343 CODE:
2917{ 3344 self->flags |= CF_SUSPENDED;
2918 HV *hv = (HV *)SvRV (coro_current);
2919 struct coro *coro = SvSTATE_hv ((SV *)hv);
2920 AV *defav = GvAV (PL_defgv);
2921 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2922 AV *invoke_av;
2923 int i, len;
2924 3345
2925 if (!invoke) 3346void
3347resume (Coro::State self)
3348 PROTOTYPE: $
3349 CODE:
3350 self->flags &= ~CF_SUSPENDED;
3351
3352void
3353_pool_handler (...)
3354 CODE:
3355 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3356
3357void
3358async_pool (SV *cv, ...)
3359 PROTOTYPE: &@
3360 PPCODE:
3361{
3362 HV *hv = (HV *)av_pop (av_async_pool);
3363 AV *av = newAV ();
3364 SV *cb = ST (0);
3365 int i;
3366
3367 av_extend (av, items - 2);
3368 for (i = 1; i < items; ++i)
3369 av_push (av, SvREFCNT_inc_NN (ST (i)));
3370
3371 if ((SV *)hv == &PL_sv_undef)
2926 { 3372 {
2927 SV *old = PL_diehook; 3373 PUSHMARK (SP);
2928 PL_diehook = 0; 3374 EXTEND (SP, 2);
2929 SvREFCNT_dec (old); 3375 PUSHs (sv_Coro);
2930 croak ("\3async_pool terminate\2\n"); 3376 PUSHs ((SV *)cv_pool_handler);
3377 PUTBACK;
3378 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3379 SPAGAIN;
3380
3381 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2931 } 3382 }
2932 3383
2933 SvREFCNT_dec (coro->saved_deffh);
2934 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2935
2936 hv_store (hv, "desc", sizeof ("desc") - 1,
2937 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2938
2939 invoke_av = (AV *)SvRV (invoke);
2940 len = av_len (invoke_av);
2941
2942 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2943
2944 if (len > 0)
2945 { 3384 {
2946 av_fill (defav, len - 1); 3385 struct coro *coro = SvSTATE_hv (hv);
2947 for (i = 0; i < len; ++i) 3386
2948 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3387 assert (!coro->invoke_cb);
3388 assert (!coro->invoke_av);
3389 coro->invoke_cb = SvREFCNT_inc (cb);
3390 coro->invoke_av = av;
2949 } 3391 }
2950}
2951 3392
2952void 3393 api_ready (aTHX_ (SV *)hv);
2953_pool_2 (SV *cb)
2954 CODE:
2955{
2956 HV *hv = (HV *)SvRV (coro_current);
2957 struct coro *coro = SvSTATE_hv ((SV *)hv);
2958 3394
2959 sv_setsv (cb, &PL_sv_undef); 3395 if (GIMME_V != G_VOID)
2960 3396 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
2961 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3397 else
2962 coro->saved_deffh = 0;
2963
2964 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2965 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2966 {
2967 SV *old = PL_diehook;
2968 PL_diehook = 0;
2969 SvREFCNT_dec (old); 3398 SvREFCNT_dec (hv);
2970 croak ("\3async_pool terminate\2\n");
2971 }
2972
2973 av_clear (GvAV (PL_defgv));
2974 hv_store (hv, "desc", sizeof ("desc") - 1,
2975 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2976
2977 coro->prio = 0;
2978
2979 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2980 api_trace (aTHX_ coro_current, 0);
2981
2982 av_push (av_async_pool, newSVsv (coro_current));
2983} 3399}
2984 3400
2985SV * 3401SV *
2986rouse_cb () 3402rouse_cb ()
2987 PROTOTYPE: 3403 PROTOTYPE:
2989 RETVAL = coro_new_rouse_cb (aTHX); 3405 RETVAL = coro_new_rouse_cb (aTHX);
2990 OUTPUT: 3406 OUTPUT:
2991 RETVAL 3407 RETVAL
2992 3408
2993void 3409void
2994rouse_wait (SV *cb = 0) 3410rouse_wait (...)
2995 PROTOTYPE: ;$ 3411 PROTOTYPE: ;$
2996 PPCODE: 3412 PPCODE:
2997 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3413 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3414
3415void
3416on_enter (SV *block)
3417 ALIAS:
3418 on_leave = 1
3419 PROTOTYPE: &
3420 CODE:
3421{
3422 struct coro *coro = SvSTATE_current;
3423 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3424
3425 block = (SV *)coro_sv_2cv (aTHX_ block);
3426
3427 if (!*avp)
3428 *avp = newAV ();
3429
3430 av_push (*avp, SvREFCNT_inc (block));
3431
3432 if (!ix)
3433 on_enterleave_call (aTHX_ block);
3434
3435 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3436 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3437 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3438}
2998 3439
2999 3440
3000MODULE = Coro::State PACKAGE = PerlIO::cede 3441MODULE = Coro::State PACKAGE = PerlIO::cede
3001 3442
3002BOOT: 3443BOOT:
3013 GvSTASH (CvGV (cv)) 3454 GvSTASH (CvGV (cv))
3014 ); 3455 );
3015 OUTPUT: 3456 OUTPUT:
3016 RETVAL 3457 RETVAL
3017 3458
3018# helper for Coro::Channel 3459# helper for Coro::Channel and others
3019SV * 3460SV *
3020_alloc (int count) 3461_alloc (int count)
3021 CODE: 3462 CODE:
3022 RETVAL = coro_waitarray_new (aTHX_ count); 3463 RETVAL = coro_waitarray_new (aTHX_ count);
3023 OUTPUT: 3464 OUTPUT:
3036 adjust = 1 3477 adjust = 1
3037 CODE: 3478 CODE:
3038 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3479 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
3039 3480
3040void 3481void
3041down (SV *self) 3482down (...)
3042 CODE: 3483 CODE:
3043 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3484 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3044 3485
3045void 3486void
3046wait (SV *self) 3487wait (...)
3047 CODE: 3488 CODE:
3048 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); 3489 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
3049 3490
3050void 3491void
3051try (SV *self) 3492try (SV *self)
3081 for (i = 1; i <= wcount; ++i) 3522 for (i = 1; i <= wcount; ++i)
3082 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3523 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3083 } 3524 }
3084} 3525}
3085 3526
3527MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3528
3529void
3530_may_delete (SV *sem, int count, int extra_refs)
3531 PPCODE:
3532{
3533 AV *av = (AV *)SvRV (sem);
3534
3535 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3536 && AvFILLp (av) == 0 /* no waiters, just count */
3537 && SvIV (AvARRAY (av)[0]) == count)
3538 XSRETURN_YES;
3539
3540 XSRETURN_NO;
3541}
3542
3086MODULE = Coro::State PACKAGE = Coro::Signal 3543MODULE = Coro::State PACKAGE = Coro::Signal
3087 3544
3088SV * 3545SV *
3089new (SV *klass) 3546new (SV *klass)
3090 CODE: 3547 CODE:
3094 ); 3551 );
3095 OUTPUT: 3552 OUTPUT:
3096 RETVAL 3553 RETVAL
3097 3554
3098void 3555void
3099wait (SV *self) 3556wait (...)
3100 CODE: 3557 CODE:
3101 CORO_EXECUTE_SLF_XS (slf_init_signal_wait); 3558 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3102 3559
3103void 3560void
3104broadcast (SV *self) 3561broadcast (SV *self)
3161 3618
3162void 3619void
3163_register (char *target, char *proto, SV *req) 3620_register (char *target, char *proto, SV *req)
3164 CODE: 3621 CODE:
3165{ 3622{
3166 HV *st; 3623 CV *req_cv = coro_sv_2cv (aTHX_ req);
3167 GV *gvp;
3168 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
3169 /* newXSproto doesn't return the CV on 5.8 */ 3624 /* newXSproto doesn't return the CV on 5.8 */
3170 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3625 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3171 sv_setpv ((SV *)slf_cv, proto); 3626 sv_setpv ((SV *)slf_cv, proto);
3172 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3627 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3173} 3628}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines