ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
(Generate patch)

Comparing Coro/Event/Event.xs (file contents):
Revision 1.17 by root, Mon Dec 4 22:05:49 2006 UTC vs.
Revision 1.25 by root, Thu Oct 25 08:14:00 2007 UTC

9#include "../Coro/CoroAPI.h" 9#include "../Coro/CoroAPI.h"
10 10
11#define CD_WAIT 0 /* wait queue */ 11#define CD_WAIT 0 /* wait queue */
12#define CD_TYPE 1 12#define CD_TYPE 1
13#define CD_OK 2 13#define CD_OK 2
14#define CD_HITS 3 /* hardcoded in Coro::Event */
15#define CD_GOT 4 /* hardcoded in Coro::Event, Coro::Handle */
16#define CD_MAX 4
14 17
15#define CD_HITS 4 /* hardcoded in Coro::Event */ 18static HV *coro_event_event_stash;
16#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */ 19
17#define CD_MAX 5 20#define PERL_MAGIC_coro_event 0x18 /* to avoid clashes with e.g. event */
18 21
19static void 22static void
20coro_std_cb (pe_event *pe) 23coro_std_cb (pe_event *pe)
21{ 24{
22 AV *priv = (AV *)pe->ext_data; 25 AV *priv = (AV *)pe->ext_data;
28 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0); 31 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0);
29 32
30 AvARRAY (priv)[CD_OK] = &PL_sv_yes; 33 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
31 34
32 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT]; 35 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
36
33 if ((coro = av_shift (cd_wait))) 37 coro = av_shift (cd_wait);
38 if (coro != &PL_sv_undef)
34 { 39 {
35 if (av_len (cd_wait) < 0)
36 GEventAPI->stop (pe->up, 0);
37
38 CORO_READY (coro); 40 CORO_READY (coro);
39 SvREFCNT_dec (coro); 41 SvREFCNT_dec (coro);
40 } 42 }
43
44 if (av_len (cd_wait) < 0)
45 GEventAPI->stop (pe->up, 0);
41} 46}
42 47
43static void 48static void
44asynccheck_hook (void *data) 49asynccheck_hook (void *data)
45{ 50{
46 /* ceding from C means allocating a stack, but we assume this is a rare case */ 51 /* this loops as long as we have _other_ coros with the same or higher priority */
52 while (CORO_NREADY && CORO_CEDE)
53 ;
54}
55
56static double
57prepare_hook (void *data)
58{
59 /* this yields once to another coro with any priority */
47 while (CORO_NREADY) 60 if (CORO_NREADY)
48 CORO_CEDE; 61 {
62 CORO_CEDE_NOTSELF;
63 /*
64 * timers might have changed, and Event fails to notice this
65 * so we have to assume the worst. If Event didn't have that bug,
66 * we would only need to do this if CORO_NREADY is != 0 now.
67 */
68 return 0.;
69 }
70 else
71 return 85197.73; /* this is as good as any value, but it factors badly with common values */
49} 72}
50 73
51MODULE = Coro::Event PACKAGE = Coro::Event 74MODULE = Coro::Event PACKAGE = Coro::Event
52 75
53PROTOTYPES: ENABLE 76PROTOTYPES: ENABLE
54 77
55BOOT: 78BOOT:
56{ 79{
80 coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE);
81
57 I_EVENT_API ("Coro::Event"); 82 I_EVENT_API ("Coro::Event");
58 I_CORO_API ("Coro::Event"); 83 I_CORO_API ("Coro::Event");
59 84
60 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0); 85 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
86 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
61} 87}
62 88
63void 89void
64_install_std_cb (SV *self, int type) 90_install_std_cb (SV *self, int type)
65 CODE: 91 CODE:
69 if (w->callback) 95 if (w->callback)
70 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught"); 96 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
71 97
72 { 98 {
73 AV *priv = newAV (); 99 AV *priv = newAV ();
74 SV *rv = newRV_noinc ((SV *)priv);
75 100
76 av_fill (priv, CD_MAX); 101 av_fill (priv, CD_MAX);
77 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* badbad */ 102 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */
78 AvARRAY (priv)[CD_TYPE] = newSViv (type); 103 AvARRAY (priv)[CD_TYPE] = newSViv (type);
79 AvARRAY (priv)[CD_OK ] = &PL_sv_no; 104 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
80 AvARRAY (priv)[CD_HITS] = newSViv (0); 105 AvARRAY (priv)[CD_HITS] = newSViv (0);
81 AvARRAY (priv)[CD_GOT ] = newSViv (0); 106 AvARRAY (priv)[CD_GOT ] = newSViv (0);
82 SvREADONLY_on (priv); 107 SvREADONLY_on (priv);
83 108
84 w->callback = coro_std_cb; 109 w->callback = coro_std_cb;
85 w->ext_data = priv; 110 w->ext_data = priv;
86 111
87 /* make sure Event does not use PERL_MAGIC_uvar, which */ 112 {
88 /* we abuse for non-uvar purposes. */ 113 SV *mob = newRV_noinc ((SV *)priv);
89 sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0); 114 sv_magicext (SvRV (self), mob, PERL_MAGIC_coro_event, 0, (char *)w, 0);
115 SvREFCNT_dec (mob); /* sv_magicext increments the refcount */
116 }
90 } 117 }
91} 118}
92 119
93void 120void
94_next (SV *self) 121_next (SV *self)
121 { 148 {
122 pe_watcher *w = GEventAPI->sv_2watcher (self); 149 pe_watcher *w = GEventAPI->sv_2watcher (self);
123 AV *priv = (AV *)w->ext_data; 150 AV *priv = (AV *)w->ext_data;
124 151
125 RETVAL = newRV_inc ((SV *)priv); 152 RETVAL = newRV_inc ((SV *)priv);
153
154 /* may need to bless it now */
155 if (!SvOBJECT (priv))
156 {
157 SvREADONLY_off ((SV *)priv);
158 sv_bless (RETVAL, coro_event_event_stash);
159 SvREADONLY_on ((SV *)priv);
160 }
126 } 161 }
127} 162}
128 OUTPUT: 163 OUTPUT:
129 RETVAL 164 RETVAL
130 165

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines