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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines