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, time_watcher |
68 |
{ |
69 |
tstamp interval; |
70 |
|
71 |
timer () |
72 |
: time_watcher (this, &timer::execute) |
73 |
{ |
74 |
} |
75 |
|
76 |
void execute (time_watcher &w) |
77 |
{ |
78 |
if (interval) |
79 |
start (at + interval); |
80 |
|
81 |
invoke (IOM_CLASS "::timer", newSVtimer (this)); |
82 |
} |
83 |
}; |
84 |
|
85 |
#define newSViow(iow) iom_new_ref ((iow)->self, IOM_CLASS "::iow") |
86 |
#define SvIOW(sv) (iow *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::iow") |
87 |
|
88 |
struct iow : perl_watcher, io_watcher |
89 |
{ |
90 |
iow () |
91 |
: io_watcher (this, &iow::execute) |
92 |
{ |
93 |
} |
94 |
|
95 |
void execute (io_watcher &w, short revents) |
96 |
{ |
97 |
invoke (IOM_CLASS "::iow", newSViow (this), revents); |
98 |
} |
99 |
}; |
100 |
|
101 |
#define newSViw(iw) iom_new_ref ((iw)->self, IOM_CLASS "::iw") |
102 |
#define SvIW(sv) (iw *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::iw") |
103 |
|
104 |
struct iw : perl_watcher, idle_watcher |
105 |
{ |
106 |
iw () |
107 |
: idle_watcher (this, &iw::execute) |
108 |
{ |
109 |
} |
110 |
|
111 |
void execute (idle_watcher &w) |
112 |
{ |
113 |
invoke (IOM_CLASS "::iw", newSViw (this)); |
114 |
} |
115 |
}; |
116 |
|
117 |
#define newSVpw(pw) iom_new_ref ((pw)->self, IOM_CLASS "::pw") |
118 |
#define SvPW(sv) (pw *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::pw") |
119 |
|
120 |
struct pw : perl_watcher, child_watcher |
121 |
{ |
122 |
pw () |
123 |
: child_watcher (this, &pw::execute) |
124 |
{ |
125 |
} |
126 |
|
127 |
void execute (child_watcher &w, int status) |
128 |
{ |
129 |
invoke (IOM_CLASS "::pw", newSVpw (this), status); |
130 |
} |
131 |
}; |
132 |
|