ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.15
Committed: Fri Dec 1 21:13:15 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.14: +1 -1 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.13 #include <assert.h>
6 root 1.3 #include <string.h>
7    
8 root 1.2 #include "EventAPI.h"
9     #include "../Coro/CoroAPI.h"
10 root 1.1
11 root 1.3 #define CD_CORO 0
12     #define CD_TYPE 1
13 root 1.4 #define CD_OK 2
14 root 1.14
15 root 1.6 #define CD_HITS 4 /* hardcoded in Coro::Event */
16     #define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
17     #define CD_MAX 5
18 root 1.3
19     static void
20 root 1.13 coro_std_cb (pe_event *pe)
21 root 1.3 {
22 root 1.6 AV *priv = (AV *)pe->ext_data;
23 root 1.13 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
24 root 1.12 SV **cd_coro;
25 root 1.6
26 root 1.13 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
27 root 1.3
28     if (type == 1)
29 root 1.13 SvIV_set (AvARRAY (priv)[CD_GOT], ((pe_ioevent *)pe)->got);
30 root 1.3
31 root 1.12 GEventAPI->stop (pe->up, 0);
32    
33 root 1.13 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
34 root 1.12
35     cd_coro = &AvARRAY(priv)[CD_CORO];
36 root 1.6 if (*cd_coro != &PL_sv_undef)
37 root 1.3 {
38 root 1.13 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
39 root 1.6 CORO_READY (*cd_coro);
40     SvREFCNT_dec (*cd_coro);
41     *cd_coro = &PL_sv_undef;
42 root 1.3 }
43     }
44    
45 root 1.13 static void
46     asynccheck_hook (void *data)
47 root 1.3 {
48 root 1.13 /* ceding from C means allocating a stack, but we assume this is a rare case */
49 root 1.3 while (CORO_NREADY)
50     CORO_CEDE;
51     }
52    
53 root 1.1 MODULE = Coro::Event PACKAGE = Coro::Event
54    
55 root 1.3 PROTOTYPES: ENABLE
56    
57 root 1.1 BOOT:
58     {
59 root 1.9 I_EVENT_API ("Coro::Event");
60 root 1.1 I_CORO_API ("Coro::Event");
61 root 1.3
62 root 1.13 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
63 root 1.1 }
64 root 1.3
65     void
66 root 1.13 _install_std_cb (SV *self, int type)
67 root 1.3 CODE:
68 root 1.12 {
69 root 1.3 pe_watcher *w = GEventAPI->sv_2watcher (self);
70    
71 root 1.13 if (w->callback)
72     croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
73    
74 root 1.9 {
75     AV *priv = newAV ();
76     SV *rv = newRV_noinc ((SV *)priv);
77    
78 root 1.15 av_fill (priv, CD_MAX);
79 root 1.13 AvARRAY (priv)[CD_CORO] = &PL_sv_undef;
80     AvARRAY (priv)[CD_TYPE] = newSViv (type);
81     AvARRAY (priv)[CD_OK ] = &PL_sv_no;
82     AvARRAY (priv)[CD_HITS] = newSViv (0);
83     AvARRAY (priv)[CD_GOT ] = newSViv (0);
84 root 1.9 SvREADONLY_on (priv);
85    
86     w->callback = coro_std_cb;
87     w->ext_data = priv;
88    
89 root 1.13 /* make sure Event does not use PERL_MAGIC_uvar, which */
90     /* we abuse for non-uvar purposes. */
91     sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0);
92 root 1.9 }
93 root 1.12 }
94 root 1.8
95 root 1.3 void
96 root 1.13 _next (SV *self)
97 root 1.3 CODE:
98 root 1.12 {
99 root 1.3 pe_watcher *w = GEventAPI->sv_2watcher (self);
100 root 1.6 AV *priv = (AV *)w->ext_data;
101 root 1.3
102 root 1.13 if (AvARRAY (priv)[CD_OK] == &PL_sv_yes)
103 root 1.4 {
104 root 1.13 AvARRAY (priv)[CD_OK] = &PL_sv_no;
105     XSRETURN_NO; /* got an event */
106 root 1.4 }
107 root 1.6
108 root 1.12 if (!w->running)
109 root 1.14 {
110     SvIV_set (AvARRAY (priv)[CD_GOT], 0);
111     SvIV_set (AvARRAY (priv)[CD_HITS], 0);
112    
113     GEventAPI->start (w, 1);
114     }
115 root 1.12
116 root 1.13 if (AvARRAY (priv)[CD_CORO] == &PL_sv_undef)
117     AvARRAY (priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
118     else if (AvARRAY (priv)[CD_CORO] != CORO_CURRENT)
119     croak ("Coro::Event::next can only be called from a single coroutine at a time, caught");
120 root 1.12
121 root 1.13 XSRETURN_YES; /* schedule */
122 root 1.12 }
123 root 1.6
124 root 1.14 SV *
125     _event (SV *self)
126     CODE:
127     {
128     if (GIMME_V == G_VOID)
129     XSRETURN_EMPTY;
130    
131     {
132     pe_watcher *w = GEventAPI->sv_2watcher (self);
133     AV *priv = (AV *)w->ext_data;
134    
135     RETVAL = newRV_inc ((SV *)priv);
136     }
137     }
138     OUTPUT:
139     RETVAL
140