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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines