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.18 by root, Fri Dec 29 11:37:49 2006 UTC vs.
Revision 1.23 by root, Tue Oct 9 22:29:27 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{
64 } 69 }
65 else 70 else
66 return 85197.73; /* this is as good as any value, but it factors badly with common values */ 71 return 85197.73; /* this is as good as any value, but it factors badly with common values */
67} 72}
68 73
74static SV *
75event_next (SV *watcher, int cancel, int wantev)
76{
77 pe_watcher *w = GEventAPI->sv_2watcher (watcher);
78 AV *priv = (AV *)w->ext_data;
79
80 if (AvARRAY (priv)[CD_OK] == &PL_sv_yes)
81 {
82 AvARRAY (priv)[CD_OK] = &PL_sv_no;
83
84 if (cancel)
85 GEventAPI->cancel (w);
86
87 if (wantev)
88 {
89 SV *ev = newRV_inc ((SV *)priv);
90
91 /* may need to bless it now */
92 if (!SvOBJECT (priv))
93 {
94 SvREADONLY_off ((SV *)priv);
95 sv_bless (ev, coro_event_event_stash);
96 SvREADONLY_on ((SV *)priv);
97 }
98
99 return sv_2mortal (ev);
100 }
101 else
102 return &PL_sv_undef;
103 }
104
105 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
106
107 if (!w->running)
108 GEventAPI->start (w, 1);
109
110 return 0; /* schedule */
111}
112
69MODULE = Coro::Event PACKAGE = Coro::Event 113MODULE = Coro::Event PACKAGE = Coro::Event
70 114
71PROTOTYPES: ENABLE 115PROTOTYPES: ENABLE
72 116
73BOOT: 117BOOT:
74{ 118{
119 coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE);
120
75 I_EVENT_API ("Coro::Event"); 121 I_EVENT_API ("Coro::Event");
76 I_CORO_API ("Coro::Event"); 122 I_CORO_API ("Coro::Event");
77 123
78 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0); 124 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
79 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0); 125 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
126
127 GCoroAPI->coro_event_next = event_next;
80} 128}
81 129
82void 130void
83_install_std_cb (SV *self, int type) 131_install_std_cb (SV *self, int type)
84 CODE: 132 CODE:
88 if (w->callback) 136 if (w->callback)
89 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught"); 137 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
90 138
91 { 139 {
92 AV *priv = newAV (); 140 AV *priv = newAV ();
93 SV *rv = newRV_noinc ((SV *)priv);
94 141
95 av_fill (priv, CD_MAX); 142 av_fill (priv, CD_MAX);
96 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* badbad */ 143 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */
97 AvARRAY (priv)[CD_TYPE] = newSViv (type); 144 AvARRAY (priv)[CD_TYPE] = newSViv (type);
98 AvARRAY (priv)[CD_OK ] = &PL_sv_no; 145 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
99 AvARRAY (priv)[CD_HITS] = newSViv (0); 146 AvARRAY (priv)[CD_HITS] = newSViv (0);
100 AvARRAY (priv)[CD_GOT ] = newSViv (0); 147 AvARRAY (priv)[CD_GOT ] = newSViv (0);
101 SvREADONLY_on (priv); 148 SvREADONLY_on (priv);
102 149
103 w->callback = coro_std_cb; 150 w->callback = coro_std_cb;
104 w->ext_data = priv; 151 w->ext_data = priv;
105 152
106 /* make sure Event does not use PERL_MAGIC_uvar, which */ 153 sv_magicext (SvRV (self), newRV_noinc ((SV *)priv), PERL_MAGIC_coro_event, 0, (char *)w, 0);
107 /* we abuse for non-uvar purposes. */
108 sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0);
109 } 154 }
110} 155}
111 156
112void
113_next (SV *self)
114 CODE:
115{
116 pe_watcher *w = GEventAPI->sv_2watcher (self);
117 AV *priv = (AV *)w->ext_data;
118
119 if (AvARRAY (priv)[CD_OK] == &PL_sv_yes)
120 {
121 AvARRAY (priv)[CD_OK] = &PL_sv_no;
122 XSRETURN_NO; /* got an event */
123 }
124
125 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
126
127 if (!w->running)
128 GEventAPI->start (w, 1);
129
130 XSRETURN_YES; /* schedule */
131}
132
133SV *
134_event (SV *self)
135 CODE:
136{
137 if (GIMME_V == G_VOID)
138 XSRETURN_EMPTY;
139
140 {
141 pe_watcher *w = GEventAPI->sv_2watcher (self);
142 AV *priv = (AV *)w->ext_data;
143
144 RETVAL = newRV_inc ((SV *)priv);
145 }
146}
147 OUTPUT:
148 RETVAL
149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines