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.4 by root, Fri Aug 17 03:33:00 2001 UTC vs.
Revision 1.22 by root, Sun Sep 23 21:52:04 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
7#include "EventAPI.h" 8#include "EventAPI.h"
8#include "../Coro/CoroAPI.h" 9#include "../Coro/CoroAPI.h"
9 10
10#define CD_CORO 0 11#define CD_WAIT 0 /* wait queue */
11#define CD_TYPE 1 12#define CD_TYPE 1
12#define CD_OK 2 13#define CD_OK 2
14#define CD_HITS 3 /* hardcoded in Coro::Event */
13#define CD_GOT 3 /* hardcoded in Coro::Event, Coro::Handle */ 15#define CD_GOT 4 /* hardcoded in Coro::Event, Coro::Handle */
14#define CD_MAX 3 16#define CD_MAX 4
15/* no support for hits and prio so far. */
16 17
17#define EV_CLASS "Coro::Event" 18static HV *coro_event_event_stash;
18 19
19static pe_idle *scheduler; 20#define PERL_MAGIC_coro_event 0x18 /* to avoid clashes with e.g. event */
20static int do_schedule;
21
22#define NEED_SCHEDULE if (!do_schedule) \
23 { \
24 do_schedule = 1; \
25 GEventAPI->now ((pe_watcher *)scheduler); \
26 }
27 21
28static void 22static void
29coro_std_cb(pe_event *pe) 23coro_std_cb (pe_event *pe)
30{ 24{
31 AV *priv = (AV *)SvRV ((SV *)pe->ext_data); 25 AV *priv = (AV *)pe->ext_data;
32 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1)); 26 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
33 SV *cd_coro = *av_fetch (priv, CD_CORO, 1); 27 AV *cd_wait;
28 SV *coro;
34 29
35 if (type == 1) 30 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
36 av_store (priv, CD_GOT, newSViv (((pe_ioevent *)pe)->got)); 31 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0);
37 32
38 if (SvROK (cd_coro)) 33 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
34
35 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
36
37 coro = av_shift (cd_wait);
38 if (coro != &PL_sv_undef)
39 { 39 {
40 CORO_READY (cd_coro); 40 CORO_READY (coro);
41 av_store (priv, CD_CORO, &PL_sv_undef); 41 SvREFCNT_dec (coro);
42 NEED_SCHEDULE;
43 } 42 }
44 else 43
45 { 44 if (av_len (cd_wait) < 0)
46 av_store (priv, CD_OK, &PL_sv_yes);
47 GEventAPI->stop (pe->up, 0); 45 GEventAPI->stop (pe->up, 0);
48 }
49} 46}
50 47
51static void 48static void
52scheduler_cb(pe_event *pe) 49asynccheck_hook (void *data)
53{ 50{
51 /* this loops as long as we have _other_ coros with the same or higher priority */
54 while (CORO_NREADY) 52 while (CORO_NREADY && CORO_CEDE)
55 CORO_CEDE; 53 ;
54}
56 55
57 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 */
58} 72}
59 73
60MODULE = Coro::Event PACKAGE = Coro::Event 74MODULE = Coro::Event PACKAGE = Coro::Event
61 75
62PROTOTYPES: ENABLE 76PROTOTYPES: ENABLE
63 77
64BOOT: 78BOOT:
65{ 79{
80 coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE);
81
66 I_EVENT_API("Coro::Event"); 82 I_EVENT_API ("Coro::Event");
67 I_CORO_API ("Coro::Event"); 83 I_CORO_API ("Coro::Event");
68 84
69 /* create a fake idle handler (we only ever call now) */ 85 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
70 scheduler = GEventAPI->new_idle (0, 0); 86 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
71 scheduler->base.callback = scheduler_cb;
72 scheduler->min_interval = newSVnv (0);
73 scheduler->max_interval = newSVnv (0);
74 GEventAPI->stop ((pe_watcher *)scheduler, 0);
75} 87}
76 88
77void 89void
78_install_std_cb(self,type) 90_install_std_cb (SV *self, int type)
79 SV * self
80 int type
81 CODE: 91 CODE:
92{
82 pe_watcher *w = GEventAPI->sv_2watcher (self); 93 pe_watcher *w = GEventAPI->sv_2watcher (self);
83 AV *priv = newAV ();
84 SV *rv = newRV_noinc ((SV *)priv);
85 94
86 av_extend (priv, CD_MAX); 95 if (w->callback)
87 av_store (priv, CD_TYPE, newSViv (type)); 96 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
88 97
89 w->callback = coro_std_cb; 98 {
90 w->ext_data = rv; 99 AV *priv = newAV ();
91 100
92 hv_store ((HV *)SvRV (self), 101 av_fill (priv, CD_MAX);
93 EV_CLASS, strlen (EV_CLASS), 102 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */
94 rv, 0); 103 AvARRAY (priv)[CD_TYPE] = newSViv (type);
104 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
105 AvARRAY (priv)[CD_HITS] = newSViv (0);
106 AvARRAY (priv)[CD_GOT ] = newSViv (0);
107 SvREADONLY_on (priv);
108
109 w->callback = coro_std_cb;
110 w->ext_data = priv;
111
112 sv_magicext (SvRV (self), newRV_noinc ((SV *)priv), PERL_MAGIC_coro_event, 0, (char *)w, 0);
113 }
114}
95 115
96void 116void
97_next(self) 117_next (SV *self)
98 SV * self
99 CODE: 118 CODE:
119{
100 pe_watcher *w = GEventAPI->sv_2watcher (self); 120 pe_watcher *w = GEventAPI->sv_2watcher (self);
101 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 121 AV *priv = (AV *)w->ext_data;
102 122
103 if (SvOK (*av_fetch (priv, CD_CORO, 1))) 123 if (AvARRAY (priv)[CD_OK] == &PL_sv_yes)
104 croak ("only one coroutine can wait for an event"); 124 {
125 AvARRAY (priv)[CD_OK] = &PL_sv_no;
126 XSRETURN_NO; /* got an event */
127 }
128
129 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
105 130
106 if (!w->running) 131 if (!w->running)
107 GEventAPI->start (w, 1); 132 GEventAPI->start (w, 1);
108 133
109 if (*av_fetch (priv, CD_OK, 1) == &PL_sv_yes) 134 XSRETURN_YES; /* schedule */
110 { 135}
111 av_store (priv, CD_OK, &PL_sv_no);
112 XSRETURN_NO;
113 }
114 else
115 {
116 av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT));
117 XSRETURN_YES;
118 }
119 136
137SV *
138_event (SV *self)
139 CODE:
140{
141 if (GIMME_V == G_VOID)
142 XSRETURN_EMPTY;
143
144 {
145 pe_watcher *w = GEventAPI->sv_2watcher (self);
146 AV *priv = (AV *)w->ext_data;
147
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 }
157 }
158}
159 OUTPUT:
160 RETVAL
161

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines