… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
|
|
31 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
33 | |
38 | |
34 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
|
|
40 | # ifndef EV_USE_MONOTONIC |
35 | # define EV_USE_MONOTONIC 1 |
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
36 | # define EV_USE_REALTIME 1 |
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
37 | # endif |
46 | # endif |
38 | |
47 | |
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
40 | # define EV_USE_SELECT 1 |
49 | # define EV_USE_SELECT 1 |
41 | # endif |
50 | # endif |
42 | |
51 | |
43 | # if HAVE_POLL && HAVE_POLL_H |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
44 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
45 | # endif |
54 | # endif |
46 | |
55 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
48 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
49 | # endif |
58 | # endif |
50 | |
59 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
52 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
53 | # endif |
62 | # endif |
54 | |
63 | |
55 | #endif |
64 | #endif |
56 | |
65 | |
… | |
… | |
64 | #include <assert.h> |
73 | #include <assert.h> |
65 | #include <errno.h> |
74 | #include <errno.h> |
66 | #include <sys/types.h> |
75 | #include <sys/types.h> |
67 | #include <time.h> |
76 | #include <time.h> |
68 | |
77 | |
69 | #ifndef PERL |
|
|
70 | # include <signal.h> |
78 | #include <signal.h> |
71 | #endif |
|
|
72 | |
79 | |
73 | #ifndef WIN32 |
80 | #ifndef _WIN32 |
74 | # include <unistd.h> |
81 | # include <unistd.h> |
75 | # include <sys/time.h> |
82 | # include <sys/time.h> |
76 | # include <sys/wait.h> |
83 | # include <sys/wait.h> |
|
|
84 | #else |
|
|
85 | # define WIN32_LEAN_AND_MEAN |
|
|
86 | # include <windows.h> |
|
|
87 | # ifndef EV_SELECT_IS_WINSOCKET |
|
|
88 | # define EV_SELECT_IS_WINSOCKET 1 |
77 | #endif |
89 | # endif |
|
|
90 | #endif |
|
|
91 | |
78 | /**/ |
92 | /**/ |
79 | |
93 | |
80 | #ifndef EV_USE_MONOTONIC |
94 | #ifndef EV_USE_MONOTONIC |
81 | # define EV_USE_MONOTONIC 1 |
95 | # define EV_USE_MONOTONIC 1 |
82 | #endif |
96 | #endif |
83 | |
97 | |
84 | #ifndef EV_USE_SELECT |
98 | #ifndef EV_USE_SELECT |
85 | # define EV_USE_SELECT 1 |
99 | # define EV_USE_SELECT 1 |
|
|
100 | # define EV_SELECT_USE_FD_SET 1 |
86 | #endif |
101 | #endif |
87 | |
102 | |
88 | #ifndef EV_USE_POLL |
103 | #ifndef EV_USE_POLL |
89 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
104 | # ifdef _WIN32 |
|
|
105 | # define EV_USE_POLL 0 |
|
|
106 | # else |
|
|
107 | # define EV_USE_POLL 1 |
|
|
108 | # endif |
90 | #endif |
109 | #endif |
91 | |
110 | |
92 | #ifndef EV_USE_EPOLL |
111 | #ifndef EV_USE_EPOLL |
93 | # define EV_USE_EPOLL 0 |
112 | # define EV_USE_EPOLL 0 |
94 | #endif |
113 | #endif |
95 | |
114 | |
96 | #ifndef EV_USE_KQUEUE |
115 | #ifndef EV_USE_KQUEUE |
97 | # define EV_USE_KQUEUE 0 |
116 | # define EV_USE_KQUEUE 0 |
98 | #endif |
117 | #endif |
99 | |
118 | |
100 | #ifndef EV_USE_WIN32 |
|
|
101 | # ifdef WIN32 |
|
|
102 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
103 | # undef EV_USE_SELECT |
|
|
104 | # define EV_USE_SELECT 1 |
|
|
105 | # else |
|
|
106 | # define EV_USE_WIN32 0 |
|
|
107 | # endif |
|
|
108 | #endif |
|
|
109 | |
|
|
110 | #ifndef EV_USE_REALTIME |
119 | #ifndef EV_USE_REALTIME |
111 | # define EV_USE_REALTIME 1 |
120 | # define EV_USE_REALTIME 1 |
112 | #endif |
121 | #endif |
113 | |
122 | |
114 | /**/ |
123 | /**/ |
|
|
124 | |
|
|
125 | /* darwin simply cannot be helped */ |
|
|
126 | #ifdef __APPLE__ |
|
|
127 | # undef EV_USE_POLL |
|
|
128 | # undef EV_USE_KQUEUE |
|
|
129 | #endif |
115 | |
130 | |
116 | #ifndef CLOCK_MONOTONIC |
131 | #ifndef CLOCK_MONOTONIC |
117 | # undef EV_USE_MONOTONIC |
132 | # undef EV_USE_MONOTONIC |
118 | # define EV_USE_MONOTONIC 0 |
133 | # define EV_USE_MONOTONIC 0 |
119 | #endif |
134 | #endif |
120 | |
135 | |
121 | #ifndef CLOCK_REALTIME |
136 | #ifndef CLOCK_REALTIME |
122 | # undef EV_USE_REALTIME |
137 | # undef EV_USE_REALTIME |
123 | # define EV_USE_REALTIME 0 |
138 | # define EV_USE_REALTIME 0 |
|
|
139 | #endif |
|
|
140 | |
|
|
141 | #if EV_SELECT_IS_WINSOCKET |
|
|
142 | # include <winsock.h> |
124 | #endif |
143 | #endif |
125 | |
144 | |
126 | /**/ |
145 | /**/ |
127 | |
146 | |
128 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
147 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
129 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
148 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
130 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
149 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
131 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
150 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
132 | |
151 | |
|
|
152 | #ifdef EV_H |
|
|
153 | # include EV_H |
|
|
154 | #else |
133 | #include "ev.h" |
155 | # include "ev.h" |
|
|
156 | #endif |
134 | |
157 | |
135 | #if __GNUC__ >= 3 |
158 | #if __GNUC__ >= 3 |
136 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
159 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
137 | # define inline inline |
160 | # define inline inline |
138 | #else |
161 | #else |
… | |
… | |
144 | #define expect_true(expr) expect ((expr) != 0, 1) |
167 | #define expect_true(expr) expect ((expr) != 0, 1) |
145 | |
168 | |
146 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
169 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
147 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
170 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
148 | |
171 | |
|
|
172 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
|
|
173 | |
149 | typedef struct ev_watcher *W; |
174 | typedef struct ev_watcher *W; |
150 | typedef struct ev_watcher_list *WL; |
175 | typedef struct ev_watcher_list *WL; |
151 | typedef struct ev_watcher_time *WT; |
176 | typedef struct ev_watcher_time *WT; |
152 | |
177 | |
153 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
178 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
154 | |
179 | |
155 | #if WIN32 |
180 | #ifdef _WIN32 |
156 | /* note: the comment below could not be substantiated, but what would I care */ |
181 | # include "ev_win32.c" |
157 | /* MSDN says this is required to handle SIGFPE */ |
|
|
158 | volatile double SIGFPE_REQ = 0.0f; |
|
|
159 | #endif |
182 | #endif |
160 | |
183 | |
161 | /*****************************************************************************/ |
184 | /*****************************************************************************/ |
162 | |
185 | |
163 | static void (*syserr_cb)(const char *msg); |
186 | static void (*syserr_cb)(const char *msg); |
… | |
… | |
211 | typedef struct |
234 | typedef struct |
212 | { |
235 | { |
213 | WL head; |
236 | WL head; |
214 | unsigned char events; |
237 | unsigned char events; |
215 | unsigned char reify; |
238 | unsigned char reify; |
|
|
239 | #if EV_SELECT_IS_WINSOCKET |
|
|
240 | SOCKET handle; |
|
|
241 | #endif |
216 | } ANFD; |
242 | } ANFD; |
217 | |
243 | |
218 | typedef struct |
244 | typedef struct |
219 | { |
245 | { |
220 | W w; |
246 | W w; |
221 | int events; |
247 | int events; |
222 | } ANPENDING; |
248 | } ANPENDING; |
223 | |
249 | |
224 | #if EV_MULTIPLICITY |
250 | #if EV_MULTIPLICITY |
225 | |
251 | |
226 | struct ev_loop |
252 | struct ev_loop |
227 | { |
253 | { |
|
|
254 | ev_tstamp ev_rt_now; |
|
|
255 | #define ev_rt_now ((loop)->ev_rt_now) |
228 | # define VAR(name,decl) decl; |
256 | #define VAR(name,decl) decl; |
229 | # include "ev_vars.h" |
257 | #include "ev_vars.h" |
230 | }; |
|
|
231 | # undef VAR |
258 | #undef VAR |
|
|
259 | }; |
232 | # include "ev_wrap.h" |
260 | #include "ev_wrap.h" |
|
|
261 | |
|
|
262 | struct ev_loop default_loop_struct; |
|
|
263 | static struct ev_loop *default_loop; |
233 | |
264 | |
234 | #else |
265 | #else |
235 | |
266 | |
|
|
267 | ev_tstamp ev_rt_now; |
236 | # define VAR(name,decl) static decl; |
268 | #define VAR(name,decl) static decl; |
237 | # include "ev_vars.h" |
269 | #include "ev_vars.h" |
238 | # undef VAR |
270 | #undef VAR |
|
|
271 | |
|
|
272 | static int default_loop; |
239 | |
273 | |
240 | #endif |
274 | #endif |
241 | |
275 | |
242 | /*****************************************************************************/ |
276 | /*****************************************************************************/ |
243 | |
277 | |
244 | inline ev_tstamp |
278 | ev_tstamp |
245 | ev_time (void) |
279 | ev_time (void) |
246 | { |
280 | { |
247 | #if EV_USE_REALTIME |
281 | #if EV_USE_REALTIME |
248 | struct timespec ts; |
282 | struct timespec ts; |
249 | clock_gettime (CLOCK_REALTIME, &ts); |
283 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
268 | #endif |
302 | #endif |
269 | |
303 | |
270 | return ev_time (); |
304 | return ev_time (); |
271 | } |
305 | } |
272 | |
306 | |
|
|
307 | #if EV_MULTIPLICITY |
273 | ev_tstamp |
308 | ev_tstamp |
274 | ev_now (EV_P) |
309 | ev_now (EV_P) |
275 | { |
310 | { |
276 | return rt_now; |
311 | return ev_rt_now; |
277 | } |
312 | } |
|
|
313 | #endif |
278 | |
314 | |
279 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
315 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
280 | |
316 | |
281 | #define array_needsize(base,cur,cnt,init) \ |
317 | #define array_needsize(type,base,cur,cnt,init) \ |
282 | if (expect_false ((cnt) > cur)) \ |
318 | if (expect_false ((cnt) > cur)) \ |
283 | { \ |
319 | { \ |
284 | int newcnt = cur; \ |
320 | int newcnt = cur; \ |
285 | do \ |
321 | do \ |
286 | { \ |
322 | { \ |
287 | newcnt = array_roundsize (base, newcnt << 1); \ |
323 | newcnt = array_roundsize (type, newcnt << 1); \ |
288 | } \ |
324 | } \ |
289 | while ((cnt) > newcnt); \ |
325 | while ((cnt) > newcnt); \ |
290 | \ |
326 | \ |
291 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
327 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
292 | init (base + cur, newcnt - cur); \ |
328 | init (base + cur, newcnt - cur); \ |
293 | cur = newcnt; \ |
329 | cur = newcnt; \ |
294 | } |
330 | } |
295 | |
331 | |
296 | #define array_slim(stem) \ |
332 | #define array_slim(type,stem) \ |
297 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
333 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
298 | { \ |
334 | { \ |
299 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
335 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
300 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
336 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
301 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
337 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
302 | } |
338 | } |
303 | |
|
|
304 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
305 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
306 | #define array_free_microshit(stem) \ |
|
|
307 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
308 | |
339 | |
309 | #define array_free(stem, idx) \ |
340 | #define array_free(stem, idx) \ |
310 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
341 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
311 | |
342 | |
312 | /*****************************************************************************/ |
343 | /*****************************************************************************/ |
… | |
… | |
322 | |
353 | |
323 | ++base; |
354 | ++base; |
324 | } |
355 | } |
325 | } |
356 | } |
326 | |
357 | |
327 | static void |
358 | void |
328 | event (EV_P_ W w, int events) |
359 | ev_feed_event (EV_P_ void *w, int revents) |
329 | { |
360 | { |
|
|
361 | W w_ = (W)w; |
|
|
362 | |
330 | if (w->pending) |
363 | if (w_->pending) |
331 | { |
364 | { |
332 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
365 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
333 | return; |
366 | return; |
334 | } |
367 | } |
335 | |
368 | |
336 | w->pending = ++pendingcnt [ABSPRI (w)]; |
369 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
337 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
370 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
338 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
371 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
339 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
372 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
340 | } |
373 | } |
341 | |
374 | |
342 | static void |
375 | static void |
343 | queue_events (EV_P_ W *events, int eventcnt, int type) |
376 | queue_events (EV_P_ W *events, int eventcnt, int type) |
344 | { |
377 | { |
345 | int i; |
378 | int i; |
346 | |
379 | |
347 | for (i = 0; i < eventcnt; ++i) |
380 | for (i = 0; i < eventcnt; ++i) |
348 | event (EV_A_ events [i], type); |
381 | ev_feed_event (EV_A_ events [i], type); |
349 | } |
382 | } |
350 | |
383 | |
351 | static void |
384 | inline void |
352 | fd_event (EV_P_ int fd, int events) |
385 | fd_event (EV_P_ int fd, int revents) |
353 | { |
386 | { |
354 | ANFD *anfd = anfds + fd; |
387 | ANFD *anfd = anfds + fd; |
355 | struct ev_io *w; |
388 | struct ev_io *w; |
356 | |
389 | |
357 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
390 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
358 | { |
391 | { |
359 | int ev = w->events & events; |
392 | int ev = w->events & revents; |
360 | |
393 | |
361 | if (ev) |
394 | if (ev) |
362 | event (EV_A_ (W)w, ev); |
395 | ev_feed_event (EV_A_ (W)w, ev); |
363 | } |
396 | } |
|
|
397 | } |
|
|
398 | |
|
|
399 | void |
|
|
400 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
401 | { |
|
|
402 | fd_event (EV_A_ fd, revents); |
364 | } |
403 | } |
365 | |
404 | |
366 | /*****************************************************************************/ |
405 | /*****************************************************************************/ |
367 | |
406 | |
368 | static void |
407 | static void |
… | |
… | |
379 | int events = 0; |
418 | int events = 0; |
380 | |
419 | |
381 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
420 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
382 | events |= w->events; |
421 | events |= w->events; |
383 | |
422 | |
|
|
423 | #if EV_SELECT_IS_WINSOCKET |
|
|
424 | if (events) |
|
|
425 | { |
|
|
426 | unsigned long argp; |
|
|
427 | anfd->handle = _get_osfhandle (fd); |
|
|
428 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
|
|
429 | } |
|
|
430 | #endif |
|
|
431 | |
384 | anfd->reify = 0; |
432 | anfd->reify = 0; |
385 | |
433 | |
386 | method_modify (EV_A_ fd, anfd->events, events); |
434 | method_modify (EV_A_ fd, anfd->events, events); |
387 | anfd->events = events; |
435 | anfd->events = events; |
388 | } |
436 | } |
… | |
… | |
397 | return; |
445 | return; |
398 | |
446 | |
399 | anfds [fd].reify = 1; |
447 | anfds [fd].reify = 1; |
400 | |
448 | |
401 | ++fdchangecnt; |
449 | ++fdchangecnt; |
402 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
450 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
403 | fdchanges [fdchangecnt - 1] = fd; |
451 | fdchanges [fdchangecnt - 1] = fd; |
404 | } |
452 | } |
405 | |
453 | |
406 | static void |
454 | static void |
407 | fd_kill (EV_P_ int fd) |
455 | fd_kill (EV_P_ int fd) |
… | |
… | |
409 | struct ev_io *w; |
457 | struct ev_io *w; |
410 | |
458 | |
411 | while ((w = (struct ev_io *)anfds [fd].head)) |
459 | while ((w = (struct ev_io *)anfds [fd].head)) |
412 | { |
460 | { |
413 | ev_io_stop (EV_A_ w); |
461 | ev_io_stop (EV_A_ w); |
414 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
462 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
415 | } |
463 | } |
416 | } |
464 | } |
417 | |
465 | |
418 | static int |
466 | static int |
419 | fd_valid (int fd) |
467 | fd_valid (int fd) |
420 | { |
468 | { |
421 | #ifdef WIN32 |
469 | #ifdef _WIN32 |
422 | return !!win32_get_osfhandle (fd); |
470 | return _get_osfhandle (fd) != -1; |
423 | #else |
471 | #else |
424 | return fcntl (fd, F_GETFD) != -1; |
472 | return fcntl (fd, F_GETFD) != -1; |
425 | #endif |
473 | #endif |
426 | } |
474 | } |
427 | |
475 | |
… | |
… | |
507 | |
555 | |
508 | heap [k] = w; |
556 | heap [k] = w; |
509 | ((W)heap [k])->active = k + 1; |
557 | ((W)heap [k])->active = k + 1; |
510 | } |
558 | } |
511 | |
559 | |
|
|
560 | inline void |
|
|
561 | adjustheap (WT *heap, int N, int k) |
|
|
562 | { |
|
|
563 | upheap (heap, k); |
|
|
564 | downheap (heap, N, k); |
|
|
565 | } |
|
|
566 | |
512 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
513 | |
568 | |
514 | typedef struct |
569 | typedef struct |
515 | { |
570 | { |
516 | WL head; |
571 | WL head; |
… | |
… | |
537 | } |
592 | } |
538 | |
593 | |
539 | static void |
594 | static void |
540 | sighandler (int signum) |
595 | sighandler (int signum) |
541 | { |
596 | { |
542 | #if WIN32 |
597 | #if _WIN32 |
543 | signal (signum, sighandler); |
598 | signal (signum, sighandler); |
544 | #endif |
599 | #endif |
545 | |
600 | |
546 | signals [signum - 1].gotsig = 1; |
601 | signals [signum - 1].gotsig = 1; |
547 | |
602 | |
… | |
… | |
552 | write (sigpipe [1], &signum, 1); |
607 | write (sigpipe [1], &signum, 1); |
553 | errno = old_errno; |
608 | errno = old_errno; |
554 | } |
609 | } |
555 | } |
610 | } |
556 | |
611 | |
|
|
612 | void |
|
|
613 | ev_feed_signal_event (EV_P_ int signum) |
|
|
614 | { |
|
|
615 | WL w; |
|
|
616 | |
|
|
617 | #if EV_MULTIPLICITY |
|
|
618 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
619 | #endif |
|
|
620 | |
|
|
621 | --signum; |
|
|
622 | |
|
|
623 | if (signum < 0 || signum >= signalmax) |
|
|
624 | return; |
|
|
625 | |
|
|
626 | signals [signum].gotsig = 0; |
|
|
627 | |
|
|
628 | for (w = signals [signum].head; w; w = w->next) |
|
|
629 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
630 | } |
|
|
631 | |
557 | static void |
632 | static void |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
633 | sigcb (EV_P_ struct ev_io *iow, int revents) |
559 | { |
634 | { |
560 | WL w; |
|
|
561 | int signum; |
635 | int signum; |
562 | |
636 | |
563 | read (sigpipe [0], &revents, 1); |
637 | read (sigpipe [0], &revents, 1); |
564 | gotsig = 0; |
638 | gotsig = 0; |
565 | |
639 | |
566 | for (signum = signalmax; signum--; ) |
640 | for (signum = signalmax; signum--; ) |
567 | if (signals [signum].gotsig) |
641 | if (signals [signum].gotsig) |
568 | { |
642 | ev_feed_signal_event (EV_A_ signum + 1); |
569 | signals [signum].gotsig = 0; |
643 | } |
570 | |
644 | |
571 | for (w = signals [signum].head; w; w = w->next) |
645 | inline void |
572 | event (EV_A_ (W)w, EV_SIGNAL); |
646 | fd_intern (int fd) |
573 | } |
647 | { |
|
|
648 | #ifdef _WIN32 |
|
|
649 | int arg = 1; |
|
|
650 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
651 | #else |
|
|
652 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
653 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
654 | #endif |
574 | } |
655 | } |
575 | |
656 | |
576 | static void |
657 | static void |
577 | siginit (EV_P) |
658 | siginit (EV_P) |
578 | { |
659 | { |
579 | #ifndef WIN32 |
660 | fd_intern (sigpipe [0]); |
580 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
661 | fd_intern (sigpipe [1]); |
581 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
582 | |
|
|
583 | /* rather than sort out wether we really need nb, set it */ |
|
|
584 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
585 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
586 | #endif |
|
|
587 | |
662 | |
588 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
663 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
589 | ev_io_start (EV_A_ &sigev); |
664 | ev_io_start (EV_A_ &sigev); |
590 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
665 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
591 | } |
666 | } |
592 | |
667 | |
593 | /*****************************************************************************/ |
668 | /*****************************************************************************/ |
594 | |
669 | |
595 | static struct ev_child *childs [PID_HASHSIZE]; |
670 | static struct ev_child *childs [PID_HASHSIZE]; |
596 | |
671 | |
597 | #ifndef WIN32 |
672 | #ifndef _WIN32 |
598 | |
673 | |
599 | static struct ev_signal childev; |
674 | static struct ev_signal childev; |
600 | |
675 | |
601 | #ifndef WCONTINUED |
676 | #ifndef WCONTINUED |
602 | # define WCONTINUED 0 |
677 | # define WCONTINUED 0 |
… | |
… | |
611 | if (w->pid == pid || !w->pid) |
686 | if (w->pid == pid || !w->pid) |
612 | { |
687 | { |
613 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
688 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
614 | w->rpid = pid; |
689 | w->rpid = pid; |
615 | w->rstatus = status; |
690 | w->rstatus = status; |
616 | event (EV_A_ (W)w, EV_CHILD); |
691 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
617 | } |
692 | } |
618 | } |
693 | } |
619 | |
694 | |
620 | static void |
695 | static void |
621 | childcb (EV_P_ struct ev_signal *sw, int revents) |
696 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
623 | int pid, status; |
698 | int pid, status; |
624 | |
699 | |
625 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
700 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
626 | { |
701 | { |
627 | /* make sure we are called again until all childs have been reaped */ |
702 | /* make sure we are called again until all childs have been reaped */ |
628 | event (EV_A_ (W)sw, EV_SIGNAL); |
703 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
629 | |
704 | |
630 | child_reap (EV_A_ sw, pid, pid, status); |
705 | child_reap (EV_A_ sw, pid, pid, status); |
631 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
706 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
632 | } |
707 | } |
633 | } |
708 | } |
… | |
… | |
663 | |
738 | |
664 | /* return true if we are running with elevated privileges and should ignore env variables */ |
739 | /* return true if we are running with elevated privileges and should ignore env variables */ |
665 | static int |
740 | static int |
666 | enable_secure (void) |
741 | enable_secure (void) |
667 | { |
742 | { |
668 | #ifdef WIN32 |
743 | #ifdef _WIN32 |
669 | return 0; |
744 | return 0; |
670 | #else |
745 | #else |
671 | return getuid () != geteuid () |
746 | return getuid () != geteuid () |
672 | || getgid () != getegid (); |
747 | || getgid () != getegid (); |
673 | #endif |
748 | #endif |
… | |
… | |
678 | { |
753 | { |
679 | return method; |
754 | return method; |
680 | } |
755 | } |
681 | |
756 | |
682 | static void |
757 | static void |
683 | loop_init (EV_P_ int methods) |
758 | loop_init (EV_P_ unsigned int flags) |
684 | { |
759 | { |
685 | if (!method) |
760 | if (!method) |
686 | { |
761 | { |
687 | #if EV_USE_MONOTONIC |
762 | #if EV_USE_MONOTONIC |
688 | { |
763 | { |
… | |
… | |
690 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
765 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
691 | have_monotonic = 1; |
766 | have_monotonic = 1; |
692 | } |
767 | } |
693 | #endif |
768 | #endif |
694 | |
769 | |
695 | rt_now = ev_time (); |
770 | ev_rt_now = ev_time (); |
696 | mn_now = get_clock (); |
771 | mn_now = get_clock (); |
697 | now_floor = mn_now; |
772 | now_floor = mn_now; |
698 | rtmn_diff = rt_now - mn_now; |
773 | rtmn_diff = ev_rt_now - mn_now; |
699 | |
774 | |
700 | if (methods == EVMETHOD_AUTO) |
775 | if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
701 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
702 | methods = atoi (getenv ("LIBEV_METHODS")); |
776 | flags = atoi (getenv ("LIBEV_FLAGS")); |
703 | else |
777 | |
704 | methods = EVMETHOD_ANY; |
778 | if (!(flags & 0x0000ffff)) |
|
|
779 | flags |= 0x0000ffff; |
705 | |
780 | |
706 | method = 0; |
781 | method = 0; |
707 | #if EV_USE_WIN32 |
|
|
708 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
709 | #endif |
|
|
710 | #if EV_USE_KQUEUE |
782 | #if EV_USE_KQUEUE |
711 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
783 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
712 | #endif |
784 | #endif |
713 | #if EV_USE_EPOLL |
785 | #if EV_USE_EPOLL |
714 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
786 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
715 | #endif |
787 | #endif |
716 | #if EV_USE_POLL |
788 | #if EV_USE_POLL |
717 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
789 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
718 | #endif |
790 | #endif |
719 | #if EV_USE_SELECT |
791 | #if EV_USE_SELECT |
720 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
792 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
721 | #endif |
793 | #endif |
722 | |
794 | |
723 | ev_watcher_init (&sigev, sigcb); |
795 | ev_init (&sigev, sigcb); |
724 | ev_set_priority (&sigev, EV_MAXPRI); |
796 | ev_set_priority (&sigev, EV_MAXPRI); |
725 | } |
797 | } |
726 | } |
798 | } |
727 | |
799 | |
728 | void |
800 | void |
729 | loop_destroy (EV_P) |
801 | loop_destroy (EV_P) |
730 | { |
802 | { |
731 | int i; |
803 | int i; |
732 | |
804 | |
733 | #if EV_USE_WIN32 |
|
|
734 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
735 | #endif |
|
|
736 | #if EV_USE_KQUEUE |
805 | #if EV_USE_KQUEUE |
737 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
806 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
738 | #endif |
807 | #endif |
739 | #if EV_USE_EPOLL |
808 | #if EV_USE_EPOLL |
740 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
809 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
748 | |
817 | |
749 | for (i = NUMPRI; i--; ) |
818 | for (i = NUMPRI; i--; ) |
750 | array_free (pending, [i]); |
819 | array_free (pending, [i]); |
751 | |
820 | |
752 | /* have to use the microsoft-never-gets-it-right macro */ |
821 | /* have to use the microsoft-never-gets-it-right macro */ |
753 | array_free_microshit (fdchange); |
822 | array_free (fdchange, EMPTY); |
754 | array_free_microshit (timer); |
823 | array_free (timer, EMPTY); |
755 | array_free_microshit (periodic); |
824 | #if EV_PERIODICS |
756 | array_free_microshit (idle); |
825 | array_free (periodic, EMPTY); |
757 | array_free_microshit (prepare); |
826 | #endif |
758 | array_free_microshit (check); |
827 | array_free (idle, EMPTY); |
|
|
828 | array_free (prepare, EMPTY); |
|
|
829 | array_free (check, EMPTY); |
759 | |
830 | |
760 | method = 0; |
831 | method = 0; |
761 | } |
832 | } |
762 | |
833 | |
763 | static void |
834 | static void |
… | |
… | |
788 | postfork = 0; |
859 | postfork = 0; |
789 | } |
860 | } |
790 | |
861 | |
791 | #if EV_MULTIPLICITY |
862 | #if EV_MULTIPLICITY |
792 | struct ev_loop * |
863 | struct ev_loop * |
793 | ev_loop_new (int methods) |
864 | ev_loop_new (unsigned int flags) |
794 | { |
865 | { |
795 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
866 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
796 | |
867 | |
797 | memset (loop, 0, sizeof (struct ev_loop)); |
868 | memset (loop, 0, sizeof (struct ev_loop)); |
798 | |
869 | |
799 | loop_init (EV_A_ methods); |
870 | loop_init (EV_A_ flags); |
800 | |
871 | |
801 | if (ev_method (EV_A)) |
872 | if (ev_method (EV_A)) |
802 | return loop; |
873 | return loop; |
803 | |
874 | |
804 | return 0; |
875 | return 0; |
… | |
… | |
818 | } |
889 | } |
819 | |
890 | |
820 | #endif |
891 | #endif |
821 | |
892 | |
822 | #if EV_MULTIPLICITY |
893 | #if EV_MULTIPLICITY |
823 | struct ev_loop default_loop_struct; |
|
|
824 | static struct ev_loop *default_loop; |
|
|
825 | |
|
|
826 | struct ev_loop * |
894 | struct ev_loop * |
827 | #else |
895 | #else |
828 | static int default_loop; |
|
|
829 | |
|
|
830 | int |
896 | int |
831 | #endif |
897 | #endif |
832 | ev_default_loop (int methods) |
898 | ev_default_loop (unsigned int flags) |
833 | { |
899 | { |
834 | if (sigpipe [0] == sigpipe [1]) |
900 | if (sigpipe [0] == sigpipe [1]) |
835 | if (pipe (sigpipe)) |
901 | if (pipe (sigpipe)) |
836 | return 0; |
902 | return 0; |
837 | |
903 | |
… | |
… | |
841 | struct ev_loop *loop = default_loop = &default_loop_struct; |
907 | struct ev_loop *loop = default_loop = &default_loop_struct; |
842 | #else |
908 | #else |
843 | default_loop = 1; |
909 | default_loop = 1; |
844 | #endif |
910 | #endif |
845 | |
911 | |
846 | loop_init (EV_A_ methods); |
912 | loop_init (EV_A_ flags); |
847 | |
913 | |
848 | if (ev_method (EV_A)) |
914 | if (ev_method (EV_A)) |
849 | { |
915 | { |
850 | siginit (EV_A); |
916 | siginit (EV_A); |
851 | |
917 | |
852 | #ifndef WIN32 |
918 | #ifndef _WIN32 |
853 | ev_signal_init (&childev, childcb, SIGCHLD); |
919 | ev_signal_init (&childev, childcb, SIGCHLD); |
854 | ev_set_priority (&childev, EV_MAXPRI); |
920 | ev_set_priority (&childev, EV_MAXPRI); |
855 | ev_signal_start (EV_A_ &childev); |
921 | ev_signal_start (EV_A_ &childev); |
856 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
922 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
857 | #endif |
923 | #endif |
… | |
… | |
868 | { |
934 | { |
869 | #if EV_MULTIPLICITY |
935 | #if EV_MULTIPLICITY |
870 | struct ev_loop *loop = default_loop; |
936 | struct ev_loop *loop = default_loop; |
871 | #endif |
937 | #endif |
872 | |
938 | |
873 | #ifndef WIN32 |
939 | #ifndef _WIN32 |
874 | ev_ref (EV_A); /* child watcher */ |
940 | ev_ref (EV_A); /* child watcher */ |
875 | ev_signal_stop (EV_A_ &childev); |
941 | ev_signal_stop (EV_A_ &childev); |
876 | #endif |
942 | #endif |
877 | |
943 | |
878 | ev_ref (EV_A); /* signal watcher */ |
944 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
894 | if (method) |
960 | if (method) |
895 | postfork = 1; |
961 | postfork = 1; |
896 | } |
962 | } |
897 | |
963 | |
898 | /*****************************************************************************/ |
964 | /*****************************************************************************/ |
|
|
965 | |
|
|
966 | static int |
|
|
967 | any_pending (EV_P) |
|
|
968 | { |
|
|
969 | int pri; |
|
|
970 | |
|
|
971 | for (pri = NUMPRI; pri--; ) |
|
|
972 | if (pendingcnt [pri]) |
|
|
973 | return 1; |
|
|
974 | |
|
|
975 | return 0; |
|
|
976 | } |
899 | |
977 | |
900 | static void |
978 | static void |
901 | call_pending (EV_P) |
979 | call_pending (EV_P) |
902 | { |
980 | { |
903 | int pri; |
981 | int pri; |
… | |
… | |
908 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
986 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
909 | |
987 | |
910 | if (p->w) |
988 | if (p->w) |
911 | { |
989 | { |
912 | p->w->pending = 0; |
990 | p->w->pending = 0; |
913 | p->w->cb (EV_A_ p->w, p->events); |
991 | EV_CB_INVOKE (p->w, p->events); |
914 | } |
992 | } |
915 | } |
993 | } |
916 | } |
994 | } |
917 | |
995 | |
918 | static void |
996 | static void |
… | |
… | |
926 | |
1004 | |
927 | /* first reschedule or stop timer */ |
1005 | /* first reschedule or stop timer */ |
928 | if (w->repeat) |
1006 | if (w->repeat) |
929 | { |
1007 | { |
930 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1008 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1009 | |
931 | ((WT)w)->at = mn_now + w->repeat; |
1010 | ((WT)w)->at += w->repeat; |
|
|
1011 | if (((WT)w)->at < mn_now) |
|
|
1012 | ((WT)w)->at = mn_now; |
|
|
1013 | |
932 | downheap ((WT *)timers, timercnt, 0); |
1014 | downheap ((WT *)timers, timercnt, 0); |
933 | } |
1015 | } |
934 | else |
1016 | else |
935 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1017 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
936 | |
1018 | |
937 | event (EV_A_ (W)w, EV_TIMEOUT); |
1019 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
938 | } |
1020 | } |
939 | } |
1021 | } |
940 | |
1022 | |
|
|
1023 | #if EV_PERIODICS |
941 | static void |
1024 | static void |
942 | periodics_reify (EV_P) |
1025 | periodics_reify (EV_P) |
943 | { |
1026 | { |
944 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1027 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
945 | { |
1028 | { |
946 | struct ev_periodic *w = periodics [0]; |
1029 | struct ev_periodic *w = periodics [0]; |
947 | |
1030 | |
948 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1031 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
949 | |
1032 | |
950 | /* first reschedule or stop timer */ |
1033 | /* first reschedule or stop timer */ |
951 | if (w->interval) |
1034 | if (w->reschedule_cb) |
952 | { |
1035 | { |
|
|
1036 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1037 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1038 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1039 | } |
|
|
1040 | else if (w->interval) |
|
|
1041 | { |
953 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1042 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
954 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1043 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
955 | downheap ((WT *)periodics, periodiccnt, 0); |
1044 | downheap ((WT *)periodics, periodiccnt, 0); |
956 | } |
1045 | } |
957 | else |
1046 | else |
958 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1047 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
959 | |
1048 | |
960 | event (EV_A_ (W)w, EV_PERIODIC); |
1049 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
961 | } |
1050 | } |
962 | } |
1051 | } |
963 | |
1052 | |
964 | static void |
1053 | static void |
965 | periodics_reschedule (EV_P) |
1054 | periodics_reschedule (EV_P) |
… | |
… | |
969 | /* adjust periodics after time jump */ |
1058 | /* adjust periodics after time jump */ |
970 | for (i = 0; i < periodiccnt; ++i) |
1059 | for (i = 0; i < periodiccnt; ++i) |
971 | { |
1060 | { |
972 | struct ev_periodic *w = periodics [i]; |
1061 | struct ev_periodic *w = periodics [i]; |
973 | |
1062 | |
|
|
1063 | if (w->reschedule_cb) |
|
|
1064 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
974 | if (w->interval) |
1065 | else if (w->interval) |
975 | { |
|
|
976 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1066 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
977 | |
|
|
978 | if (fabs (diff) >= 1e-4) |
|
|
979 | { |
|
|
980 | ev_periodic_stop (EV_A_ w); |
|
|
981 | ev_periodic_start (EV_A_ w); |
|
|
982 | |
|
|
983 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
984 | } |
|
|
985 | } |
|
|
986 | } |
1067 | } |
|
|
1068 | |
|
|
1069 | /* now rebuild the heap */ |
|
|
1070 | for (i = periodiccnt >> 1; i--; ) |
|
|
1071 | downheap ((WT *)periodics, periodiccnt, i); |
987 | } |
1072 | } |
|
|
1073 | #endif |
988 | |
1074 | |
989 | inline int |
1075 | inline int |
990 | time_update_monotonic (EV_P) |
1076 | time_update_monotonic (EV_P) |
991 | { |
1077 | { |
992 | mn_now = get_clock (); |
1078 | mn_now = get_clock (); |
993 | |
1079 | |
994 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1080 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
995 | { |
1081 | { |
996 | rt_now = rtmn_diff + mn_now; |
1082 | ev_rt_now = rtmn_diff + mn_now; |
997 | return 0; |
1083 | return 0; |
998 | } |
1084 | } |
999 | else |
1085 | else |
1000 | { |
1086 | { |
1001 | now_floor = mn_now; |
1087 | now_floor = mn_now; |
1002 | rt_now = ev_time (); |
1088 | ev_rt_now = ev_time (); |
1003 | return 1; |
1089 | return 1; |
1004 | } |
1090 | } |
1005 | } |
1091 | } |
1006 | |
1092 | |
1007 | static void |
1093 | static void |
… | |
… | |
1016 | { |
1102 | { |
1017 | ev_tstamp odiff = rtmn_diff; |
1103 | ev_tstamp odiff = rtmn_diff; |
1018 | |
1104 | |
1019 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1105 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1020 | { |
1106 | { |
1021 | rtmn_diff = rt_now - mn_now; |
1107 | rtmn_diff = ev_rt_now - mn_now; |
1022 | |
1108 | |
1023 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1109 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1024 | return; /* all is well */ |
1110 | return; /* all is well */ |
1025 | |
1111 | |
1026 | rt_now = ev_time (); |
1112 | ev_rt_now = ev_time (); |
1027 | mn_now = get_clock (); |
1113 | mn_now = get_clock (); |
1028 | now_floor = mn_now; |
1114 | now_floor = mn_now; |
1029 | } |
1115 | } |
1030 | |
1116 | |
|
|
1117 | # if EV_PERIODICS |
1031 | periodics_reschedule (EV_A); |
1118 | periodics_reschedule (EV_A); |
|
|
1119 | # endif |
1032 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1120 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1033 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1121 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1034 | } |
1122 | } |
1035 | } |
1123 | } |
1036 | else |
1124 | else |
1037 | #endif |
1125 | #endif |
1038 | { |
1126 | { |
1039 | rt_now = ev_time (); |
1127 | ev_rt_now = ev_time (); |
1040 | |
1128 | |
1041 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1129 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1042 | { |
1130 | { |
|
|
1131 | #if EV_PERIODICS |
1043 | periodics_reschedule (EV_A); |
1132 | periodics_reschedule (EV_A); |
|
|
1133 | #endif |
1044 | |
1134 | |
1045 | /* adjust timers. this is easy, as the offset is the same for all */ |
1135 | /* adjust timers. this is easy, as the offset is the same for all */ |
1046 | for (i = 0; i < timercnt; ++i) |
1136 | for (i = 0; i < timercnt; ++i) |
1047 | ((WT)timers [i])->at += rt_now - mn_now; |
1137 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1048 | } |
1138 | } |
1049 | |
1139 | |
1050 | mn_now = rt_now; |
1140 | mn_now = ev_rt_now; |
1051 | } |
1141 | } |
1052 | } |
1142 | } |
1053 | |
1143 | |
1054 | void |
1144 | void |
1055 | ev_ref (EV_P) |
1145 | ev_ref (EV_P) |
… | |
… | |
1087 | /* update fd-related kernel structures */ |
1177 | /* update fd-related kernel structures */ |
1088 | fd_reify (EV_A); |
1178 | fd_reify (EV_A); |
1089 | |
1179 | |
1090 | /* calculate blocking time */ |
1180 | /* calculate blocking time */ |
1091 | |
1181 | |
1092 | /* we only need this for !monotonic clockor timers, but as we basically |
1182 | /* we only need this for !monotonic clock or timers, but as we basically |
1093 | always have timers, we just calculate it always */ |
1183 | always have timers, we just calculate it always */ |
1094 | #if EV_USE_MONOTONIC |
1184 | #if EV_USE_MONOTONIC |
1095 | if (expect_true (have_monotonic)) |
1185 | if (expect_true (have_monotonic)) |
1096 | time_update_monotonic (EV_A); |
1186 | time_update_monotonic (EV_A); |
1097 | else |
1187 | else |
1098 | #endif |
1188 | #endif |
1099 | { |
1189 | { |
1100 | rt_now = ev_time (); |
1190 | ev_rt_now = ev_time (); |
1101 | mn_now = rt_now; |
1191 | mn_now = ev_rt_now; |
1102 | } |
1192 | } |
1103 | |
1193 | |
1104 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1194 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1105 | block = 0.; |
1195 | block = 0.; |
1106 | else |
1196 | else |
… | |
… | |
1111 | { |
1201 | { |
1112 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1202 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1113 | if (block > to) block = to; |
1203 | if (block > to) block = to; |
1114 | } |
1204 | } |
1115 | |
1205 | |
|
|
1206 | #if EV_PERIODICS |
1116 | if (periodiccnt) |
1207 | if (periodiccnt) |
1117 | { |
1208 | { |
1118 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1209 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1119 | if (block > to) block = to; |
1210 | if (block > to) block = to; |
1120 | } |
1211 | } |
|
|
1212 | #endif |
1121 | |
1213 | |
1122 | if (block < 0.) block = 0.; |
1214 | if (block < 0.) block = 0.; |
1123 | } |
1215 | } |
1124 | |
1216 | |
1125 | method_poll (EV_A_ block); |
1217 | method_poll (EV_A_ block); |
1126 | |
1218 | |
1127 | /* update rt_now, do magic */ |
1219 | /* update ev_rt_now, do magic */ |
1128 | time_update (EV_A); |
1220 | time_update (EV_A); |
1129 | |
1221 | |
1130 | /* queue pending timers and reschedule them */ |
1222 | /* queue pending timers and reschedule them */ |
1131 | timers_reify (EV_A); /* relative timers called last */ |
1223 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1224 | #if EV_PERIODICS |
1132 | periodics_reify (EV_A); /* absolute timers called first */ |
1225 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1226 | #endif |
1133 | |
1227 | |
1134 | /* queue idle watchers unless io or timers are pending */ |
1228 | /* queue idle watchers unless io or timers are pending */ |
1135 | if (!pendingcnt) |
1229 | if (idlecnt && !any_pending (EV_A)) |
1136 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1230 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1137 | |
1231 | |
1138 | /* queue check watchers, to be executed first */ |
1232 | /* queue check watchers, to be executed first */ |
1139 | if (checkcnt) |
1233 | if (checkcnt) |
1140 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1234 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1215 | return; |
1309 | return; |
1216 | |
1310 | |
1217 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1311 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1218 | |
1312 | |
1219 | ev_start (EV_A_ (W)w, 1); |
1313 | ev_start (EV_A_ (W)w, 1); |
1220 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1314 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1221 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1315 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1222 | |
1316 | |
1223 | fd_change (EV_A_ fd); |
1317 | fd_change (EV_A_ fd); |
1224 | } |
1318 | } |
1225 | |
1319 | |
… | |
… | |
1228 | { |
1322 | { |
1229 | ev_clear_pending (EV_A_ (W)w); |
1323 | ev_clear_pending (EV_A_ (W)w); |
1230 | if (!ev_is_active (w)) |
1324 | if (!ev_is_active (w)) |
1231 | return; |
1325 | return; |
1232 | |
1326 | |
|
|
1327 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1328 | |
1233 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1329 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1234 | ev_stop (EV_A_ (W)w); |
1330 | ev_stop (EV_A_ (W)w); |
1235 | |
1331 | |
1236 | fd_change (EV_A_ w->fd); |
1332 | fd_change (EV_A_ w->fd); |
1237 | } |
1333 | } |
… | |
… | |
1245 | ((WT)w)->at += mn_now; |
1341 | ((WT)w)->at += mn_now; |
1246 | |
1342 | |
1247 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1343 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1248 | |
1344 | |
1249 | ev_start (EV_A_ (W)w, ++timercnt); |
1345 | ev_start (EV_A_ (W)w, ++timercnt); |
1250 | array_needsize (timers, timermax, timercnt, (void)); |
1346 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1251 | timers [timercnt - 1] = w; |
1347 | timers [timercnt - 1] = w; |
1252 | upheap ((WT *)timers, timercnt - 1); |
1348 | upheap ((WT *)timers, timercnt - 1); |
1253 | |
1349 | |
1254 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1350 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1255 | } |
1351 | } |
… | |
… | |
1264 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1360 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1265 | |
1361 | |
1266 | if (((W)w)->active < timercnt--) |
1362 | if (((W)w)->active < timercnt--) |
1267 | { |
1363 | { |
1268 | timers [((W)w)->active - 1] = timers [timercnt]; |
1364 | timers [((W)w)->active - 1] = timers [timercnt]; |
1269 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1365 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1270 | } |
1366 | } |
1271 | |
1367 | |
1272 | ((WT)w)->at = w->repeat; |
1368 | ((WT)w)->at -= mn_now; |
1273 | |
1369 | |
1274 | ev_stop (EV_A_ (W)w); |
1370 | ev_stop (EV_A_ (W)w); |
1275 | } |
1371 | } |
1276 | |
1372 | |
1277 | void |
1373 | void |
… | |
… | |
1280 | if (ev_is_active (w)) |
1376 | if (ev_is_active (w)) |
1281 | { |
1377 | { |
1282 | if (w->repeat) |
1378 | if (w->repeat) |
1283 | { |
1379 | { |
1284 | ((WT)w)->at = mn_now + w->repeat; |
1380 | ((WT)w)->at = mn_now + w->repeat; |
1285 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1381 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1286 | } |
1382 | } |
1287 | else |
1383 | else |
1288 | ev_timer_stop (EV_A_ w); |
1384 | ev_timer_stop (EV_A_ w); |
1289 | } |
1385 | } |
1290 | else if (w->repeat) |
1386 | else if (w->repeat) |
1291 | ev_timer_start (EV_A_ w); |
1387 | ev_timer_start (EV_A_ w); |
1292 | } |
1388 | } |
1293 | |
1389 | |
|
|
1390 | #if EV_PERIODICS |
1294 | void |
1391 | void |
1295 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1392 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1296 | { |
1393 | { |
1297 | if (ev_is_active (w)) |
1394 | if (ev_is_active (w)) |
1298 | return; |
1395 | return; |
1299 | |
1396 | |
|
|
1397 | if (w->reschedule_cb) |
|
|
1398 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1399 | else if (w->interval) |
|
|
1400 | { |
1300 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1401 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1301 | |
|
|
1302 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1402 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1303 | if (w->interval) |
|
|
1304 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1403 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1404 | } |
1305 | |
1405 | |
1306 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1406 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1307 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1407 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1308 | periodics [periodiccnt - 1] = w; |
1408 | periodics [periodiccnt - 1] = w; |
1309 | upheap ((WT *)periodics, periodiccnt - 1); |
1409 | upheap ((WT *)periodics, periodiccnt - 1); |
1310 | |
1410 | |
1311 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1411 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1312 | } |
1412 | } |
… | |
… | |
1321 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1421 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1322 | |
1422 | |
1323 | if (((W)w)->active < periodiccnt--) |
1423 | if (((W)w)->active < periodiccnt--) |
1324 | { |
1424 | { |
1325 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1425 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1326 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1426 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1327 | } |
1427 | } |
1328 | |
1428 | |
1329 | ev_stop (EV_A_ (W)w); |
1429 | ev_stop (EV_A_ (W)w); |
1330 | } |
1430 | } |
1331 | |
1431 | |
1332 | void |
1432 | void |
|
|
1433 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1434 | { |
|
|
1435 | /* TODO: use adjustheap and recalculation */ |
|
|
1436 | ev_periodic_stop (EV_A_ w); |
|
|
1437 | ev_periodic_start (EV_A_ w); |
|
|
1438 | } |
|
|
1439 | #endif |
|
|
1440 | |
|
|
1441 | void |
1333 | ev_idle_start (EV_P_ struct ev_idle *w) |
1442 | ev_idle_start (EV_P_ struct ev_idle *w) |
1334 | { |
1443 | { |
1335 | if (ev_is_active (w)) |
1444 | if (ev_is_active (w)) |
1336 | return; |
1445 | return; |
1337 | |
1446 | |
1338 | ev_start (EV_A_ (W)w, ++idlecnt); |
1447 | ev_start (EV_A_ (W)w, ++idlecnt); |
1339 | array_needsize (idles, idlemax, idlecnt, (void)); |
1448 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1340 | idles [idlecnt - 1] = w; |
1449 | idles [idlecnt - 1] = w; |
1341 | } |
1450 | } |
1342 | |
1451 | |
1343 | void |
1452 | void |
1344 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1453 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1345 | { |
1454 | { |
1346 | ev_clear_pending (EV_A_ (W)w); |
1455 | ev_clear_pending (EV_A_ (W)w); |
1347 | if (ev_is_active (w)) |
1456 | if (!ev_is_active (w)) |
1348 | return; |
1457 | return; |
1349 | |
1458 | |
1350 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1459 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1351 | ev_stop (EV_A_ (W)w); |
1460 | ev_stop (EV_A_ (W)w); |
1352 | } |
1461 | } |
… | |
… | |
1356 | { |
1465 | { |
1357 | if (ev_is_active (w)) |
1466 | if (ev_is_active (w)) |
1358 | return; |
1467 | return; |
1359 | |
1468 | |
1360 | ev_start (EV_A_ (W)w, ++preparecnt); |
1469 | ev_start (EV_A_ (W)w, ++preparecnt); |
1361 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1470 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1362 | prepares [preparecnt - 1] = w; |
1471 | prepares [preparecnt - 1] = w; |
1363 | } |
1472 | } |
1364 | |
1473 | |
1365 | void |
1474 | void |
1366 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1475 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1367 | { |
1476 | { |
1368 | ev_clear_pending (EV_A_ (W)w); |
1477 | ev_clear_pending (EV_A_ (W)w); |
1369 | if (ev_is_active (w)) |
1478 | if (!ev_is_active (w)) |
1370 | return; |
1479 | return; |
1371 | |
1480 | |
1372 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1481 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1373 | ev_stop (EV_A_ (W)w); |
1482 | ev_stop (EV_A_ (W)w); |
1374 | } |
1483 | } |
… | |
… | |
1378 | { |
1487 | { |
1379 | if (ev_is_active (w)) |
1488 | if (ev_is_active (w)) |
1380 | return; |
1489 | return; |
1381 | |
1490 | |
1382 | ev_start (EV_A_ (W)w, ++checkcnt); |
1491 | ev_start (EV_A_ (W)w, ++checkcnt); |
1383 | array_needsize (checks, checkmax, checkcnt, (void)); |
1492 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1384 | checks [checkcnt - 1] = w; |
1493 | checks [checkcnt - 1] = w; |
1385 | } |
1494 | } |
1386 | |
1495 | |
1387 | void |
1496 | void |
1388 | ev_check_stop (EV_P_ struct ev_check *w) |
1497 | ev_check_stop (EV_P_ struct ev_check *w) |
1389 | { |
1498 | { |
1390 | ev_clear_pending (EV_A_ (W)w); |
1499 | ev_clear_pending (EV_A_ (W)w); |
1391 | if (ev_is_active (w)) |
1500 | if (!ev_is_active (w)) |
1392 | return; |
1501 | return; |
1393 | |
1502 | |
1394 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1503 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1395 | ev_stop (EV_A_ (W)w); |
1504 | ev_stop (EV_A_ (W)w); |
1396 | } |
1505 | } |
… | |
… | |
1409 | return; |
1518 | return; |
1410 | |
1519 | |
1411 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1520 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1412 | |
1521 | |
1413 | ev_start (EV_A_ (W)w, 1); |
1522 | ev_start (EV_A_ (W)w, 1); |
1414 | array_needsize (signals, signalmax, w->signum, signals_init); |
1523 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1415 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1524 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1416 | |
1525 | |
1417 | if (!((WL)w)->next) |
1526 | if (!((WL)w)->next) |
1418 | { |
1527 | { |
1419 | #if WIN32 |
1528 | #if _WIN32 |
1420 | signal (w->signum, sighandler); |
1529 | signal (w->signum, sighandler); |
1421 | #else |
1530 | #else |
1422 | struct sigaction sa; |
1531 | struct sigaction sa; |
1423 | sa.sa_handler = sighandler; |
1532 | sa.sa_handler = sighandler; |
1424 | sigfillset (&sa.sa_mask); |
1533 | sigfillset (&sa.sa_mask); |
… | |
… | |
1457 | |
1566 | |
1458 | void |
1567 | void |
1459 | ev_child_stop (EV_P_ struct ev_child *w) |
1568 | ev_child_stop (EV_P_ struct ev_child *w) |
1460 | { |
1569 | { |
1461 | ev_clear_pending (EV_A_ (W)w); |
1570 | ev_clear_pending (EV_A_ (W)w); |
1462 | if (ev_is_active (w)) |
1571 | if (!ev_is_active (w)) |
1463 | return; |
1572 | return; |
1464 | |
1573 | |
1465 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1574 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1466 | ev_stop (EV_A_ (W)w); |
1575 | ev_stop (EV_A_ (W)w); |
1467 | } |
1576 | } |
… | |
… | |
1502 | } |
1611 | } |
1503 | |
1612 | |
1504 | void |
1613 | void |
1505 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1614 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1506 | { |
1615 | { |
1507 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1616 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1508 | |
1617 | |
1509 | if (!once) |
1618 | if (!once) |
1510 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1619 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1511 | else |
1620 | else |
1512 | { |
1621 | { |
1513 | once->cb = cb; |
1622 | once->cb = cb; |
1514 | once->arg = arg; |
1623 | once->arg = arg; |
1515 | |
1624 | |
1516 | ev_watcher_init (&once->io, once_cb_io); |
1625 | ev_init (&once->io, once_cb_io); |
1517 | if (fd >= 0) |
1626 | if (fd >= 0) |
1518 | { |
1627 | { |
1519 | ev_io_set (&once->io, fd, events); |
1628 | ev_io_set (&once->io, fd, events); |
1520 | ev_io_start (EV_A_ &once->io); |
1629 | ev_io_start (EV_A_ &once->io); |
1521 | } |
1630 | } |
1522 | |
1631 | |
1523 | ev_watcher_init (&once->to, once_cb_to); |
1632 | ev_init (&once->to, once_cb_to); |
1524 | if (timeout >= 0.) |
1633 | if (timeout >= 0.) |
1525 | { |
1634 | { |
1526 | ev_timer_set (&once->to, timeout, 0.); |
1635 | ev_timer_set (&once->to, timeout, 0.); |
1527 | ev_timer_start (EV_A_ &once->to); |
1636 | ev_timer_start (EV_A_ &once->to); |
1528 | } |
1637 | } |
1529 | } |
1638 | } |
1530 | } |
1639 | } |
1531 | |
1640 | |
|
|
1641 | #ifdef __cplusplus |
|
|
1642 | } |
|
|
1643 | #endif |
|
|
1644 | |