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.6 |
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 |
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.6 |
if (*cd_coro != &PL_sv_undef) |
50 |
root |
1.3 |
{ |
51 |
root |
1.6 |
CORO_READY (*cd_coro); |
52 |
|
|
SvREFCNT_dec (*cd_coro); |
53 |
|
|
*cd_coro = &PL_sv_undef; |
54 |
root |
1.3 |
} |
55 |
root |
1.4 |
else |
56 |
|
|
{ |
57 |
root |
1.6 |
AvARRAY(priv)[CD_OK] = &PL_sv_yes; |
58 |
root |
1.4 |
GEventAPI->stop (pe->up, 0); |
59 |
|
|
} |
60 |
root |
1.3 |
} |
61 |
|
|
|
62 |
root |
1.10 |
static double |
63 |
|
|
prepare_hook (void *data) |
64 |
root |
1.3 |
{ |
65 |
|
|
while (CORO_NREADY) |
66 |
|
|
CORO_CEDE; |
67 |
|
|
|
68 |
root |
1.10 |
return 1e20; |
69 |
root |
1.3 |
} |
70 |
|
|
|
71 |
root |
1.1 |
MODULE = Coro::Event PACKAGE = Coro::Event |
72 |
|
|
|
73 |
root |
1.3 |
PROTOTYPES: ENABLE |
74 |
|
|
|
75 |
root |
1.1 |
BOOT: |
76 |
|
|
{ |
77 |
root |
1.9 |
I_EVENT_API ("Coro::Event"); |
78 |
root |
1.1 |
I_CORO_API ("Coro::Event"); |
79 |
root |
1.3 |
|
80 |
root |
1.10 |
GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0); |
81 |
root |
1.1 |
} |
82 |
root |
1.3 |
|
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 |
root |
1.9 |
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 |
root |
1.3 |
|
112 |
root |
1.9 |
GEventAPI->start (w, 0); |
113 |
|
|
} |
114 |
root |
1.8 |
|
115 |
root |
1.3 |
void |
116 |
root |
1.4 |
_next(self) |
117 |
root |
1.3 |
SV * self |
118 |
|
|
CODE: |
119 |
|
|
pe_watcher *w = GEventAPI->sv_2watcher (self); |
120 |
root |
1.6 |
AV *priv = (AV *)w->ext_data; |
121 |
root |
1.3 |
|
122 |
root |
1.4 |
if (!w->running) |
123 |
|
|
GEventAPI->start (w, 1); |
124 |
root |
1.3 |
|
125 |
root |
1.6 |
if (AvARRAY(priv)[CD_OK] == &PL_sv_yes) |
126 |
root |
1.4 |
{ |
127 |
root |
1.6 |
AvARRAY(priv)[CD_OK] = &PL_sv_no; |
128 |
root |
1.4 |
XSRETURN_NO; |
129 |
|
|
} |
130 |
|
|
else |
131 |
|
|
{ |
132 |
root |
1.6 |
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 |
root |
1.4 |
XSRETURN_YES; |
137 |
|
|
} |
138 |
root |
1.6 |
|