1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
#include <assert.h> |
6 |
#include <string.h> |
7 |
|
8 |
#define EV_PROTOTYPES 0 |
9 |
#include "EVAPI.h" |
10 |
#include "../Coro/CoroAPI.h" |
11 |
|
12 |
static void |
13 |
once_cb (int revents, void *arg) |
14 |
{ |
15 |
AV *av = (AV *)arg; /* @_ */ |
16 |
av_push (av, newSViv (revents)); |
17 |
CORO_READY (AvARRAY (av)[0]); |
18 |
SvREFCNT_dec (av); |
19 |
} |
20 |
|
21 |
#define ONCE_INIT AV *av = GvAV (PL_defgv); |
22 |
#define ONCE_DONE av_clear (av); av_push (av, SvREFCNT_inc (CORO_CURRENT)); |
23 |
|
24 |
static struct ev_prepare scheduler; |
25 |
static struct ev_idle idler; |
26 |
|
27 |
static void |
28 |
idle_cb (struct ev_idle *w, int revents) |
29 |
{ |
30 |
ev_idle_stop (w); |
31 |
} |
32 |
|
33 |
static void |
34 |
prepare_cb (struct ev_watcher *w, int revents) |
35 |
{ |
36 |
while (CORO_NREADY && CORO_CEDE) |
37 |
; |
38 |
|
39 |
/* if still ready, then we have lower-priority coroutines. |
40 |
* cede once, then poll nonblocking |
41 |
*/ |
42 |
if (CORO_NREADY) |
43 |
{ |
44 |
CORO_CEDE_NOTSELF; |
45 |
|
46 |
if (CORO_NREADY) |
47 |
ev_idle_start (&idler); |
48 |
} |
49 |
} |
50 |
|
51 |
MODULE = Coro::EV PACKAGE = Coro::EV |
52 |
|
53 |
PROTOTYPES: ENABLE |
54 |
|
55 |
BOOT: |
56 |
{ |
57 |
I_EV_API ("Coro::EV"); |
58 |
I_CORO_API ("Coro::Event"); |
59 |
|
60 |
ev_prepare_init (&scheduler, prepare_cb); |
61 |
ev_prepare_start (&scheduler); |
62 |
|
63 |
ev_idle_init (&idler, idle_cb); |
64 |
} |
65 |
|
66 |
void |
67 |
_timed_io_once (...) |
68 |
CODE: |
69 |
{ |
70 |
ONCE_INIT; |
71 |
assert (AvFILLp (av) >= 1); |
72 |
ev_once ( |
73 |
sv_fileno (AvARRAY (av)[0]), |
74 |
SvIV (AvARRAY (av)[1]), |
75 |
AvFILLp (av) >= 2 && SvOK (AvARRAY (av)[2]) ? SvNV (AvARRAY (av)[2]) : -1., |
76 |
once_cb, |
77 |
(void *)SvREFCNT_inc (av) |
78 |
); |
79 |
ONCE_DONE; |
80 |
} |
81 |
|
82 |
void |
83 |
_timer_once (...) |
84 |
CODE: |
85 |
{ |
86 |
ONCE_INIT; |
87 |
NV after = SvNV (AvARRAY (av)[0]); |
88 |
ev_once ( |
89 |
-1, |
90 |
0, |
91 |
after >= 0. ? after : 0., |
92 |
once_cb, |
93 |
(void *)SvREFCNT_inc (av) |
94 |
); |
95 |
ONCE_DONE; |
96 |
} |
97 |
|
98 |
|
99 |
|