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.9 by root, Sat Mar 22 18:50:32 2003 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h> 5#include <string.h>
6 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#include "EventAPI.h" 19#include "EventAPI.h"
8#include "../Coro/CoroAPI.h" 20#include "../Coro/CoroAPI.h"
9 21
22#ifndef PE_PERLCB
23# define PE_PERLCB 0x020 /* not public, but we need it :( */
24#endif
25
10#define CD_CORO 0 26#define CD_CORO 0
11#define CD_TYPE 1 27#define CD_TYPE 1
12#define CD_W 2 28#define CD_OK 2
13#define CD_GOT 3 29#define CD_PRIO 3 /* hardcoded in Coro::Event */
30#define CD_HITS 4 /* hardcoded in Coro::Event */
31#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
14#define CD_MAX 3 32#define CD_MAX 5
15/* no support for hits and prio so far. */
16 33
17#define EV_CLASS "Coro::Event::Ev" 34#define EV_CLASS "Coro::Event"
18 35
19static HV *ev_stash;
20static pe_idle *scheduler; 36static pe_idle *scheduler;
21static int do_schedule; 37static int do_schedule;
22 38
23#define NEED_SCHEDULE if (!do_schedule) \ 39#define NEED_SCHEDULE if (!do_schedule) \
24 { \ 40 { \
25 do_schedule = 1; \ 41 do_schedule = 1; \
26 GEventAPI->now ((pe_watcher *)scheduler); \ 42 GEventAPI->now ((pe_watcher *)scheduler); \
27 } 43 }
44
28static void 45static void
29coro_std_cb(pe_event *pe) 46coro_std_cb(pe_event *pe)
30{ 47{
31 AV *av = (AV *)SvRV ((SV *)pe->ext_data); 48 AV *priv = (AV *)pe->ext_data;
32 IV type = SvIV (*av_fetch (av, CD_TYPE, 1)); 49 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
33 SV *cd_coro = *av_fetch (av, CD_CORO, 1); 50 SV **cd_coro = &AvARRAY(priv)[CD_CORO];
34 51
35 av_store (av, CD_W, SvREFCNT_inc (pe->up->mysv)); 52 sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
53 sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits);
36 54
37 if (type == 1) 55 if (type == 1)
38 av_store (av, CD_GOT, newSViv (((pe_ioevent *)pe)->got)); 56 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
39 57
40 GEventAPI->stop (pe->up, 0); 58 if (*cd_coro != &PL_sv_undef)
41
42 if (SvROK (cd_coro))
43 { 59 {
44 CORO_READY (cd_coro); 60 CORO_READY (*cd_coro);
61 SvREFCNT_dec (*cd_coro);
62 *cd_coro = &PL_sv_undef;
45 NEED_SCHEDULE; 63 NEED_SCHEDULE;
64 }
65 else
66 {
67 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
68 GEventAPI->stop (pe->up, 0);
46 } 69 }
47} 70}
48 71
49static void 72static void
50scheduler_cb(pe_event *pe) 73scheduler_cb(pe_event *pe)
59 82
60PROTOTYPES: ENABLE 83PROTOTYPES: ENABLE
61 84
62BOOT: 85BOOT:
63{ 86{
64 I_EVENT_API("Coro::Event"); 87 I_EVENT_API ("Coro::Event");
65 I_CORO_API ("Coro::Event"); 88 I_CORO_API ("Coro::Event");
66
67 ev_stash = gv_stashpv (EV_CLASS, TRUE);
68 89
69 /* create a fake idle handler (we only ever call now) */ 90 /* create a fake idle handler (we only ever call now) */
70 scheduler = GEventAPI->new_idle (0, 0); 91 scheduler = GEventAPI->new_idle (0, 0);
71 scheduler->base.callback = scheduler_cb; 92 scheduler->base.callback = scheduler_cb;
93 scheduler->base.prio = PE_PRIO_NORMAL; /* StarvePrio */
72 scheduler->min_interval = newSVnv (0); 94 scheduler->min_interval = newSVnv (0);
73 scheduler->max_interval = newSVnv (0); 95 scheduler->max_interval = newSVnv (0);
74 GEventAPI->stop ((pe_watcher *)scheduler, 0); 96 GEventAPI->stop ((pe_watcher *)scheduler, 0);
75} 97}
76 98
78_install_std_cb(self,type) 100_install_std_cb(self,type)
79 SV * self 101 SV * self
80 int type 102 int type
81 CODE: 103 CODE:
82 pe_watcher *w = GEventAPI->sv_2watcher (self); 104 pe_watcher *w = GEventAPI->sv_2watcher (self);
83 AV *priv = newAV ();
84 SV *rv;
85 105
86 av_extend (priv, CD_MAX); 106 if (WaFLAGS (w) & PE_PERLCB)
87 av_store (priv, CD_TYPE, newSViv (type)); 107 croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught");
108 {
109 AV *priv = newAV ();
110 SV *rv = newRV_noinc ((SV *)priv);
88 111
89 rv = sv_bless (newRV_noinc ((SV *)priv), ev_stash); 112 av_extend (priv, CD_MAX);
113 av_store (priv, CD_CORO, &PL_sv_undef);
114 av_store (priv, CD_TYPE, newSViv (type));
115 av_store (priv, CD_OK , &PL_sv_no);
116 av_store (priv, CD_PRIO, newSViv (0));
117 av_store (priv, CD_HITS, newSViv (0));
118 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
119 SvREADONLY_on (priv);
90 120
91 hv_store ((HV *)SvRV (self), 121 w->callback = coro_std_cb;
92 EV_CLASS, strlen (EV_CLASS), 122 w->ext_data = priv;
93 rv, 0);
94 123
95 w->ext_data = rv; 124 hv_store ((HV *)SvRV (self),
96 w->callback = coro_std_cb; 125 EV_CLASS, strlen (EV_CLASS),
126 rv, 0);
127
128 GEventAPI->start (w, 0);
129 }
97 130
98void 131void
99_next0(self) 132_next(self)
100 SV * self 133 SV * self
101 CODE: 134 CODE:
102 pe_watcher *w = GEventAPI->sv_2watcher (self); 135 pe_watcher *w = GEventAPI->sv_2watcher (self);
103 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 136 AV *priv = (AV *)w->ext_data;
104 137
138 if (!w->running)
105 GEventAPI->start (w, 1); 139 GEventAPI->start (w, 1);
106 140
107 if (SvROK (*av_fetch (priv, CD_CORO, 1))) 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)
108 croak ("only one coroutine can wait for an event"); 149 confess ("only one coroutine can wait for an event");
109 150
110 av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT)); 151 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
152 XSRETURN_YES;
153 }
111 154
112SV * 155MODULE = Coro::Event PACKAGE = Coro
113_next1(self) 156
157# overwrite the ready function
158void
159ready(self)
114 SV * self 160 SV * self
115 CODE: 161 PROTOTYPE: $
116 pe_watcher *w = GEventAPI->sv_2watcher (self); 162 CODE:
117 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 163 NEED_SCHEDULE;
164 CORO_READY (self);
118 165
119 av_store (priv, CD_CORO, &PL_sv_undef);
120 166
121 RETVAL = SvREFCNT_inc ((SV *)w->ext_data);
122 OUTPUT:
123 RETVAL
124

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines