ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.11
Committed: Fri Nov 24 00:10:40 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
CVS Tags: stack_sharing
Changes since 1.10: +1 -1 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 at any given time",0)));
15 PUTBACK;
16 call_pv ("Carp::confess", G_VOID);
17 }
18
19 #include "EventAPI.h"
20 #include "../Coro/CoroAPI.h"
21
22 #ifndef PE_PERLCB
23 # define PE_PERLCB 0x020 /* not public, but we need it :( */
24 #endif
25
26 #define CD_CORO 0
27 #define CD_TYPE 1
28 #define CD_OK 2
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 */
32 #define CD_MAX 5
33
34 #define EV_CLASS "Coro::Event"
35
36 static void
37 coro_std_cb(pe_event *pe)
38 {
39 AV *priv = (AV *)pe->ext_data;
40 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
41 SV **cd_coro = &AvARRAY(priv)[CD_CORO];
42
43 sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
44 sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits);
45
46 if (type == 1)
47 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
48
49 if (*cd_coro != &PL_sv_undef)
50 {
51 CORO_READY (*cd_coro);
52 SvREFCNT_dec (*cd_coro);
53 *cd_coro = &PL_sv_undef;
54 }
55 else
56 {
57 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
58 GEventAPI->stop (pe->up, 0);
59 }
60 }
61
62 static double
63 prepare_hook (void *data)
64 {
65 while (CORO_NREADY)
66 CORO_CEDE;
67
68 return 1e10;
69 }
70
71 MODULE = Coro::Event PACKAGE = Coro::Event
72
73 PROTOTYPES: ENABLE
74
75 BOOT:
76 {
77 I_EVENT_API ("Coro::Event");
78 I_CORO_API ("Coro::Event");
79
80 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
81 }
82
83 void
84 _install_std_cb(self,type)
85 SV * self
86 int type
87 CODE:
88 pe_watcher *w = GEventAPI->sv_2watcher (self);
89
90 if (WaFLAGS (w) & PE_PERLCB)
91 croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught");
92 {
93 AV *priv = newAV ();
94 SV *rv = newRV_noinc ((SV *)priv);
95
96 av_extend (priv, CD_MAX);
97 av_store (priv, CD_CORO, &PL_sv_undef);
98 av_store (priv, CD_TYPE, newSViv (type));
99 av_store (priv, CD_OK , &PL_sv_no);
100 av_store (priv, CD_PRIO, newSViv (0));
101 av_store (priv, CD_HITS, newSViv (0));
102 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
103 SvREADONLY_on (priv);
104
105 w->callback = coro_std_cb;
106 w->ext_data = priv;
107
108 hv_store ((HV *)SvRV (self),
109 EV_CLASS, strlen (EV_CLASS),
110 rv, 0);
111
112 GEventAPI->start (w, 0);
113 }
114
115 void
116 _next(self)
117 SV * self
118 CODE:
119 pe_watcher *w = GEventAPI->sv_2watcher (self);
120 AV *priv = (AV *)w->ext_data;
121
122 if (!w->running)
123 GEventAPI->start (w, 1);
124
125 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes)
126 {
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
135 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
136 XSRETURN_YES;
137 }
138