1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
#include <assert.h> |
6 |
#include <string.h> |
7 |
|
8 |
#include "EventAPI.h" |
9 |
#include "../Coro/CoroAPI.h" |
10 |
|
11 |
#define CD_WAIT 0 /* wait queue */ |
12 |
#define CD_TYPE 1 |
13 |
#define CD_OK 2 |
14 |
|
15 |
#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 |
|
19 |
static void |
20 |
coro_std_cb (pe_event *pe) |
21 |
{ |
22 |
AV *priv = (AV *)pe->ext_data; |
23 |
IV type = SvIV (AvARRAY (priv)[CD_TYPE]); |
24 |
AV *cd_wait; |
25 |
SV *coro; |
26 |
|
27 |
SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits); |
28 |
SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0); |
29 |
|
30 |
AvARRAY (priv)[CD_OK] = &PL_sv_yes; |
31 |
|
32 |
cd_wait = (AV *)AvARRAY(priv)[CD_WAIT]; |
33 |
if ((coro = av_shift (cd_wait))) |
34 |
{ |
35 |
if (av_len (cd_wait) < 0) |
36 |
GEventAPI->stop (pe->up, 0); |
37 |
|
38 |
CORO_READY (coro); |
39 |
SvREFCNT_dec (coro); |
40 |
} |
41 |
} |
42 |
|
43 |
static void |
44 |
asynccheck_hook (void *data) |
45 |
{ |
46 |
/* this loops as long as we have _other_ coros with the same or higher priority */ |
47 |
while (CORO_NREADY && CORO_CEDE) |
48 |
; |
49 |
} |
50 |
|
51 |
static double |
52 |
prepare_hook (void *data) |
53 |
{ |
54 |
/* this yields once to another coro with any priority */ |
55 |
if (CORO_NREADY) |
56 |
{ |
57 |
CORO_CEDE_NOTSELF; |
58 |
/* |
59 |
* timers might have changed, and Event fails to notice this |
60 |
* so we have to assume the worst. If Event didn't have that bug, |
61 |
* we would only need to do this if CORO_NREADY is != 0 now. |
62 |
*/ |
63 |
return 0.; |
64 |
} |
65 |
else |
66 |
return 85197.73; /* this is as good as any value, but it factors badly with common values */ |
67 |
} |
68 |
|
69 |
MODULE = Coro::Event PACKAGE = Coro::Event |
70 |
|
71 |
PROTOTYPES: ENABLE |
72 |
|
73 |
BOOT: |
74 |
{ |
75 |
I_EVENT_API ("Coro::Event"); |
76 |
I_CORO_API ("Coro::Event"); |
77 |
|
78 |
GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0); |
79 |
GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0); |
80 |
} |
81 |
|
82 |
void |
83 |
_install_std_cb (SV *self, int type) |
84 |
CODE: |
85 |
{ |
86 |
pe_watcher *w = GEventAPI->sv_2watcher (self); |
87 |
|
88 |
if (w->callback) |
89 |
croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught"); |
90 |
|
91 |
{ |
92 |
AV *priv = newAV (); |
93 |
SV *rv = newRV_noinc ((SV *)priv); |
94 |
|
95 |
av_fill (priv, CD_MAX); |
96 |
AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* badbad */ |
97 |
AvARRAY (priv)[CD_TYPE] = newSViv (type); |
98 |
AvARRAY (priv)[CD_OK ] = &PL_sv_no; |
99 |
AvARRAY (priv)[CD_HITS] = newSViv (0); |
100 |
AvARRAY (priv)[CD_GOT ] = newSViv (0); |
101 |
SvREADONLY_on (priv); |
102 |
|
103 |
w->callback = coro_std_cb; |
104 |
w->ext_data = priv; |
105 |
|
106 |
/* make sure Event does not use PERL_MAGIC_uvar, which */ |
107 |
/* we abuse for non-uvar purposes. */ |
108 |
sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0); |
109 |
} |
110 |
} |
111 |
|
112 |
void |
113 |
_next (SV *self) |
114 |
CODE: |
115 |
{ |
116 |
pe_watcher *w = GEventAPI->sv_2watcher (self); |
117 |
AV *priv = (AV *)w->ext_data; |
118 |
|
119 |
if (AvARRAY (priv)[CD_OK] == &PL_sv_yes) |
120 |
{ |
121 |
AvARRAY (priv)[CD_OK] = &PL_sv_no; |
122 |
XSRETURN_NO; /* got an event */ |
123 |
} |
124 |
|
125 |
av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT)); |
126 |
|
127 |
if (!w->running) |
128 |
GEventAPI->start (w, 1); |
129 |
|
130 |
XSRETURN_YES; /* schedule */ |
131 |
} |
132 |
|
133 |
SV * |
134 |
_event (SV *self) |
135 |
CODE: |
136 |
{ |
137 |
if (GIMME_V == G_VOID) |
138 |
XSRETURN_EMPTY; |
139 |
|
140 |
{ |
141 |
pe_watcher *w = GEventAPI->sv_2watcher (self); |
142 |
AV *priv = (AV *)w->ext_data; |
143 |
|
144 |
RETVAL = newRV_inc ((SV *)priv); |
145 |
} |
146 |
} |
147 |
OUTPUT: |
148 |
RETVAL |
149 |
|