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.9 by root, Sat Mar 22 18:50:32 2003 UTC vs.
Revision 1.23 by root, Tue Oct 9 22:29:27 2007 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <assert.h>
5#include <string.h> 6#include <string.h>
6
7/* this useful idiom is unfortunately missing... */
8static void
9confess (const char *msg)
10{
11 dSP;
12
13 PUSHMARK(SP);
14 XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0)));
15 PUTBACK;
16 call_pv ("Carp::confess", G_VOID);
17}
18 7
19#include "EventAPI.h" 8#include "EventAPI.h"
20#include "../Coro/CoroAPI.h" 9#include "../Coro/CoroAPI.h"
21 10
22#ifndef PE_PERLCB 11#define CD_WAIT 0 /* wait queue */
23# define PE_PERLCB 0x020 /* not public, but we need it :( */
24#endif
25
26#define CD_CORO 0
27#define CD_TYPE 1 12#define CD_TYPE 1
28#define CD_OK 2 13#define CD_OK 2
29#define CD_PRIO 3 /* hardcoded in Coro::Event */
30#define CD_HITS 4 /* hardcoded in Coro::Event */ 14#define CD_HITS 3 /* hardcoded in Coro::Event */
31#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */ 15#define CD_GOT 4 /* hardcoded in Coro::Event, Coro::Handle */
32#define CD_MAX 5 16#define CD_MAX 4
33 17
34#define EV_CLASS "Coro::Event" 18static HV *coro_event_event_stash;
35 19
36static pe_idle *scheduler; 20#define PERL_MAGIC_coro_event 0x18 /* to avoid clashes with e.g. event */
37static int do_schedule;
38
39#define NEED_SCHEDULE if (!do_schedule) \
40 { \
41 do_schedule = 1; \
42 GEventAPI->now ((pe_watcher *)scheduler); \
43 }
44 21
45static void 22static void
46coro_std_cb(pe_event *pe) 23coro_std_cb (pe_event *pe)
47{ 24{
48 AV *priv = (AV *)pe->ext_data; 25 AV *priv = (AV *)pe->ext_data;
49 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1)); 26 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
50 SV **cd_coro = &AvARRAY(priv)[CD_CORO]; 27 AV *cd_wait;
28 SV *coro;
51 29
52 sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
53 sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits); 30 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
31 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0);
54 32
55 if (type == 1) 33 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
56 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
57 34
35 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
36
37 coro = av_shift (cd_wait);
58 if (*cd_coro != &PL_sv_undef) 38 if (coro != &PL_sv_undef)
59 { 39 {
60 CORO_READY (*cd_coro); 40 CORO_READY (coro);
61 SvREFCNT_dec (*cd_coro); 41 SvREFCNT_dec (coro);
62 *cd_coro = &PL_sv_undef;
63 NEED_SCHEDULE;
64 } 42 }
65 else 43
66 { 44 if (av_len (cd_wait) < 0)
67 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
68 GEventAPI->stop (pe->up, 0); 45 GEventAPI->stop (pe->up, 0);
69 }
70} 46}
71 47
72static void 48static void
73scheduler_cb(pe_event *pe) 49asynccheck_hook (void *data)
74{ 50{
51 /* this loops as long as we have _other_ coros with the same or higher priority */
75 while (CORO_NREADY) 52 while (CORO_NREADY && CORO_CEDE)
76 CORO_CEDE; 53 ;
54}
77 55
78 do_schedule = 0; 56static double
57prepare_hook (void *data)
58{
59 /* this yields once to another coro with any priority */
60 if (CORO_NREADY)
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 */
72}
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 */
79} 111}
80 112
81MODULE = Coro::Event PACKAGE = Coro::Event 113MODULE = Coro::Event PACKAGE = Coro::Event
82 114
83PROTOTYPES: ENABLE 115PROTOTYPES: ENABLE
84 116
85BOOT: 117BOOT:
86{ 118{
119 coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE);
120
87 I_EVENT_API ("Coro::Event"); 121 I_EVENT_API ("Coro::Event");
88 I_CORO_API ("Coro::Event"); 122 I_CORO_API ("Coro::Event");
89 123
90 /* create a fake idle handler (we only ever call now) */ 124 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
91 scheduler = GEventAPI->new_idle (0, 0); 125 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
92 scheduler->base.callback = scheduler_cb; 126
93 scheduler->base.prio = PE_PRIO_NORMAL; /* StarvePrio */ 127 GCoroAPI->coro_event_next = event_next;
94 scheduler->min_interval = newSVnv (0);
95 scheduler->max_interval = newSVnv (0);
96 GEventAPI->stop ((pe_watcher *)scheduler, 0);
97} 128}
98 129
99void 130void
100_install_std_cb(self,type) 131_install_std_cb (SV *self, int type)
101 SV * self
102 int type
103 CODE: 132 CODE:
133{
104 pe_watcher *w = GEventAPI->sv_2watcher (self); 134 pe_watcher *w = GEventAPI->sv_2watcher (self);
105 135
106 if (WaFLAGS (w) & PE_PERLCB) 136 if (w->callback)
107 croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught"); 137 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
138
108 { 139 {
109 AV *priv = newAV (); 140 AV *priv = newAV ();
110 SV *rv = newRV_noinc ((SV *)priv);
111 141
112 av_extend (priv, CD_MAX); 142 av_fill (priv, CD_MAX);
113 av_store (priv, CD_CORO, &PL_sv_undef); 143 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */
114 av_store (priv, CD_TYPE, newSViv (type)); 144 AvARRAY (priv)[CD_TYPE] = newSViv (type);
115 av_store (priv, CD_OK , &PL_sv_no); 145 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
116 av_store (priv, CD_PRIO, newSViv (0));
117 av_store (priv, CD_HITS, newSViv (0)); 146 AvARRAY (priv)[CD_HITS] = newSViv (0);
118 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef); 147 AvARRAY (priv)[CD_GOT ] = newSViv (0);
119 SvREADONLY_on (priv); 148 SvREADONLY_on (priv);
120 149
121 w->callback = coro_std_cb; 150 w->callback = coro_std_cb;
122 w->ext_data = priv; 151 w->ext_data = priv;
123 152
124 hv_store ((HV *)SvRV (self), 153 sv_magicext (SvRV (self), newRV_noinc ((SV *)priv), PERL_MAGIC_coro_event, 0, (char *)w, 0);
125 EV_CLASS, strlen (EV_CLASS), 154 }
126 rv, 0); 155}
127 156
128 GEventAPI->start (w, 0);
129 }
130
131void
132_next(self)
133 SV * self
134 CODE:
135 pe_watcher *w = GEventAPI->sv_2watcher (self);
136 AV *priv = (AV *)w->ext_data;
137
138 if (!w->running)
139 GEventAPI->start (w, 1);
140
141 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes)
142 {
143 AvARRAY(priv)[CD_OK] = &PL_sv_no;
144 XSRETURN_NO;
145 }
146 else
147 {
148 if (AvARRAY(priv)[CD_CORO] != &PL_sv_undef)
149 confess ("only one coroutine can wait for an event");
150
151 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
152 XSRETURN_YES;
153 }
154
155MODULE = Coro::Event PACKAGE = Coro
156
157# overwrite the ready function
158void
159ready(self)
160 SV * self
161 PROTOTYPE: $
162 CODE:
163 NEED_SCHEDULE;
164 CORO_READY (self);
165
166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines