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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines