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.124 by root, Tue Dec 5 12:50:04 2006 UTC vs.
Revision 1.131 by root, Fri Dec 29 11:37:49 2006 UTC

4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
23# ifndef PL_ppaddr
24# define PL_ppaddr ppaddr
25# endif
26# ifndef call_sv
27# define call_sv perl_call_sv
28# endif
29# ifndef get_sv
30# define get_sv perl_get_sv
31# endif
32# ifndef get_cv
33# define get_cv perl_get_cv
34# endif
35# ifndef IS_PADGV
36# define IS_PADGV(v) 0
37# endif
38# ifndef IS_PADCONST
39# define IS_PADCONST(v) 0
40# endif
41#endif
42
43#include <stdio.h> 9#include <stdio.h>
44#include <errno.h> 10#include <errno.h>
45#include <assert.h> 11#include <assert.h>
46
47#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
48# undef STACKGUARD
49#endif
50
51#ifndef STACKGUARD
52# define STACKGUARD 0
53#endif
54 12
55#ifdef HAVE_MMAP 13#ifdef HAVE_MMAP
56# include <unistd.h> 14# include <unistd.h>
57# include <sys/mman.h> 15# include <sys/mman.h>
58# ifndef MAP_ANONYMOUS 16# ifndef MAP_ANONYMOUS
73#else 31#else
74# define PAGESIZE 0 32# define PAGESIZE 0
75# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
76#endif 34#endif
77 35
36#if USE_VALGRIND
37# include <valgrind/valgrind.h>
38#endif
39
40/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8
42
43#define PERL_VERSION_ATLEAST(a,b,c) \
44 (PERL_REVISION > (a) \
45 || (PERL_REVISION == (a) \
46 && (PERL_VERSION > (b) \
47 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
48
49#if !PERL_VERSION_ATLEAST (5,6,0)
50# ifndef PL_ppaddr
51# define PL_ppaddr ppaddr
52# endif
53# ifndef call_sv
54# define call_sv perl_call_sv
55# endif
56# ifndef get_sv
57# define get_sv perl_get_sv
58# endif
59# ifndef get_cv
60# define get_cv perl_get_cv
61# endif
62# ifndef IS_PADGV
63# define IS_PADGV(v) 0
64# endif
65# ifndef IS_PADCONST
66# define IS_PADCONST(v) 0
67# endif
68#endif
69
70/* 5.8.7 */
71#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v)
73#endif
74
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD
77#endif
78
79#ifndef STACKGUARD
80# define STACKGUARD 0
81#endif
82
83/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0
86#endif
87
78/* The next macro should declare a variable stacklevel that contains and approximation 88/* The next macro should declare a variable stacklevel that contains and approximation
79 * to the current C stack pointer. Its property is that it changes with each call 89 * to the current C stack pointer. Its property is that it changes with each call
80 * and should be unique. */ 90 * and should be unique. */
81#define dSTACKLEVEL int stacklevel 91#define dSTACKLEVEL int stacklevel
82#define STACKLEVEL ((void *)&stacklevel) 92#define STACKLEVEL ((void *)&stacklevel)
83 93
84#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
85 95
86#if __GNUC__ >= 3 96#if __GNUC__ >= 3
87# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
88#else 99#else
89# define attribute(x) 100# define attribute(x)
101# define BARRIER
90#endif 102#endif
91 103
92#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
93 105
94#include "CoroAPI.h" 106#include "CoroAPI.h"
254 266
255 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 267 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
256 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 268 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
257 else 269 else
258 { 270 {
259#if 0 271#if PREFER_PERL_FUNCTIONS
260 /* this is probably cleaner, but also slower? */ 272 /* this is probably cleaner, but also slower? */
261 CV *cp = Perl_cv_clone (cv); 273 CV *cp = Perl_cv_clone (cv);
262 CvPADLIST (cv) = CvPADLIST (cp); 274 CvPADLIST (cv) = CvPADLIST (cp);
263 CvPADLIST (cp) = 0; 275 CvPADLIST (cp) = 0;
264 SvREFCNT_dec (cp); 276 SvREFCNT_dec (cp);
345 /* 357 /*
346 * the worst thing you can imagine happens first - we have to save 358 * the worst thing you can imagine happens first - we have to save
347 * (and reinitialize) all cv's in the whole callchain :( 359 * (and reinitialize) all cv's in the whole callchain :(
348 */ 360 */
349 361
362 EXTEND (SP, 3 + 1);
350 PUSHs (Nullsv); 363 PUSHs (Nullsv);
351 /* this loop was inspired by pp_caller */ 364 /* this loop was inspired by pp_caller */
352 for (;;) 365 for (;;)
353 { 366 {
354 while (cxix >= 0) 367 while (cxix >= 0)
360 CV *cv = cx->blk_sub.cv; 373 CV *cv = cx->blk_sub.cv;
361 374
362 if (CvDEPTH (cv)) 375 if (CvDEPTH (cv))
363 { 376 {
364 EXTEND (SP, 3); 377 EXTEND (SP, 3);
365
366 PUSHs ((SV *)CvPADLIST (cv)); 378 PUSHs ((SV *)CvPADLIST (cv));
367 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 379 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
368 PUSHs ((SV *)cv); 380 PUSHs ((SV *)cv);
369 381
370 CvDEPTH (cv) = 0; 382 CvDEPTH (cv) = 0;
371 get_padlist (cv); 383 get_padlist (cv);
372 } 384 }
373 } 385 }
374#ifdef CXt_FORMAT
375 else if (CxTYPE (cx) == CXt_FORMAT)
376 {
377 /* I never used formats, so how should I know how these are implemented? */
378 /* my bold guess is as a simple, plain sub... */
379 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
380 }
381#endif
382 } 386 }
383 387
384 if (top_si->si_type == PERLSI_MAIN) 388 if (top_si->si_type == PERLSI_MAIN)
385 break; 389 break;
386 390
406 * allocate various perl stacks. This is an exact copy 410 * allocate various perl stacks. This is an exact copy
407 * of perl.c:init_stacks, except that it uses less memory 411 * of perl.c:init_stacks, except that it uses less memory
408 * on the (sometimes correct) assumption that coroutines do 412 * on the (sometimes correct) assumption that coroutines do
409 * not usually need a lot of stackspace. 413 * not usually need a lot of stackspace.
410 */ 414 */
415#if PREFER_PERL_FUNCTIONS
416# define coro_init_stacks init_stacks
417#else
411static void 418static void
412coro_init_stacks () 419coro_init_stacks ()
413{ 420{
414 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 421 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
415 PL_curstackinfo->si_type = PERLSI_MAIN; 422 PL_curstackinfo->si_type = PERLSI_MAIN;
445 New(54,PL_retstack,16,OP*); 452 New(54,PL_retstack,16,OP*);
446 PL_retstack_ix = 0; 453 PL_retstack_ix = 0;
447 PL_retstack_max = 16; 454 PL_retstack_max = 16;
448#endif 455#endif
449} 456}
457#endif
450 458
451/* 459/*
452 * destroy the stacks, the callchain etc... 460 * destroy the stacks, the callchain etc...
453 */ 461 */
454static void 462static void
455coro_destroy_stacks () 463coro_destroy_stacks ()
456{ 464{
457 if (!IN_DESTRUCT) 465 if (!IN_DESTRUCT)
458 { 466 {
459 /* is this ugly, I ask? */ 467 /* restore all saved variables and stuff */
460 LEAVE_SCOPE (0); 468 LEAVE_SCOPE (0);
469 assert (PL_tmps_floor == -1);
461 470
462 /* sure it is, but more important: is it correct?? :/ */ 471 /* free all temporaries */
463 FREETMPS; 472 FREETMPS;
473 assert (PL_tmps_ix == -1);
464 474
465 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 475 POPSTACK_TO (PL_mainstack);
466 } 476 }
467 477
468 while (PL_curstackinfo->si_next) 478 while (PL_curstackinfo->si_next)
469 PL_curstackinfo = PL_curstackinfo->si_next; 479 PL_curstackinfo = PL_curstackinfo->si_next;
470 480
471 while (PL_curstackinfo) 481 while (PL_curstackinfo)
472 { 482 {
473 PERL_SI *p = PL_curstackinfo->si_prev; 483 PERL_SI *p = PL_curstackinfo->si_prev;
474 484
475 { /*D*//*remove*/
476 dSP;
477 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
478 PUTBACK; /* possibly superfluous */
479 }
480
481 if (!IN_DESTRUCT) 485 if (!IN_DESTRUCT)
482 {
483 dounwind (-1);/*D*//*remove*/
484 SvREFCNT_dec (PL_curstackinfo->si_stack); 486 SvREFCNT_dec (PL_curstackinfo->si_stack);
485 }
486 487
487 Safefree (PL_curstackinfo->si_cxstack); 488 Safefree (PL_curstackinfo->si_cxstack);
488 Safefree (PL_curstackinfo); 489 Safefree (PL_curstackinfo);
489 PL_curstackinfo = p; 490 PL_curstackinfo = p;
490 } 491 }
958 coro = SvSTATE (coro_sv); 959 coro = SvSTATE (coro_sv);
959 960
960 if (coro->flags & CF_READY) 961 if (coro->flags & CF_READY)
961 return 0; 962 return 0;
962 963
963#if 0 /* this is actually harmless */
964 if (coro->flags & CF_RUNNING)
965 croak ("Coro::ready called on currently running coroutine");
966#endif
967
968 coro->flags |= CF_READY; 964 coro->flags |= CF_READY;
969 965
970 LOCK; 966 LOCK;
971 coro_enq (SvREFCNT_inc (coro_sv)); 967 coro_enq (SvREFCNT_inc (coro_sv));
972 UNLOCK; 968 UNLOCK;
975} 971}
976 972
977static int 973static int
978api_is_ready (SV *coro_sv) 974api_is_ready (SV *coro_sv)
979{ 975{
980 return !!SvSTATE (coro_sv)->flags & CF_READY; 976 return !!(SvSTATE (coro_sv)->flags & CF_READY);
981} 977}
982 978
983static void 979static void
984prepare_schedule (struct transfer_args *ta) 980prepare_schedule (struct transfer_args *ta)
985{ 981{
1030 1026
1031static void 1027static void
1032prepare_cede (struct transfer_args *ta) 1028prepare_cede (struct transfer_args *ta)
1033{ 1029{
1034 api_ready (coro_current); 1030 api_ready (coro_current);
1035
1036 prepare_schedule (ta); 1031 prepare_schedule (ta);
1032}
1033
1034static int
1035prepare_cede_notself (struct transfer_args *ta)
1036{
1037 if (coro_nready)
1038 {
1039 SV *prev = SvRV (coro_current);
1040 prepare_schedule (ta);
1041 api_ready (prev);
1042 return 1;
1043 }
1044 else
1045 return 0;
1037} 1046}
1038 1047
1039static void 1048static void
1040api_schedule (void) 1049api_schedule (void)
1041{ 1050{
1043 1052
1044 prepare_schedule (&ta); 1053 prepare_schedule (&ta);
1045 TRANSFER (ta); 1054 TRANSFER (ta);
1046} 1055}
1047 1056
1048static void 1057static int
1049api_cede (void) 1058api_cede (void)
1050{ 1059{
1051 struct transfer_args ta; 1060 struct transfer_args ta;
1052 1061
1053 prepare_cede (&ta); 1062 prepare_cede (&ta);
1063
1064 if (ta.prev != ta.next)
1065 {
1054 TRANSFER (ta); 1066 TRANSFER (ta);
1067 return 1;
1068 }
1069 else
1070 return 0;
1071}
1072
1073static int
1074api_cede_notself (void)
1075{
1076 struct transfer_args ta;
1077
1078 if (prepare_cede_notself (&ta))
1079 {
1080 TRANSFER (ta);
1081 return 1;
1082 }
1083 else
1084 return 0;
1055} 1085}
1056 1086
1057MODULE = Coro::State PACKAGE = Coro::State 1087MODULE = Coro::State PACKAGE = Coro::State
1058 1088
1059PROTOTYPES: DISABLE 1089PROTOTYPES: DISABLE
1115_set_stacklevel (...) 1145_set_stacklevel (...)
1116 ALIAS: 1146 ALIAS:
1117 Coro::State::transfer = 1 1147 Coro::State::transfer = 1
1118 Coro::schedule = 2 1148 Coro::schedule = 2
1119 Coro::cede = 3 1149 Coro::cede = 3
1150 Coro::cede_notself = 4
1120 CODE: 1151 CODE:
1121{ 1152{
1122 struct transfer_args ta; 1153 struct transfer_args ta;
1123 1154
1124 switch (ix) 1155 switch (ix)
1140 break; 1171 break;
1141 1172
1142 case 3: 1173 case 3:
1143 prepare_cede (&ta); 1174 prepare_cede (&ta);
1144 break; 1175 break;
1176
1177 case 4:
1178 if (!prepare_cede_notself (&ta))
1179 XSRETURN_EMPTY;
1180
1181 break;
1145 } 1182 }
1146 1183
1184 BARRIER;
1147 TRANSFER (ta); 1185 TRANSFER (ta);
1148} 1186}
1149 1187
1150void 1188void
1151_clone_state_from (SV *dst, SV *src) 1189_clone_state_from (SV *dst, SV *src)
1202 coro_ready[i] = newAV (); 1240 coro_ready[i] = newAV ();
1203 1241
1204 { 1242 {
1205 SV *sv = perl_get_sv("Coro::API", 1); 1243 SV *sv = perl_get_sv("Coro::API", 1);
1206 1244
1207 coroapi.schedule = api_schedule; 1245 coroapi.schedule = api_schedule;
1208 coroapi.save = api_save; 1246 coroapi.save = api_save;
1209 coroapi.cede = api_cede; 1247 coroapi.cede = api_cede;
1248 coroapi.cede_notself = api_cede_notself;
1210 coroapi.ready = api_ready; 1249 coroapi.ready = api_ready;
1211 coroapi.is_ready = api_is_ready; 1250 coroapi.is_ready = api_is_ready;
1212 coroapi.nready = &coro_nready; 1251 coroapi.nready = &coro_nready;
1213 coroapi.current = coro_current; 1252 coroapi.current = coro_current;
1214 1253
1215 GCoroAPI = &coroapi; 1254 GCoroAPI = &coroapi;
1216 sv_setiv (sv, (IV)&coroapi); 1255 sv_setiv (sv, (IV)&coroapi);
1217 SvREADONLY_on (sv); 1256 SvREADONLY_on (sv);
1218 } 1257 }
1234 RETVAL = coro->prio; 1273 RETVAL = coro->prio;
1235 1274
1236 if (items > 1) 1275 if (items > 1)
1237 { 1276 {
1238 if (ix) 1277 if (ix)
1239 newprio += coro->prio; 1278 newprio = coro->prio - newprio;
1240 1279
1241 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1280 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1242 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1281 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1243 1282
1244 coro->prio = newprio; 1283 coro->prio = newprio;
1245 } 1284 }
1246} 1285}
1286 OUTPUT:
1287 RETVAL
1247 1288
1248SV * 1289SV *
1249ready (SV *self) 1290ready (SV *self)
1250 PROTOTYPE: $ 1291 PROTOTYPE: $
1251 CODE: 1292 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines