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.338 by root, Sun Dec 7 15:33:21 2008 UTC vs.
Revision 1.341 by root, Mon Dec 15 15:03:31 2008 UTC

238enum { 238enum {
239 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
240 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
241 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
242 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 */
243}; 244};
244 245
245/* 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 */
246typedef struct 247typedef struct
247{ 248{
285 /* async_pool */ 286 /* async_pool */
286 SV *saved_deffh; 287 SV *saved_deffh;
287 SV *invoke_cb; 288 SV *invoke_cb;
288 AV *invoke_av; 289 AV *invoke_av;
289 290
291 /* on_enter/on_leave */
292 AV *on_enter;
293 AV *on_leave;
294
290 /* linked list */ 295 /* linked list */
291 struct coro *next, *prev; 296 struct coro *next, *prev;
292}; 297};
293 298
294typedef struct coro *Coro__State; 299typedef struct coro *Coro__State;
352INLINE CV * 357INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv) 358coro_sv_2cv (pTHX_ SV *sv)
354{ 359{
355 HV *st; 360 HV *st;
356 GV *gvp; 361 GV *gvp;
357 return sv_2cv (sv, &st, &gvp, 0); 362 CV *cv = sv_2cv (sv, &st, &gvp, 0);
363
364 if (!cv)
365 croak ("code reference expected");
366
367 return cv;
358} 368}
359 369
360/*****************************************************************************/ 370/*****************************************************************************/
361/* magic glue */ 371/* magic glue */
362 372
521} 531}
522 532
523/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
524 534
525static void 535static void
536on_enterleave_call (pTHX_ SV *cb);
537
538static void
526load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
527{ 540{
528 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
529 c->slot = 0; 542 c->slot = 0;
530 543
556 PUTBACK; 569 PUTBACK;
557 } 570 }
558 571
559 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
560 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 }
561} 582}
562 583
563static void 584static void
564save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
565{ 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
566 c->except = CORO_THROW; 595 c->except = CORO_THROW;
567 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
568 597
569 { 598 {
570 dSP; 599 dSP;
1354 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1355 1384
1356 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1357 { 1386 {
1358 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1359 croak ("Coro::State::transfer called with a suspended 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,");
1360 1389
1361 if (expect_false (next->flags & CF_RUNNING))
1362 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1363
1364 if (expect_false (next->flags & CF_DESTROYED)) 1390 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1365 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,");
1366 1392
1367#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1368 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1369 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,");
1370#endif 1396#endif
1477 if (coro->mainstack 1503 if (coro->mainstack
1478 && coro->mainstack != main_mainstack 1504 && coro->mainstack != main_mainstack
1479 && coro->slot 1505 && coro->slot
1480 && !PL_dirty) 1506 && !PL_dirty)
1481 { 1507 {
1482 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1483 1509
1484 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1485 1511
1486 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1487 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1488 1514
1489 coro_destruct_perl (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1490 1516
1491 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1492 1518
1493 coro->slot = 0; 1519 coro->slot = 0;
1494 } 1520 }
1495 1521
1496 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1677 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1678 { 1704 {
1679 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1680 1706
1681 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1682 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1683 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1684 else 1710 else
1685 { 1711 {
1686 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1687 --coro_nready; 1713 --coro_nready;
2298 2324
2299 PL_op->op_ppaddr = pp_slf; 2325 PL_op->op_ppaddr = pp_slf;
2300 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2326 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2301 2327
2302 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));
2303} 2376}
2304 2377
2305/*****************************************************************************/ 2378/*****************************************************************************/
2306/* PerlIO::cede */ 2379/* PerlIO::cede */
2307 2380
3005 eval = 1 3078 eval = 1
3006 CODE: 3079 CODE:
3007{ 3080{
3008 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3081 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3009 { 3082 {
3010 struct coro temp; 3083 struct coro *current = SvSTATE_current;
3011 3084
3012 if (!(coro->flags & CF_RUNNING)) 3085 if (current != coro)
3013 { 3086 {
3014 PUTBACK; 3087 PUTBACK;
3015 save_perl (aTHX_ &temp); 3088 save_perl (aTHX_ current);
3016 load_perl (aTHX_ coro); 3089 load_perl (aTHX_ coro);
3090 SPAGAIN;
3017 } 3091 }
3018 3092
3019 {
3020 dSP;
3021 ENTER;
3022 SAVETMPS;
3023 PUTBACK;
3024 PUSHSTACK; 3093 PUSHSTACK;
3094
3025 PUSHMARK (SP); 3095 PUSHMARK (SP);
3096 PUTBACK;
3026 3097
3027 if (ix) 3098 if (ix)
3028 eval_sv (coderef, 0); 3099 eval_sv (coderef, 0);
3029 else 3100 else
3030 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3101 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3031 3102
3103 SPAGAIN;
3032 POPSTACK; 3104 POPSTACK;
3033 SPAGAIN;
3034 FREETMPS;
3035 LEAVE;
3036 PUTBACK;
3037 }
3038 3105
3039 if (!(coro->flags & CF_RUNNING)) 3106 if (current != coro)
3040 { 3107 {
3108 PUTBACK;
3041 save_perl (aTHX_ coro); 3109 save_perl (aTHX_ coro);
3042 load_perl (aTHX_ &temp); 3110 load_perl (aTHX_ current);
3043 SPAGAIN; 3111 SPAGAIN;
3044 } 3112 }
3045 } 3113 }
3046} 3114}
3047 3115
3051 ALIAS: 3119 ALIAS:
3052 is_ready = CF_READY 3120 is_ready = CF_READY
3053 is_running = CF_RUNNING 3121 is_running = CF_RUNNING
3054 is_new = CF_NEW 3122 is_new = CF_NEW
3055 is_destroyed = CF_DESTROYED 3123 is_destroyed = CF_DESTROYED
3124 is_suspended = CF_SUSPENDED
3056 CODE: 3125 CODE:
3057 RETVAL = boolSV (coro->flags & ix); 3126 RETVAL = boolSV (coro->flags & ix);
3058 OUTPUT: 3127 OUTPUT:
3059 RETVAL 3128 RETVAL
3060 3129
3125 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3194 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3126 3195
3127 SV *tmp = *src; *src = *dst; *dst = tmp; 3196 SV *tmp = *src; *src = *dst; *dst = tmp;
3128 } 3197 }
3129 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
3130 3205
3131MODULE = Coro::State PACKAGE = Coro 3206MODULE = Coro::State PACKAGE = Coro
3132 3207
3133BOOT: 3208BOOT:
3134{ 3209{
3208cede_notself (...) 3283cede_notself (...)
3209 CODE: 3284 CODE:
3210 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3285 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3211 3286
3212void 3287void
3213_cancel (Coro::State self)
3214 CODE:
3215 coro_state_destroy (aTHX_ self);
3216 coro_call_on_destroy (aTHX_ self);
3217
3218void
3219_set_current (SV *current) 3288_set_current (SV *current)
3220 PROTOTYPE: $ 3289 PROTOTYPE: $
3221 CODE: 3290 CODE:
3222 SvREFCNT_dec (SvRV (coro_current)); 3291 SvREFCNT_dec (SvRV (coro_current));
3223 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3292 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3329rouse_wait (...) 3398rouse_wait (...)
3330 PROTOTYPE: ;$ 3399 PROTOTYPE: ;$
3331 PPCODE: 3400 PPCODE:
3332 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3401 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3333 3402
3403void
3404on_enter (SV *block)
3405 ALIAS:
3406 on_leave = 1
3407 PROTOTYPE: &
3408 CODE:
3409{
3410 struct coro *coro = SvSTATE_current;
3411 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3412
3413 block = (SV *)coro_sv_2cv (aTHX_ block);
3414
3415 if (!*avp)
3416 *avp = newAV ();
3417
3418 av_push (*avp, SvREFCNT_inc (block));
3419
3420 if (!ix)
3421 on_enterleave_call (aTHX_ block);
3422
3423 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3424 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3425 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3426}
3427
3334 3428
3335MODULE = Coro::State PACKAGE = PerlIO::cede 3429MODULE = Coro::State PACKAGE = PerlIO::cede
3336 3430
3337BOOT: 3431BOOT:
3338 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3432 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines