ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.8
Committed: Sun Apr 14 01:18:38 2002 UTC (22 years, 2 months ago) by root
Branch: MAIN
Changes since 1.7: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <string.h>
6
7 /* this useful idiom is unfortunately missing... */
8 static void
9 confess (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
19 #include "EventAPI.h"
20 #include "../Coro/CoroAPI.h"
21
22 #define CD_CORO 0
23 #define CD_TYPE 1
24 #define CD_OK 2
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 */
28 #define CD_MAX 5
29
30 #define EV_CLASS "Coro::Event"
31
32 static pe_idle *scheduler;
33 static int do_schedule;
34
35 #define NEED_SCHEDULE if (!do_schedule) \
36 { \
37 do_schedule = 1; \
38 GEventAPI->now ((pe_watcher *)scheduler); \
39 }
40
41 static void
42 coro_std_cb(pe_event *pe)
43 {
44 AV *priv = (AV *)pe->ext_data;
45 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
46 SV **cd_coro = &AvARRAY(priv)[CD_CORO];
47
48 sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
49 sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits);
50
51 if (type == 1)
52 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
53
54 if (*cd_coro != &PL_sv_undef)
55 {
56 CORO_READY (*cd_coro);
57 SvREFCNT_dec (*cd_coro);
58 *cd_coro = &PL_sv_undef;
59 NEED_SCHEDULE;
60 }
61 else
62 {
63 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
64 GEventAPI->stop (pe->up, 0);
65 }
66 }
67
68 static void
69 scheduler_cb(pe_event *pe)
70 {
71 while (CORO_NREADY)
72 CORO_CEDE;
73
74 do_schedule = 0;
75 }
76
77 MODULE = Coro::Event PACKAGE = Coro::Event
78
79 PROTOTYPES: ENABLE
80
81 BOOT:
82 {
83 I_EVENT_API("Coro::Event");
84 I_CORO_API ("Coro::Event");
85
86 /* create a fake idle handler (we only ever call now) */
87 scheduler = GEventAPI->new_idle (0, 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 }
94
95 void
96 _install_std_cb(self,type)
97 SV * self
98 int type
99 CODE:
100 pe_watcher *w = GEventAPI->sv_2watcher (self);
101 AV *priv = newAV ();
102 SV *rv = newRV_noinc ((SV *)priv);
103
104 av_extend (priv, CD_MAX);
105 av_store (priv, CD_CORO, &PL_sv_undef);
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
113 w->callback = coro_std_cb;
114 w->ext_data = priv;
115
116 hv_store ((HV *)SvRV (self),
117 EV_CLASS, strlen (EV_CLASS),
118 rv, 0);
119
120 GEventAPI->start (w, 0);
121
122 void
123 _next(self)
124 SV * self
125 CODE:
126 pe_watcher *w = GEventAPI->sv_2watcher (self);
127 AV *priv = (AV *)w->ext_data;
128
129 if (!w->running)
130 GEventAPI->start (w, 1);
131
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)
140 confess ("only one coroutine can wait for an event");
141
142 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
143 XSRETURN_YES;
144 }
145
146 MODULE = Coro::Event PACKAGE = Coro
147
148 # overwrite the ready function
149 void
150 ready(self)
151 SV * self
152 PROTOTYPE: $
153 CODE:
154 NEED_SCHEDULE;
155 CORO_READY (self);
156
157