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.10 by root, Thu Nov 23 23:54:02 2006 UTC vs.
Revision 1.26 by root, Sat Jun 20 08:58:00 2009 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;
19
20#define PERL_MAGIC_coro_event 0x18 /* to avoid clashes with e.g. event */
35 21
36static void 22static void
37coro_std_cb(pe_event *pe) 23coro_std_cb (pe_event *pe)
38{ 24{
39 AV *priv = (AV *)pe->ext_data; 25 AV *priv = (AV *)pe->ext_data;
40 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1)); 26 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
41 SV **cd_coro = &AvARRAY(priv)[CD_CORO]; 27 AV *cd_wait;
28 SV *coro;
42 29
43 sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
44 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);
45 32
46 if (type == 1) 33 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
47 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
48 34
35 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
36
37 coro = av_shift (cd_wait);
49 if (*cd_coro != &PL_sv_undef) 38 if (coro != &PL_sv_undef)
50 { 39 {
51 CORO_READY (*cd_coro); 40 CORO_READY (coro);
52 SvREFCNT_dec (*cd_coro); 41 SvREFCNT_dec (coro);
53 *cd_coro = &PL_sv_undef;
54 } 42 }
55 else 43
56 { 44 if (av_len (cd_wait) < 0)
57 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
58 GEventAPI->stop (pe->up, 0); 45 GEventAPI->stop (pe->up, 0);
46}
47
48static void
49asynccheck_hook (void *data)
50{
51 /* this loops as long as we have _other_ coros with the same or higher priority */
52 while (CORO_NREADY && CORO_CEDE)
59 } 53 ;
60} 54}
61 55
62static double 56static double
63prepare_hook (void *data) 57prepare_hook (void *data)
64{ 58{
59 /* this yields once to another coro with any priority */
65 while (CORO_NREADY) 60 if (CORO_NREADY)
66 CORO_CEDE; 61 {
67 62 CORO_CEDE_NOTSELF;
68 return 1e20; 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 */
69} 72}
70 73
71MODULE = Coro::Event PACKAGE = Coro::Event 74MODULE = Coro::Event PACKAGE = Coro::Event
72 75
73PROTOTYPES: ENABLE 76PROTOTYPES: ENABLE
74 77
75BOOT: 78BOOT:
76{ 79{
80 coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE);
81
77 I_EVENT_API ("Coro::Event"); 82 I_EVENT_API ("Coro::Event");
78 I_CORO_API ("Coro::Event"); 83 I_CORO_API ("Coro::Event");
79 84
85 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
80 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0); 86 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
81} 87}
82 88
83void 89void
84_install_std_cb(self,type) 90_install_std_cb (SV *self, int type)
85 SV * self
86 int type
87 CODE: 91 CODE:
92{
88 pe_watcher *w = GEventAPI->sv_2watcher (self); 93 pe_watcher *w = GEventAPI->sv_2watcher (self);
89 94
90 if (WaFLAGS (w) & PE_PERLCB) 95 if (w->callback)
91 croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught"); 96 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
97
92 { 98 {
93 AV *priv = newAV (); 99 AV *priv = newAV ();
94 SV *rv = newRV_noinc ((SV *)priv);
95 100
96 av_extend (priv, CD_MAX); 101 av_fill (priv, CD_MAX);
97 av_store (priv, CD_CORO, &PL_sv_undef); 102 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */
98 av_store (priv, CD_TYPE, newSViv (type)); 103 AvARRAY (priv)[CD_TYPE] = newSViv (type);
99 av_store (priv, CD_OK , &PL_sv_no); 104 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
100 av_store (priv, CD_PRIO, newSViv (0));
101 av_store (priv, CD_HITS, newSViv (0)); 105 AvARRAY (priv)[CD_HITS] = newSViv (0);
102 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef); 106 AvARRAY (priv)[CD_GOT ] = newSViv (0);
103 SvREADONLY_on (priv); 107 SvREADONLY_on (priv);
104 108
105 w->callback = coro_std_cb; 109 w->callback = coro_std_cb;
106 w->ext_data = priv; 110 w->ext_data = priv;
107 111
108 hv_store ((HV *)SvRV (self), 112 {
109 EV_CLASS, strlen (EV_CLASS), 113 SV *mob = newRV_noinc ((SV *)priv);
110 rv, 0); 114 sv_magicext (SvRV (self), mob, PERL_MAGIC_coro_event, 0, (char *)w, 0);
111 115 SvREFCNT_dec (mob); /* sv_magicext increments the refcount */
112 GEventAPI->start (w, 0); 116 }
113 } 117 }
118}
114 119
115void 120void
116_next(self) 121_next (SV *self)
117 SV * self
118 CODE: 122 CODE:
123{
119 pe_watcher *w = GEventAPI->sv_2watcher (self); 124 pe_watcher *w = GEventAPI->sv_2watcher (self);
120 AV *priv = (AV *)w->ext_data; 125 AV *priv = (AV *)w->ext_data;
126
127 if (AvARRAY (priv)[CD_OK] == &PL_sv_yes)
128 {
129 AvARRAY (priv)[CD_OK] = &PL_sv_no;
130 XSRETURN_NO; /* got an event */
131 }
132
133 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
121 134
122 if (!w->running) 135 if (!w->running)
123 GEventAPI->start (w, 1); 136 GEventAPI->start (w, 1);
124 137
125 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes) 138 XSRETURN_YES; /* schedule */
126 { 139}
127 AvARRAY(priv)[CD_OK] = &PL_sv_no;
128 XSRETURN_NO;
129 }
130 else
131 {
132 if (AvARRAY(priv)[CD_CORO] != &PL_sv_undef)
133 confess ("only one coroutine can wait for an event");
134 140
135 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT); 141SV *
142_event (SV *self)
143 CODE:
144{
145 if (GIMME_V == G_VOID)
136 XSRETURN_YES; 146 XSRETURN_EMPTY;
137 }
138 147
148 {
149 pe_watcher *w = GEventAPI->sv_2watcher (self);
150 AV *priv = (AV *)w->ext_data;
151
152 RETVAL = newRV_inc ((SV *)priv);
153
154 /* may need to bless it now */
155 if (!SvOBJECT (priv))
156 {
157 SvREADONLY_off ((SV *)priv);
158 sv_bless (RETVAL, coro_event_event_stash);
159 SvREADONLY_on ((SV *)priv);
160 }
161 }
162}
163 OUTPUT:
164 RETVAL
165

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines