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.193 by root, Fri Oct 5 23:38:40 2007 UTC vs.
Revision 1.194 by root, Sat Oct 6 00:08:04 2007 UTC

106#define IN_DESTRUCT (PL_main_cv == Nullcv) 106#define IN_DESTRUCT (PL_main_cv == Nullcv)
107 107
108#if __GNUC__ >= 3 108#if __GNUC__ >= 3
109# define attribute(x) __attribute__(x) 109# define attribute(x) __attribute__(x)
110# define BARRIER __asm__ __volatile__ ("" : : : "memory") 110# define BARRIER __asm__ __volatile__ ("" : : : "memory")
111# define expect(expr,value) __builtin_expect ((expr),(value))
111#else 112#else
112# define attribute(x) 113# define attribute(x)
113# define BARRIER 114# define BARRIER
115# define expect(expr,value) (expr)
114#endif 116#endif
117
118#define expect_false(expr) expect ((expr) != 0, 0)
119#define expect_true(expr) expect ((expr) != 0, 1)
115 120
116#define NOINLINE attribute ((noinline)) 121#define NOINLINE attribute ((noinline))
117 122
118#include "CoroAPI.h" 123#include "CoroAPI.h"
119 124
325 MAGIC *mg; 330 MAGIC *mg;
326 331
327 if (SvROK (coro)) 332 if (SvROK (coro))
328 coro = SvRV (coro); 333 coro = SvRV (coro);
329 334
330 if (SvTYPE (coro) != SVt_PVHV) 335 if (expect_false (SvTYPE (coro) != SVt_PVHV))
331 croak ("Coro::State object required"); 336 croak ("Coro::State object required");
332 337
333 stash = SvSTASH (coro); 338 stash = SvSTASH (coro);
334 if (stash != coro_stash && stash != coro_state_stash) 339 if (expect_false (stash != coro_stash && stash != coro_state_stash))
335 { 340 {
336 /* very slow, but rare, check */ 341 /* very slow, but rare, check */
337 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 342 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
338 croak ("Coro::State object required"); 343 croak ("Coro::State object required");
339 } 344 }
349get_padlist (pTHX_ CV *cv) 354get_padlist (pTHX_ CV *cv)
350{ 355{
351 MAGIC *mg = CORO_MAGIC (cv); 356 MAGIC *mg = CORO_MAGIC (cv);
352 AV *av; 357 AV *av;
353 358
354 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 359 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
355 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 360 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
356 else 361 else
357 { 362 {
358#if CORO_PREFER_PERL_FUNCTIONS 363#if CORO_PREFER_PERL_FUNCTIONS
359 /* this is probably cleaner, but also slower? */ 364 /* this is probably cleaner, but also slower? */
371put_padlist (pTHX_ CV *cv) 376put_padlist (pTHX_ CV *cv)
372{ 377{
373 MAGIC *mg = CORO_MAGIC (cv); 378 MAGIC *mg = CORO_MAGIC (cv);
374 AV *av; 379 AV *av;
375 380
376 if (!mg) 381 if (expect_false (!mg))
377 { 382 {
378 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 383 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
379 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 384 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
380 mg->mg_virtual = &vtbl_coro; 385 mg->mg_virtual = &vtbl_coro;
381 mg->mg_obj = (SV *)newAV (); 386 mg->mg_obj = (SV *)newAV ();
382 } 387 }
383 388
384 av = (AV *)mg->mg_obj; 389 av = (AV *)mg->mg_obj;
385 390
386 if (AvFILLp (av) >= AvMAX (av)) 391 if (expect_false (AvFILLp (av) >= AvMAX (av)))
387 av_extend (av, AvMAX (av) + 1); 392 av_extend (av, AvMAX (av) + 1);
388 393
389 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 394 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
390} 395}
391 396
406 { 411 {
407 dSP; 412 dSP;
408 CV *cv; 413 CV *cv;
409 414
410 /* now do the ugly restore mess */ 415 /* now do the ugly restore mess */
411 while ((cv = (CV *)POPs)) 416 while (expect_true (cv = (CV *)POPs))
412 { 417 {
413 put_padlist (aTHX_ cv); /* mark this padlist as available */ 418 put_padlist (aTHX_ cv); /* mark this padlist as available */
414 CvDEPTH (cv) = PTR2IV (POPs); 419 CvDEPTH (cv) = PTR2IV (POPs);
415 CvPADLIST (cv) = (AV *)POPs; 420 CvPADLIST (cv) = (AV *)POPs;
416 } 421 }
435 440
436 XPUSHs (Nullsv); 441 XPUSHs (Nullsv);
437 /* this loop was inspired by pp_caller */ 442 /* this loop was inspired by pp_caller */
438 for (;;) 443 for (;;)
439 { 444 {
440 while (cxix >= 0) 445 while (expect_true (cxix >= 0))
441 { 446 {
442 PERL_CONTEXT *cx = &ccstk[cxix--]; 447 PERL_CONTEXT *cx = &ccstk[cxix--];
443 448
444 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) 449 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT))
445 { 450 {
446 CV *cv = cx->blk_sub.cv; 451 CV *cv = cx->blk_sub.cv;
447 452
448 if (CvDEPTH (cv)) 453 if (expect_true (CvDEPTH (cv)))
449 { 454 {
450 EXTEND (SP, 3); 455 EXTEND (SP, 3);
451 PUSHs ((SV *)CvPADLIST (cv)); 456 PUSHs ((SV *)CvPADLIST (cv));
452 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 457 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
453 PUSHs ((SV *)cv); 458 PUSHs ((SV *)cv);
456 get_padlist (aTHX_ cv); 461 get_padlist (aTHX_ cv);
457 } 462 }
458 } 463 }
459 } 464 }
460 465
461 if (top_si->si_type == PERLSI_MAIN) 466 if (expect_true (top_si->si_type == PERLSI_MAIN))
462 break; 467 break;
463 468
464 top_si = top_si->si_prev; 469 top_si = top_si->si_prev;
465 ccstk = top_si->si_cxstack; 470 ccstk = top_si->si_cxstack;
466 cxix = top_si->si_cxix; 471 cxix = top_si->si_cxix;
669} 674}
670 675
671static void 676static void
672free_coro_mortal (pTHX) 677free_coro_mortal (pTHX)
673{ 678{
674 if (coro_mortal) 679 if (expect_true (coro_mortal))
675 { 680 {
676 SvREFCNT_dec (coro_mortal); 681 SvREFCNT_dec (coro_mortal);
677 coro_mortal = 0; 682 coro_mortal = 0;
678 } 683 }
679} 684}
936#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 941#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
937 942
938static coro_cctx * 943static coro_cctx *
939cctx_get (pTHX) 944cctx_get (pTHX)
940{ 945{
941 while (cctx_first) 946 while (expect_true (cctx_first))
942 { 947 {
943 coro_cctx *cctx = cctx_first; 948 coro_cctx *cctx = cctx_first;
944 cctx_first = cctx->next; 949 cctx_first = cctx->next;
945 --cctx_idle; 950 --cctx_idle;
946 951
947 if (!CCTX_EXPIRED (cctx)) 952 if (expect_true (!CCTX_EXPIRED (cctx)))
948 return cctx; 953 return cctx;
949 954
950 cctx_destroy (cctx); 955 cctx_destroy (cctx);
951 } 956 }
952 957
955 960
956static void 961static void
957cctx_put (coro_cctx *cctx) 962cctx_put (coro_cctx *cctx)
958{ 963{
959 /* free another cctx if overlimit */ 964 /* free another cctx if overlimit */
960 if (cctx_idle >= MAX_IDLE_CCTX) 965 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
961 { 966 {
962 coro_cctx *first = cctx_first; 967 coro_cctx *first = cctx_first;
963 cctx_first = first->next; 968 cctx_first = first->next;
964 --cctx_idle; 969 --cctx_idle;
965 970
971 cctx_first = cctx; 976 cctx_first = cctx;
972} 977}
973 978
974/** coroutine switching *****************************************************/ 979/** coroutine switching *****************************************************/
975 980
976static void NOINLINE 981static void
977transfer_check (pTHX_ struct coro *prev, struct coro *next) 982transfer_check (pTHX_ struct coro *prev, struct coro *next)
978{ 983{
979 if (prev != next) 984 if (expect_true (prev != next))
980 { 985 {
981 if (!(prev->flags & (CF_RUNNING | CF_NEW))) 986 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
982 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); 987 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
983 988
984 if (next->flags & CF_RUNNING) 989 if (expect_false (next->flags & CF_RUNNING))
985 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 990 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
986 991
987 if (next->flags & CF_DESTROYED) 992 if (expect_false (next->flags & CF_DESTROYED))
988 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 993 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
989 994
990 if (PL_lex_state != LEX_NOTPARSING) 995 if (expect_false (PL_lex_state != LEX_NOTPARSING))
991 croak ("Coro::State::transfer called while parsing, but this is not supported"); 996 croak ("Coro::State::transfer called while parsing, but this is not supported");
992 } 997 }
993} 998}
994 999
995/* always use the TRANSFER macro */ 1000/* always use the TRANSFER macro */
997transfer (pTHX_ struct coro *prev, struct coro *next) 1002transfer (pTHX_ struct coro *prev, struct coro *next)
998{ 1003{
999 dSTACKLEVEL; 1004 dSTACKLEVEL;
1000 1005
1001 /* sometimes transfer is only called to set idle_sp */ 1006 /* sometimes transfer is only called to set idle_sp */
1002 if (!next) 1007 if (expect_false (!next))
1003 { 1008 {
1004 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1009 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1005 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1010 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1006 } 1011 }
1007 else if (prev != next) 1012 else if (expect_true (prev != next))
1008 { 1013 {
1009 coro_cctx *prev__cctx; 1014 coro_cctx *prev__cctx;
1010 1015
1011 if (prev->flags & CF_NEW) 1016 if (expect_false (prev->flags & CF_NEW))
1012 { 1017 {
1013 /* create a new empty context */ 1018 /* create a new empty context */
1014 Newz (0, prev->cctx, 1, coro_cctx); 1019 Newz (0, prev->cctx, 1, coro_cctx);
1015 prev->flags &= ~CF_NEW; 1020 prev->flags &= ~CF_NEW;
1016 prev->flags |= CF_RUNNING; 1021 prev->flags |= CF_RUNNING;
1019 prev->flags &= ~CF_RUNNING; 1024 prev->flags &= ~CF_RUNNING;
1020 next->flags |= CF_RUNNING; 1025 next->flags |= CF_RUNNING;
1021 1026
1022 LOCK; 1027 LOCK;
1023 1028
1024 if (next->flags & CF_NEW) 1029 if (expect_false (next->flags & CF_NEW))
1025 { 1030 {
1026 /* need to start coroutine */ 1031 /* need to start coroutine */
1027 next->flags &= ~CF_NEW; 1032 next->flags &= ~CF_NEW;
1028 /* first get rid of the old state */ 1033 /* first get rid of the old state */
1029 save_perl (aTHX_ prev); 1034 save_perl (aTHX_ prev);
1038 } 1043 }
1039 1044
1040 prev__cctx = prev->cctx; 1045 prev__cctx = prev->cctx;
1041 1046
1042 /* possibly "free" the cctx */ 1047 /* possibly "free" the cctx */
1043 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) 1048 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1044 { 1049 {
1045 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1050 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1046 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1051 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1047 1052
1048 prev->cctx = 0; 1053 prev->cctx = 0;
1049 1054
1050 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1055 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1056 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1051 if (CCTX_EXPIRED (prev__cctx)) 1057 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1052 next->cctx = cctx_get (aTHX); 1058 next->cctx = cctx_get (aTHX);
1053 1059
1054 cctx_put (prev__cctx); 1060 cctx_put (prev__cctx);
1055 } 1061 }
1056 1062
1057 ++next->usecount; 1063 ++next->usecount;
1058 1064
1059 if (!next->cctx) 1065 if (expect_true (!next->cctx))
1060 next->cctx = cctx_get (aTHX); 1066 next->cctx = cctx_get (aTHX);
1061 1067
1062 if (prev__cctx != next->cctx) 1068 if (expect_false (prev__cctx != next->cctx))
1063 { 1069 {
1064 prev__cctx->top_env = PL_top_env; 1070 prev__cctx->top_env = PL_top_env;
1065 PL_top_env = next->cctx->top_env; 1071 PL_top_env = next->cctx->top_env;
1066 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1072 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1067 } 1073 }
1247 { 1253 {
1248 LOCK; 1254 LOCK;
1249 next_sv = coro_deq (aTHX_ PRIO_MIN); 1255 next_sv = coro_deq (aTHX_ PRIO_MIN);
1250 1256
1251 /* nothing to schedule: call the idle handler */ 1257 /* nothing to schedule: call the idle handler */
1252 if (!next_sv) 1258 if (expect_false (!next_sv))
1253 { 1259 {
1254 dSP; 1260 dSP;
1255 UNLOCK; 1261 UNLOCK;
1256 1262
1257 ENTER; 1263 ENTER;
1267 } 1273 }
1268 1274
1269 ta->next = SvSTATE (next_sv); 1275 ta->next = SvSTATE (next_sv);
1270 1276
1271 /* cannot transfer to destroyed coros, skip and look for next */ 1277 /* cannot transfer to destroyed coros, skip and look for next */
1272 if (ta->next->flags & CF_DESTROYED) 1278 if (expect_false (ta->next->flags & CF_DESTROYED))
1273 { 1279 {
1274 UNLOCK; 1280 UNLOCK;
1275 SvREFCNT_dec (next_sv); 1281 SvREFCNT_dec (next_sv);
1276 /* coro_nready is already taken care of by destroy */ 1282 /* coro_nready is already taken care of by destroy */
1277 continue; 1283 continue;
1333 dTHX; 1339 dTHX;
1334 struct transfer_args ta; 1340 struct transfer_args ta;
1335 1341
1336 prepare_cede (aTHX_ &ta); 1342 prepare_cede (aTHX_ &ta);
1337 1343
1338 if (ta.prev != ta.next) 1344 if (expect_true (ta.prev != ta.next))
1339 { 1345 {
1340 TRANSFER (ta); 1346 TRANSFER (ta);
1341 return 1; 1347 return 1;
1342 } 1348 }
1343 else 1349 else
1489 } 1495 }
1490 1496
1491 BARRIER; 1497 BARRIER;
1492 TRANSFER (ta); 1498 TRANSFER (ta);
1493 1499
1494 if (GIMME_V != G_VOID && ta.next != ta.prev) 1500 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1495 XSRETURN_YES; 1501 XSRETURN_YES;
1496} 1502}
1497 1503
1498bool 1504bool
1499_destroy (SV *coro_sv) 1505_destroy (SV *coro_sv)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines