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.3 by root, Fri Aug 17 01:45:39 2001 UTC vs.
Revision 1.18 by root, Fri Dec 29 11:37:49 2006 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_W 2 13#define CD_OK 2
13#define CD_GOT 3
14#define CD_MAX 3
15/* no support for hits and prio so far. */
16 14
17#define EV_CLASS "Coro::Event::Ev" 15#define CD_HITS 4 /* hardcoded in Coro::Event */
16#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
17#define CD_MAX 5
18 18
19static HV *ev_stash; 19static void
20static pe_idle *scheduler; 20coro_std_cb (pe_event *pe)
21static int do_schedule; 21{
22 AV *priv = (AV *)pe->ext_data;
23 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
24 AV *cd_wait;
25 SV *coro;
22 26
23#define NEED_SCHEDULE if (!do_schedule) \ 27 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
24 { \ 28 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0);
25 do_schedule = 1; \
26 GEventAPI->now ((pe_watcher *)scheduler); \
27 }
28static void
29coro_std_cb(pe_event *pe)
30{
31 AV *av = (AV *)SvRV ((SV *)pe->ext_data);
32 IV type = SvIV (*av_fetch (av, CD_TYPE, 1));
33 SV *cd_coro = *av_fetch (av, CD_CORO, 1);
34 29
35 av_store (av, CD_W, SvREFCNT_inc (pe->up->mysv)); 30 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
36 31
37 if (type == 1) 32 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
38 av_store (av, CD_GOT, newSViv (((pe_ioevent *)pe)->got)); 33 if ((coro = av_shift (cd_wait)))
34 {
35 if (av_len (cd_wait) < 0)
36 GEventAPI->stop (pe->up, 0);
39 37
40 GEventAPI->stop (pe->up, 0);
41
42 if (SvROK (cd_coro))
43 {
44 CORO_READY (cd_coro); 38 CORO_READY (coro);
45 NEED_SCHEDULE; 39 SvREFCNT_dec (coro);
46 } 40 }
47} 41}
48 42
49static void 43static void
50scheduler_cb(pe_event *pe) 44asynccheck_hook (void *data)
51{ 45{
46 /* this loops as long as we have _other_ coros with the same or higher priority */
52 while (CORO_NREADY) 47 while (CORO_NREADY && CORO_CEDE)
53 CORO_CEDE; 48 ;
49}
54 50
55 do_schedule = 0; 51static double
52prepare_hook (void *data)
53{
54 /* this yields once to another coro with any priority */
55 if (CORO_NREADY)
56 {
57 CORO_CEDE_NOTSELF;
58 /*
59 * timers might have changed, and Event fails to notice this
60 * so we have to assume the worst. If Event didn't have that bug,
61 * we would only need to do this if CORO_NREADY is != 0 now.
62 */
63 return 0.;
64 }
65 else
66 return 85197.73; /* this is as good as any value, but it factors badly with common values */
56} 67}
57 68
58MODULE = Coro::Event PACKAGE = Coro::Event 69MODULE = Coro::Event PACKAGE = Coro::Event
59 70
60PROTOTYPES: ENABLE 71PROTOTYPES: ENABLE
61 72
62BOOT: 73BOOT:
63{ 74{
64 I_EVENT_API("Coro::Event"); 75 I_EVENT_API ("Coro::Event");
65 I_CORO_API ("Coro::Event"); 76 I_CORO_API ("Coro::Event");
66 77
67 ev_stash = gv_stashpv (EV_CLASS, TRUE); 78 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
68 79 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
69 /* create a fake idle handler (we only ever call now) */
70 scheduler = GEventAPI->new_idle (0, 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} 80}
76 81
77void 82void
78_install_std_cb(self,type) 83_install_std_cb (SV *self, int type)
79 SV * self
80 int type
81 CODE: 84 CODE:
85{
82 pe_watcher *w = GEventAPI->sv_2watcher (self); 86 pe_watcher *w = GEventAPI->sv_2watcher (self);
83 AV *priv = newAV ();
84 SV *rv;
85 87
86 av_extend (priv, CD_MAX); 88 if (w->callback)
87 av_store (priv, CD_TYPE, newSViv (type)); 89 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
88 90
89 rv = sv_bless (newRV_noinc ((SV *)priv), ev_stash); 91 {
92 AV *priv = newAV ();
93 SV *rv = newRV_noinc ((SV *)priv);
90 94
91 hv_store ((HV *)SvRV (self), 95 av_fill (priv, CD_MAX);
92 EV_CLASS, strlen (EV_CLASS), 96 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* badbad */
93 rv, 0); 97 AvARRAY (priv)[CD_TYPE] = newSViv (type);
98 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
99 AvARRAY (priv)[CD_HITS] = newSViv (0);
100 AvARRAY (priv)[CD_GOT ] = newSViv (0);
101 SvREADONLY_on (priv);
94 102
95 w->ext_data = rv;
96 w->callback = coro_std_cb; 103 w->callback = coro_std_cb;
104 w->ext_data = priv;
105
106 /* make sure Event does not use PERL_MAGIC_uvar, which */
107 /* we abuse for non-uvar purposes. */
108 sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0);
109 }
110}
97 111
98void 112void
99_next0(self) 113_next (SV *self)
100 SV * self
101 CODE: 114 CODE:
115{
102 pe_watcher *w = GEventAPI->sv_2watcher (self); 116 pe_watcher *w = GEventAPI->sv_2watcher (self);
103 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 117 AV *priv = (AV *)w->ext_data;
104 118
105 GEventAPI->start (w, 1); 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 }
106 124
107 if (SvROK (*av_fetch (priv, CD_CORO, 1))) 125 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
108 croak ("only one coroutine can wait for an event");
109 126
110 av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT)); 127 if (!w->running)
128 GEventAPI->start (w, 1);
129
130 XSRETURN_YES; /* schedule */
131}
111 132
112SV * 133SV *
113_next1(self) 134_event (SV *self)
114 SV * self 135 CODE:
115 CODE: 136{
116 pe_watcher *w = GEventAPI->sv_2watcher (self); 137 if (GIMME_V == G_VOID)
117 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 138 XSRETURN_EMPTY;
118 139
119 av_store (priv, CD_CORO, &PL_sv_undef); 140 {
141 pe_watcher *w = GEventAPI->sv_2watcher (self);
142 AV *priv = (AV *)w->ext_data;
120 143
121 RETVAL = SvREFCNT_inc ((SV *)w->ext_data); 144 RETVAL = newRV_inc ((SV *)priv);
145 }
146}
122 OUTPUT: 147 OUTPUT:
123 RETVAL 148 RETVAL
124 149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines