… | |
… | |
19 | pe_io *w; |
19 | pe_io *w; |
20 | GPollFD *pfd; |
20 | GPollFD *pfd; |
21 | }; |
21 | }; |
22 | |
22 | |
23 | static void |
23 | static void |
24 | event_timer_cb (pe_event *pe) |
|
|
25 | { |
|
|
26 | *(int *)(pe->up->ext_data) = -1; |
|
|
27 | } |
|
|
28 | |
|
|
29 | static void |
|
|
30 | event_io_cb (pe_event *pe) |
24 | event_io_cb (pe_event *pe) |
31 | { |
25 | { |
32 | U16 got = ((pe_ioevent *)pe)->got; |
26 | U16 got = ((pe_ioevent *)pe)->got; |
33 | struct info *i = (struct info *)(pe->up->ext_data); |
27 | struct info *i = (struct info *)(pe->up->ext_data); |
34 | |
28 | |
… | |
… | |
37 | | (got & PE_W ? G_IO_OUT : 0) |
31 | | (got & PE_W ? G_IO_OUT : 0) |
38 | | (got & PE_E ? G_IO_PRI : 0) |
32 | | (got & PE_E ? G_IO_PRI : 0) |
39 | ); |
33 | ); |
40 | |
34 | |
41 | if (i->pfd->revents) |
35 | if (i->pfd->revents) |
42 | *(i->got_events)++; |
36 | (*(i->got_events))++; |
43 | } |
37 | } |
44 | |
38 | |
45 | static gint |
39 | static gint |
46 | event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
40 | event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
47 | { |
41 | { |
48 | dSP; |
42 | dSP; |
49 | // yes, I use C99. If your compiler barfs here, fix it, but don't |
43 | // yes, I use C99. If your compiler barfs here, fix it, but don't |
50 | // tell me your compiler vendor was too incompetent to implement |
44 | // tell me your compiler vendor was too incompetent to implement |
51 | // the C standard within the last six years. |
45 | // the C standard within the last six years. |
52 | struct info info[nfds]; |
46 | struct info info[nfds]; |
53 | pe_timer *w_timeout = 0; |
47 | int got_events = 0; |
54 | int got_events = 0, got_timeout = 0; |
|
|
55 | int n; |
48 | int n; |
56 | |
49 | |
57 | for (n = 0; n < nfds; n++) |
50 | for (n = 0; n < nfds; n++) |
58 | { |
51 | { |
59 | GPollFD *pfd = fds + n; |
52 | GPollFD *pfd = fds + n; |
60 | struct info *i = info + n; |
53 | struct info *i = info + n; |
61 | |
54 | |
|
|
55 | i->pfd = pfd; |
62 | i->got_events = &got_events; |
56 | i->got_events = &got_events; |
63 | |
57 | |
64 | pe_io *w = i->w = GEventAPI->new_io (0, 0); |
58 | pe_io *w = i->w = GEventAPI->new_io (0, 0); |
65 | w->base.callback = (void *)event_io_cb; |
59 | w->base.callback = (void *)event_io_cb; |
66 | w->base.ext_data = (void *)i; |
60 | w->base.ext_data = (void *)i; |
… | |
… | |
71 | |
65 | |
72 | pfd->revents = 0; |
66 | pfd->revents = 0; |
73 | GEventAPI->start ((pe_watcher *)w, 0); |
67 | GEventAPI->start ((pe_watcher *)w, 0); |
74 | } |
68 | } |
75 | |
69 | |
76 | if (timeout >= 0) |
|
|
77 | { |
|
|
78 | w_timeout = GEventAPI->new_timer (0, 0); |
|
|
79 | w_timeout->base.callback = (void *)event_timer_cb; |
|
|
80 | w_timeout->base.ext_data = (void *)&got_timeout; |
|
|
81 | w_timeout->tm.at = GEventAPI->NVtime () + timeout * 0.001; |
|
|
82 | } |
|
|
83 | |
|
|
84 | do { |
70 | do { |
85 | PUSHMARK (SP); |
71 | PUSHMARK (SP); |
|
|
72 | XPUSHs (sv_2mortal (newSVnv (timeout >= 0 ? timeout * 0.001 : 86400. * 365.))); |
|
|
73 | PUTBACK; |
86 | call_pv ("Event::one_event", G_DISCARD | G_EVAL); |
74 | call_pv ("Event::one_event", G_DISCARD | G_EVAL); |
87 | } while (!got_events && !got_timeout); |
75 | SPAGAIN; |
88 | |
76 | } while (timeout < 0 && !got_events); |
89 | if (w_timeout) |
|
|
90 | GEventAPI->cancel ((pe_watcher *)w_timeout); |
|
|
91 | |
77 | |
92 | for (n = 0; n < nfds; n++) |
78 | for (n = 0; n < nfds; n++) |
93 | GEventAPI->cancel ((pe_watcher *)info[n].w); |
79 | GEventAPI->cancel ((pe_watcher *)info[n].w); |
94 | |
80 | |
95 | if (SvOK (ERRSV)) |
81 | if (SvTRUE (ERRSV)) |
96 | croak (0); |
82 | croak (0); |
97 | |
|
|
98 | if (got_timeout) |
|
|
99 | return 0; |
|
|
100 | |
83 | |
101 | return got_events; |
84 | return got_events; |
102 | } |
85 | } |
103 | |
86 | |
104 | MODULE = Glib::Event PACKAGE = Glib::Event |
87 | MODULE = Glib::Event PACKAGE = Glib::Event |