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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines