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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines