1 |
typedef int IOM_CHAINED; |
2 |
|
3 |
static SV * |
4 |
iom_new_ref (HV *hv, const char *klass) |
5 |
{ |
6 |
return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1)); |
7 |
} |
8 |
|
9 |
///////////////////////////////////////////////////////////////////////////// |
10 |
|
11 |
#define SvWATCHER(sv) (perl_watcher *)SvPTR (sv, IOM_CLASS "::watcher") |
12 |
|
13 |
struct perl_watcher |
14 |
{ |
15 |
SV *cbsv; |
16 |
HV *self; |
17 |
|
18 |
perl_watcher () |
19 |
: cbsv (0) |
20 |
{ |
21 |
} |
22 |
|
23 |
~perl_watcher () |
24 |
{ |
25 |
SvREFCNT_dec (cbsv); |
26 |
} |
27 |
|
28 |
void cb (SV *cb) |
29 |
{ |
30 |
SvREFCNT_dec (cbsv); |
31 |
cbsv = newSVsv (cb); |
32 |
} |
33 |
|
34 |
void invoke (const char *type, SV *self, int arg = -1); |
35 |
}; |
36 |
|
37 |
void |
38 |
perl_watcher::invoke (const char *type, SV *self, int arg) |
39 |
{ |
40 |
dSP; |
41 |
|
42 |
ENTER; |
43 |
SAVETMPS; |
44 |
|
45 |
PUSHMARK (SP); |
46 |
|
47 |
XPUSHs (sv_2mortal (self)); |
48 |
|
49 |
if (arg >= 0) |
50 |
XPUSHs (sv_2mortal (newSViv (arg))); |
51 |
|
52 |
PUTBACK; |
53 |
call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD); |
54 |
SPAGAIN; |
55 |
|
56 |
PUTBACK; |
57 |
FREETMPS; |
58 |
LEAVE; |
59 |
|
60 |
if (SvTRUE (ERRSV)) |
61 |
IOM_WARN ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV)); |
62 |
} |
63 |
|
64 |
#define newSVtimer(timer) iom_new_ref ((timer)->self, IOM_CLASS "::timer") |
65 |
#define SvTIMER(sv) (timer *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::timer") |
66 |
|
67 |
struct timer : perl_watcher, ev::timer |
68 |
{ |
69 |
timer () |
70 |
{ |
71 |
set<timer, &timer::execute> (this); |
72 |
} |
73 |
|
74 |
void execute (ev::timer &w, int revents) |
75 |
{ |
76 |
invoke (IOM_CLASS "::timer", newSVtimer (this)); |
77 |
} |
78 |
}; |
79 |
|
80 |
#define newSViow(iow) iom_new_ref ((iow)->self, IOM_CLASS "::iow") |
81 |
#define SvIOW(sv) (iow *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::iow") |
82 |
|
83 |
struct iow : perl_watcher, ev::io |
84 |
{ |
85 |
iow () |
86 |
{ |
87 |
set<iow, &iow::execute> (this); |
88 |
} |
89 |
|
90 |
void execute (ev::io &w, int revents) |
91 |
{ |
92 |
invoke (IOM_CLASS "::iow", newSViow (this), revents); |
93 |
} |
94 |
}; |
95 |
|
96 |
#define newSViw(iw) iom_new_ref ((iw)->self, IOM_CLASS "::iw") |
97 |
#define SvIW(sv) (iw *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::iw") |
98 |
|
99 |
struct iw : perl_watcher, ev::idle |
100 |
{ |
101 |
iw () |
102 |
{ |
103 |
set<iw, &iw::execute> (this); |
104 |
} |
105 |
|
106 |
void execute (ev::idle &w, int revents) |
107 |
{ |
108 |
invoke (IOM_CLASS "::iw", newSViw (this)); |
109 |
} |
110 |
}; |
111 |
|
112 |
#define newSVpw(pw) iom_new_ref ((pw)->self, IOM_CLASS "::pw") |
113 |
#define SvPW(sv) (pw *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::pw") |
114 |
|
115 |
struct pw : perl_watcher, ev::child |
116 |
{ |
117 |
pw () |
118 |
{ |
119 |
set<pw, &pw::execute> (this); |
120 |
} |
121 |
|
122 |
void execute (ev::child &w, int revents) |
123 |
{ |
124 |
invoke (IOM_CLASS "::pw", newSVpw (this), w.rstatus); |
125 |
} |
126 |
}; |
127 |
|