ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.12
Committed: Fri Dec 1 02:17:37 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.11: +18 -16 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.3 #include <string.h>
6    
7 root 1.6 /* this useful idiom is unfortunately missing... */
8     static void
9     confess (const char *msg)
10     {
11     dSP;
12    
13     PUSHMARK(SP);
14 root 1.9 XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0)));
15 root 1.6 PUTBACK;
16     call_pv ("Carp::confess", G_VOID);
17     }
18    
19 root 1.2 #include "EventAPI.h"
20     #include "../Coro/CoroAPI.h"
21 root 1.1
22 root 1.9 #ifndef PE_PERLCB
23     # define PE_PERLCB 0x020 /* not public, but we need it :( */
24     #endif
25    
26 root 1.3 #define CD_CORO 0
27     #define CD_TYPE 1
28 root 1.4 #define CD_OK 2
29 root 1.6 #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 root 1.3
34 root 1.4 #define EV_CLASS "Coro::Event"
35 root 1.3
36     static void
37     coro_std_cb(pe_event *pe)
38     {
39 root 1.6 AV *priv = (AV *)pe->ext_data;
40 root 1.4 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
41 root 1.12 SV **cd_coro;
42 root 1.6
43     sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
44     sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits);
45 root 1.3
46     if (type == 1)
47 root 1.6 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
48 root 1.3
49 root 1.12 GEventAPI->stop (pe->up, 0);
50    
51     AvARRAY(priv)[CD_OK] = &PL_sv_yes;
52    
53     cd_coro = &AvARRAY(priv)[CD_CORO];
54 root 1.6 if (*cd_coro != &PL_sv_undef)
55 root 1.3 {
56 root 1.12 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
57 root 1.6 CORO_READY (*cd_coro);
58     SvREFCNT_dec (*cd_coro);
59     *cd_coro = &PL_sv_undef;
60 root 1.3 }
61     }
62    
63 root 1.10 static double
64     prepare_hook (void *data)
65 root 1.3 {
66     while (CORO_NREADY)
67     CORO_CEDE;
68    
69 root 1.11 return 1e10;
70 root 1.3 }
71    
72 root 1.1 MODULE = Coro::Event PACKAGE = Coro::Event
73    
74 root 1.3 PROTOTYPES: ENABLE
75    
76 root 1.1 BOOT:
77     {
78 root 1.9 I_EVENT_API ("Coro::Event");
79 root 1.1 I_CORO_API ("Coro::Event");
80 root 1.3
81 root 1.10 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
82 root 1.1 }
83 root 1.3
84     void
85     _install_std_cb(self,type)
86     SV * self
87     int type
88     CODE:
89 root 1.12 {
90 root 1.3 pe_watcher *w = GEventAPI->sv_2watcher (self);
91    
92 root 1.9 if (WaFLAGS (w) & PE_PERLCB)
93     croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught");
94     {
95     AV *priv = newAV ();
96     SV *rv = newRV_noinc ((SV *)priv);
97    
98     av_extend (priv, CD_MAX);
99     av_store (priv, CD_CORO, &PL_sv_undef);
100     av_store (priv, CD_TYPE, newSViv (type));
101     av_store (priv, CD_OK , &PL_sv_no);
102     av_store (priv, CD_PRIO, newSViv (0));
103     av_store (priv, CD_HITS, newSViv (0));
104     av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
105     SvREADONLY_on (priv);
106    
107     w->callback = coro_std_cb;
108     w->ext_data = priv;
109    
110     hv_store ((HV *)SvRV (self),
111     EV_CLASS, strlen (EV_CLASS),
112     rv, 0);
113 root 1.3
114 root 1.9 GEventAPI->start (w, 0);
115     }
116 root 1.12 }
117 root 1.8
118 root 1.3 void
119 root 1.4 _next(self)
120 root 1.3 SV * self
121     CODE:
122 root 1.12 {
123 root 1.3 pe_watcher *w = GEventAPI->sv_2watcher (self);
124 root 1.6 AV *priv = (AV *)w->ext_data;
125 root 1.3
126 root 1.6 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes)
127 root 1.4 {
128 root 1.6 AvARRAY(priv)[CD_OK] = &PL_sv_no;
129 root 1.4 XSRETURN_NO;
130     }
131 root 1.6
132 root 1.12 if (!w->running)
133     GEventAPI->start (w, 1);
134    
135     if (AvARRAY(priv)[CD_CORO] == &PL_sv_undef)
136     AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
137    
138     XSRETURN_YES;
139     }
140 root 1.6