… | |
… | |
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 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
58 | |
58 | |
59 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
61 | #endif |
61 | #endif |
62 | |
62 | |
63 | #ifndef EV_USE_POLL |
63 | #ifndef EV_USEV_POLL |
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
65 | #endif |
66 | |
66 | |
67 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
69 | #endif |
69 | #endif |
… | |
… | |
113 | |
113 | |
114 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
117 | |
117 | |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | ev_tstamp ev_now; |
|
|
120 | int ev_method; |
|
|
121 | |
|
|
122 | static int have_monotonic; /* runtime */ |
|
|
123 | |
|
|
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
|
|
125 | static void (*method_modify)(int fd, int oev, int nev); |
|
|
126 | static void (*method_poll)(ev_tstamp timeout); |
|
|
127 | |
119 | |
128 | /*****************************************************************************/ |
120 | /*****************************************************************************/ |
129 | |
121 | |
130 | ev_tstamp |
122 | typedef struct |
|
|
123 | { |
|
|
124 | struct ev_watcher_list *head; |
|
|
125 | unsigned char events; |
|
|
126 | unsigned char reify; |
|
|
127 | } ANFD; |
|
|
128 | |
|
|
129 | typedef struct |
|
|
130 | { |
|
|
131 | W w; |
|
|
132 | int events; |
|
|
133 | } ANPENDING; |
|
|
134 | |
|
|
135 | #if EV_MULTIPLICITY |
|
|
136 | |
|
|
137 | struct ev_loop |
|
|
138 | { |
|
|
139 | # define VAR(name,decl) decl; |
|
|
140 | # include "ev_vars.h" |
|
|
141 | }; |
|
|
142 | # undef VAR |
|
|
143 | # include "ev_wrap.h" |
|
|
144 | |
|
|
145 | #else |
|
|
146 | |
|
|
147 | # define VAR(name,decl) static decl; |
|
|
148 | # include "ev_vars.h" |
|
|
149 | # undef VAR |
|
|
150 | |
|
|
151 | #endif |
|
|
152 | |
|
|
153 | /*****************************************************************************/ |
|
|
154 | |
|
|
155 | inline ev_tstamp |
131 | ev_time (void) |
156 | ev_time (void) |
132 | { |
157 | { |
133 | #if EV_USE_REALTIME |
158 | #if EV_USE_REALTIME |
134 | struct timespec ts; |
159 | struct timespec ts; |
135 | clock_gettime (CLOCK_REALTIME, &ts); |
160 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
139 | gettimeofday (&tv, 0); |
164 | gettimeofday (&tv, 0); |
140 | return tv.tv_sec + tv.tv_usec * 1e-6; |
165 | return tv.tv_sec + tv.tv_usec * 1e-6; |
141 | #endif |
166 | #endif |
142 | } |
167 | } |
143 | |
168 | |
144 | static ev_tstamp |
169 | inline ev_tstamp |
145 | get_clock (void) |
170 | get_clock (void) |
146 | { |
171 | { |
147 | #if EV_USE_MONOTONIC |
172 | #if EV_USE_MONOTONIC |
148 | if (expect_true (have_monotonic)) |
173 | if (expect_true (have_monotonic)) |
149 | { |
174 | { |
… | |
… | |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
177 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
153 | } |
178 | } |
154 | #endif |
179 | #endif |
155 | |
180 | |
156 | return ev_time (); |
181 | return ev_time (); |
|
|
182 | } |
|
|
183 | |
|
|
184 | ev_tstamp |
|
|
185 | ev_now (EV_P) |
|
|
186 | { |
|
|
187 | return rt_now; |
157 | } |
188 | } |
158 | |
189 | |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
190 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
160 | |
191 | |
161 | #define array_needsize(base,cur,cnt,init) \ |
192 | #define array_needsize(base,cur,cnt,init) \ |
… | |
… | |
173 | cur = newcnt; \ |
204 | cur = newcnt; \ |
174 | } |
205 | } |
175 | |
206 | |
176 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
177 | |
208 | |
178 | typedef struct |
|
|
179 | { |
|
|
180 | struct ev_io *head; |
|
|
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
|
|
183 | } ANFD; |
|
|
184 | |
|
|
185 | static ANFD *anfds; |
|
|
186 | static int anfdmax; |
|
|
187 | |
|
|
188 | static void |
209 | static void |
189 | anfds_init (ANFD *base, int count) |
210 | anfds_init (ANFD *base, int count) |
190 | { |
211 | { |
191 | while (count--) |
212 | while (count--) |
192 | { |
213 | { |
… | |
… | |
196 | |
217 | |
197 | ++base; |
218 | ++base; |
198 | } |
219 | } |
199 | } |
220 | } |
200 | |
221 | |
201 | typedef struct |
|
|
202 | { |
|
|
203 | W w; |
|
|
204 | int events; |
|
|
205 | } ANPENDING; |
|
|
206 | |
|
|
207 | static ANPENDING *pendings [NUMPRI]; |
|
|
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
209 | |
|
|
210 | static void |
222 | static void |
211 | event (W w, int events) |
223 | event (EV_P_ W w, int events) |
212 | { |
224 | { |
213 | if (w->pending) |
225 | if (w->pending) |
214 | { |
226 | { |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
227 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
216 | return; |
228 | return; |
… | |
… | |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
233 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
234 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
223 | } |
235 | } |
224 | |
236 | |
225 | static void |
237 | static void |
226 | queue_events (W *events, int eventcnt, int type) |
238 | queue_events (EV_P_ W *events, int eventcnt, int type) |
227 | { |
239 | { |
228 | int i; |
240 | int i; |
229 | |
241 | |
230 | for (i = 0; i < eventcnt; ++i) |
242 | for (i = 0; i < eventcnt; ++i) |
231 | event (events [i], type); |
243 | event (EV_A_ events [i], type); |
232 | } |
244 | } |
233 | |
245 | |
234 | static void |
246 | static void |
235 | fd_event (int fd, int events) |
247 | fd_event (EV_P_ int fd, int events) |
236 | { |
248 | { |
237 | ANFD *anfd = anfds + fd; |
249 | ANFD *anfd = anfds + fd; |
238 | struct ev_io *w; |
250 | struct ev_io *w; |
239 | |
251 | |
240 | for (w = anfd->head; w; w = w->next) |
252 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
241 | { |
253 | { |
242 | int ev = w->events & events; |
254 | int ev = w->events & events; |
243 | |
255 | |
244 | if (ev) |
256 | if (ev) |
245 | event ((W)w, ev); |
257 | event (EV_A_ (W)w, ev); |
246 | } |
258 | } |
247 | } |
259 | } |
248 | |
260 | |
249 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
250 | |
262 | |
251 | static int *fdchanges; |
|
|
252 | static int fdchangemax, fdchangecnt; |
|
|
253 | |
|
|
254 | static void |
263 | static void |
255 | fd_reify (void) |
264 | fd_reify (EV_P) |
256 | { |
265 | { |
257 | int i; |
266 | int i; |
258 | |
267 | |
259 | for (i = 0; i < fdchangecnt; ++i) |
268 | for (i = 0; i < fdchangecnt; ++i) |
260 | { |
269 | { |
… | |
… | |
262 | ANFD *anfd = anfds + fd; |
271 | ANFD *anfd = anfds + fd; |
263 | struct ev_io *w; |
272 | struct ev_io *w; |
264 | |
273 | |
265 | int events = 0; |
274 | int events = 0; |
266 | |
275 | |
267 | for (w = anfd->head; w; w = w->next) |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
268 | events |= w->events; |
277 | events |= w->events; |
269 | |
278 | |
270 | anfd->reify = 0; |
279 | anfd->reify = 0; |
271 | |
280 | |
272 | if (anfd->events != events) |
281 | if (anfd->events != events) |
273 | { |
282 | { |
274 | method_modify (fd, anfd->events, events); |
283 | method_modify (EV_A_ fd, anfd->events, events); |
275 | anfd->events = events; |
284 | anfd->events = events; |
276 | } |
285 | } |
277 | } |
286 | } |
278 | |
287 | |
279 | fdchangecnt = 0; |
288 | fdchangecnt = 0; |
280 | } |
289 | } |
281 | |
290 | |
282 | static void |
291 | static void |
283 | fd_change (int fd) |
292 | fd_change (EV_P_ int fd) |
284 | { |
293 | { |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
294 | if (anfds [fd].reify || fdchangecnt < 0) |
286 | return; |
295 | return; |
287 | |
296 | |
288 | anfds [fd].reify = 1; |
297 | anfds [fd].reify = 1; |
… | |
… | |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
300 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
292 | fdchanges [fdchangecnt - 1] = fd; |
301 | fdchanges [fdchangecnt - 1] = fd; |
293 | } |
302 | } |
294 | |
303 | |
295 | static void |
304 | static void |
296 | fd_kill (int fd) |
305 | fd_kill (EV_P_ int fd) |
297 | { |
306 | { |
298 | struct ev_io *w; |
307 | struct ev_io *w; |
299 | |
308 | |
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
309 | while ((w = (struct ev_io *)anfds [fd].head)) |
302 | { |
310 | { |
303 | ev_io_stop (w); |
311 | ev_io_stop (EV_A_ w); |
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
312 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
305 | } |
313 | } |
306 | } |
314 | } |
307 | |
315 | |
308 | /* called on EBADF to verify fds */ |
316 | /* called on EBADF to verify fds */ |
309 | static void |
317 | static void |
310 | fd_ebadf (void) |
318 | fd_ebadf (EV_P) |
311 | { |
319 | { |
312 | int fd; |
320 | int fd; |
313 | |
321 | |
314 | for (fd = 0; fd < anfdmax; ++fd) |
322 | for (fd = 0; fd < anfdmax; ++fd) |
315 | if (anfds [fd].events) |
323 | if (anfds [fd].events) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
324 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
317 | fd_kill (fd); |
325 | fd_kill (EV_A_ fd); |
318 | } |
326 | } |
319 | |
327 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
328 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
329 | static void |
322 | fd_enomem (void) |
330 | fd_enomem (EV_P) |
323 | { |
331 | { |
324 | int fd = anfdmax; |
332 | int fd = anfdmax; |
325 | |
333 | |
326 | while (fd--) |
334 | while (fd--) |
327 | if (anfds [fd].events) |
335 | if (anfds [fd].events) |
328 | { |
336 | { |
329 | close (fd); |
337 | close (fd); |
330 | fd_kill (fd); |
338 | fd_kill (EV_A_ fd); |
331 | return; |
339 | return; |
332 | } |
340 | } |
333 | } |
341 | } |
334 | |
342 | |
335 | /*****************************************************************************/ |
343 | /*****************************************************************************/ |
336 | |
344 | |
337 | static struct ev_timer **timers; |
|
|
338 | static int timermax, timercnt; |
|
|
339 | |
|
|
340 | static struct ev_periodic **periodics; |
|
|
341 | static int periodicmax, periodiccnt; |
|
|
342 | |
|
|
343 | static void |
345 | static void |
344 | upheap (WT *timers, int k) |
346 | upheap (WT *heap, int k) |
345 | { |
347 | { |
346 | WT w = timers [k]; |
348 | WT w = heap [k]; |
347 | |
349 | |
348 | while (k && timers [k >> 1]->at > w->at) |
350 | while (k && heap [k >> 1]->at > w->at) |
349 | { |
351 | { |
350 | timers [k] = timers [k >> 1]; |
352 | heap [k] = heap [k >> 1]; |
351 | timers [k]->active = k + 1; |
353 | heap [k]->active = k + 1; |
352 | k >>= 1; |
354 | k >>= 1; |
353 | } |
355 | } |
354 | |
356 | |
355 | timers [k] = w; |
357 | heap [k] = w; |
356 | timers [k]->active = k + 1; |
358 | heap [k]->active = k + 1; |
357 | |
359 | |
358 | } |
360 | } |
359 | |
361 | |
360 | static void |
362 | static void |
361 | downheap (WT *timers, int N, int k) |
363 | downheap (WT *heap, int N, int k) |
362 | { |
364 | { |
363 | WT w = timers [k]; |
365 | WT w = heap [k]; |
364 | |
366 | |
365 | while (k < (N >> 1)) |
367 | while (k < (N >> 1)) |
366 | { |
368 | { |
367 | int j = k << 1; |
369 | int j = k << 1; |
368 | |
370 | |
369 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
371 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
370 | ++j; |
372 | ++j; |
371 | |
373 | |
372 | if (w->at <= timers [j]->at) |
374 | if (w->at <= heap [j]->at) |
373 | break; |
375 | break; |
374 | |
376 | |
375 | timers [k] = timers [j]; |
377 | heap [k] = heap [j]; |
376 | timers [k]->active = k + 1; |
378 | heap [k]->active = k + 1; |
377 | k = j; |
379 | k = j; |
378 | } |
380 | } |
379 | |
381 | |
380 | timers [k] = w; |
382 | heap [k] = w; |
381 | timers [k]->active = k + 1; |
383 | heap [k]->active = k + 1; |
382 | } |
384 | } |
383 | |
385 | |
384 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
385 | |
387 | |
386 | typedef struct |
388 | typedef struct |
387 | { |
389 | { |
388 | struct ev_signal *head; |
390 | struct ev_watcher_list *head; |
389 | sig_atomic_t volatile gotsig; |
391 | sig_atomic_t volatile gotsig; |
390 | } ANSIG; |
392 | } ANSIG; |
391 | |
393 | |
392 | static ANSIG *signals; |
394 | static ANSIG *signals; |
393 | static int signalmax; |
395 | static int signalmax; |
394 | |
396 | |
395 | static int sigpipe [2]; |
397 | static int sigpipe [2]; |
396 | static sig_atomic_t volatile gotsig; |
398 | static sig_atomic_t volatile gotsig; |
397 | static struct ev_io sigev; |
|
|
398 | |
399 | |
399 | static void |
400 | static void |
400 | signals_init (ANSIG *base, int count) |
401 | signals_init (ANSIG *base, int count) |
401 | { |
402 | { |
402 | while (count--) |
403 | while (count--) |
… | |
… | |
413 | { |
414 | { |
414 | signals [signum - 1].gotsig = 1; |
415 | signals [signum - 1].gotsig = 1; |
415 | |
416 | |
416 | if (!gotsig) |
417 | if (!gotsig) |
417 | { |
418 | { |
|
|
419 | int old_errno = errno; |
418 | gotsig = 1; |
420 | gotsig = 1; |
419 | write (sigpipe [1], &signum, 1); |
421 | write (sigpipe [1], &signum, 1); |
|
|
422 | errno = old_errno; |
420 | } |
423 | } |
421 | } |
424 | } |
422 | |
425 | |
423 | static void |
426 | static void |
424 | sigcb (struct ev_io *iow, int revents) |
427 | sigcb (EV_P_ struct ev_io *iow, int revents) |
425 | { |
428 | { |
426 | struct ev_signal *w; |
429 | struct ev_watcher_list *w; |
427 | int signum; |
430 | int signum; |
428 | |
431 | |
429 | read (sigpipe [0], &revents, 1); |
432 | read (sigpipe [0], &revents, 1); |
430 | gotsig = 0; |
433 | gotsig = 0; |
431 | |
434 | |
… | |
… | |
433 | if (signals [signum].gotsig) |
436 | if (signals [signum].gotsig) |
434 | { |
437 | { |
435 | signals [signum].gotsig = 0; |
438 | signals [signum].gotsig = 0; |
436 | |
439 | |
437 | for (w = signals [signum].head; w; w = w->next) |
440 | for (w = signals [signum].head; w; w = w->next) |
438 | event ((W)w, EV_SIGNAL); |
441 | event (EV_A_ (W)w, EV_SIGNAL); |
439 | } |
442 | } |
440 | } |
443 | } |
441 | |
444 | |
442 | static void |
445 | static void |
443 | siginit (void) |
446 | siginit (EV_P) |
444 | { |
447 | { |
445 | #ifndef WIN32 |
448 | #ifndef WIN32 |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
450 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
448 | |
451 | |
… | |
… | |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
454 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
452 | #endif |
455 | #endif |
453 | |
456 | |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
457 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
455 | ev_io_start (&sigev); |
458 | ev_io_start (EV_A_ &sigev); |
|
|
459 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
456 | } |
460 | } |
457 | |
461 | |
458 | /*****************************************************************************/ |
462 | /*****************************************************************************/ |
459 | |
|
|
460 | static struct ev_idle **idles; |
|
|
461 | static int idlemax, idlecnt; |
|
|
462 | |
|
|
463 | static struct ev_prepare **prepares; |
|
|
464 | static int preparemax, preparecnt; |
|
|
465 | |
|
|
466 | static struct ev_check **checks; |
|
|
467 | static int checkmax, checkcnt; |
|
|
468 | |
|
|
469 | /*****************************************************************************/ |
|
|
470 | |
|
|
471 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
472 | static struct ev_signal childev; |
|
|
473 | |
463 | |
474 | #ifndef WIN32 |
464 | #ifndef WIN32 |
475 | |
465 | |
476 | #ifndef WCONTINUED |
466 | #ifndef WCONTINUED |
477 | # define WCONTINUED 0 |
467 | # define WCONTINUED 0 |
478 | #endif |
468 | #endif |
479 | |
469 | |
480 | static void |
470 | static void |
481 | childcb (struct ev_signal *sw, int revents) |
471 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
482 | { |
472 | { |
483 | struct ev_child *w; |
473 | struct ev_child *w; |
|
|
474 | |
|
|
475 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
|
|
476 | if (w->pid == pid || !w->pid) |
|
|
477 | { |
|
|
478 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
479 | w->rpid = pid; |
|
|
480 | w->rstatus = status; |
|
|
481 | event (EV_A_ (W)w, EV_CHILD); |
|
|
482 | } |
|
|
483 | } |
|
|
484 | |
|
|
485 | static void |
|
|
486 | childcb (EV_P_ struct ev_signal *sw, int revents) |
|
|
487 | { |
484 | int pid, status; |
488 | int pid, status; |
485 | |
489 | |
486 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
490 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
487 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
491 | { |
488 | if (w->pid == pid || !w->pid) |
492 | /* make sure we are called again until all childs have been reaped */ |
489 | { |
493 | event (EV_A_ (W)sw, EV_SIGNAL); |
490 | w->status = status; |
494 | |
491 | event ((W)w, EV_CHILD); |
495 | child_reap (EV_A_ sw, pid, pid, status); |
492 | } |
496 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
497 | } |
493 | } |
498 | } |
494 | |
499 | |
495 | #endif |
500 | #endif |
496 | |
501 | |
497 | /*****************************************************************************/ |
502 | /*****************************************************************************/ |
… | |
… | |
500 | # include "ev_kqueue.c" |
505 | # include "ev_kqueue.c" |
501 | #endif |
506 | #endif |
502 | #if EV_USE_EPOLL |
507 | #if EV_USE_EPOLL |
503 | # include "ev_epoll.c" |
508 | # include "ev_epoll.c" |
504 | #endif |
509 | #endif |
505 | #if EV_USE_POLL |
510 | #if EV_USEV_POLL |
506 | # include "ev_poll.c" |
511 | # include "ev_poll.c" |
507 | #endif |
512 | #endif |
508 | #if EV_USE_SELECT |
513 | #if EV_USE_SELECT |
509 | # include "ev_select.c" |
514 | # include "ev_select.c" |
510 | #endif |
515 | #endif |
… | |
… | |
519 | ev_version_minor (void) |
524 | ev_version_minor (void) |
520 | { |
525 | { |
521 | return EV_VERSION_MINOR; |
526 | return EV_VERSION_MINOR; |
522 | } |
527 | } |
523 | |
528 | |
524 | /* return true if we are running with elevated privileges and ignore env variables */ |
529 | /* return true if we are running with elevated privileges and should ignore env variables */ |
525 | static int |
530 | static int |
526 | enable_secure () |
531 | enable_secure (void) |
527 | { |
532 | { |
|
|
533 | #ifdef WIN32 |
|
|
534 | return 0; |
|
|
535 | #else |
528 | return getuid () != geteuid () |
536 | return getuid () != geteuid () |
529 | || getgid () != getegid (); |
537 | || getgid () != getegid (); |
|
|
538 | #endif |
530 | } |
539 | } |
531 | |
540 | |
532 | int ev_init (int methods) |
541 | int |
|
|
542 | ev_method (EV_P) |
533 | { |
543 | { |
|
|
544 | return method; |
|
|
545 | } |
|
|
546 | |
|
|
547 | inline int |
|
|
548 | loop_init (EV_P_ int methods) |
|
|
549 | { |
534 | if (!ev_method) |
550 | if (!method) |
535 | { |
551 | { |
536 | #if EV_USE_MONOTONIC |
552 | #if EV_USE_MONOTONIC |
537 | { |
553 | { |
538 | struct timespec ts; |
554 | struct timespec ts; |
539 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
555 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
540 | have_monotonic = 1; |
556 | have_monotonic = 1; |
541 | } |
557 | } |
542 | #endif |
558 | #endif |
543 | |
559 | |
544 | ev_now = ev_time (); |
560 | rt_now = ev_time (); |
545 | now = get_clock (); |
561 | mn_now = get_clock (); |
546 | now_floor = now; |
562 | now_floor = mn_now; |
547 | diff = ev_now - now; |
563 | rtmn_diff = rt_now - mn_now; |
548 | |
564 | |
549 | if (pipe (sigpipe)) |
565 | if (pipe (sigpipe)) |
550 | return 0; |
566 | return 0; |
551 | |
567 | |
552 | if (methods == EVMETHOD_AUTO) |
568 | if (methods == EVMETHOD_AUTO) |
553 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
569 | if (!enable_secure () && getenv ("LIBmethodS")) |
554 | methods = atoi (getenv ("LIBEV_METHODS")); |
570 | methods = atoi (getenv ("LIBmethodS")); |
555 | else |
571 | else |
556 | methods = EVMETHOD_ANY; |
572 | methods = EVMETHOD_ANY; |
557 | |
573 | |
558 | ev_method = 0; |
574 | method = 0; |
559 | #if EV_USE_KQUEUE |
575 | #if EV_USE_KQUEUE |
560 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
576 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
561 | #endif |
577 | #endif |
562 | #if EV_USE_EPOLL |
578 | #if EV_USE_EPOLL |
563 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
579 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
564 | #endif |
580 | #endif |
565 | #if EV_USE_POLL |
581 | #if EV_USEV_POLL |
566 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
582 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
567 | #endif |
583 | #endif |
568 | #if EV_USE_SELECT |
584 | #if EV_USE_SELECT |
569 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
585 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
570 | #endif |
586 | #endif |
571 | |
587 | |
572 | if (ev_method) |
588 | if (method) |
573 | { |
589 | { |
574 | ev_watcher_init (&sigev, sigcb); |
590 | ev_watcher_init (&sigev, sigcb); |
|
|
591 | ev_set_priority (&sigev, EV_MAXPRI); |
575 | siginit (); |
592 | siginit (EV_A); |
576 | |
593 | |
577 | #ifndef WIN32 |
594 | #ifndef WIN32 |
578 | ev_signal_init (&childev, childcb, SIGCHLD); |
595 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
596 | ev_set_priority (&childev, EV_MAXPRI); |
579 | ev_signal_start (&childev); |
597 | ev_signal_start (EV_A_ &childev); |
|
|
598 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
580 | #endif |
599 | #endif |
581 | } |
600 | } |
582 | } |
601 | } |
583 | |
602 | |
584 | return ev_method; |
603 | return method; |
585 | } |
604 | } |
|
|
605 | |
|
|
606 | #if EV_MULTIPLICITY |
|
|
607 | |
|
|
608 | struct ev_loop * |
|
|
609 | ev_loop_new (int methods) |
|
|
610 | { |
|
|
611 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
612 | |
|
|
613 | if (loop_init (EV_A_ methods)) |
|
|
614 | return loop; |
|
|
615 | |
|
|
616 | ev_loop_delete (loop); |
|
|
617 | |
|
|
618 | return 0; |
|
|
619 | } |
|
|
620 | |
|
|
621 | void |
|
|
622 | ev_loop_delete (EV_P) |
|
|
623 | { |
|
|
624 | /*TODO*/ |
|
|
625 | free (loop); |
|
|
626 | } |
|
|
627 | |
|
|
628 | #else |
|
|
629 | |
|
|
630 | int |
|
|
631 | ev_init (int methods) |
|
|
632 | { |
|
|
633 | return loop_init (methods); |
|
|
634 | } |
|
|
635 | |
|
|
636 | #endif |
586 | |
637 | |
587 | /*****************************************************************************/ |
638 | /*****************************************************************************/ |
588 | |
639 | |
589 | void |
640 | void |
590 | ev_fork_prepare (void) |
641 | ev_fork_prepare (void) |
… | |
… | |
599 | } |
650 | } |
600 | |
651 | |
601 | void |
652 | void |
602 | ev_fork_child (void) |
653 | ev_fork_child (void) |
603 | { |
654 | { |
|
|
655 | /*TODO*/ |
|
|
656 | #if !EV_MULTIPLICITY |
604 | #if EV_USE_EPOLL |
657 | #if EV_USE_EPOLL |
605 | if (ev_method == EVMETHOD_EPOLL) |
658 | if (method == EVMETHOD_EPOLL) |
606 | epoll_postfork_child (); |
659 | epoll_postfork_child (EV_A); |
607 | #endif |
660 | #endif |
608 | |
661 | |
609 | ev_io_stop (&sigev); |
662 | ev_io_stop (EV_A_ &sigev); |
610 | close (sigpipe [0]); |
663 | close (sigpipe [0]); |
611 | close (sigpipe [1]); |
664 | close (sigpipe [1]); |
612 | pipe (sigpipe); |
665 | pipe (sigpipe); |
613 | siginit (); |
666 | siginit (EV_A); |
|
|
667 | #endif |
614 | } |
668 | } |
615 | |
669 | |
616 | /*****************************************************************************/ |
670 | /*****************************************************************************/ |
617 | |
671 | |
618 | static void |
672 | static void |
619 | call_pending (void) |
673 | call_pending (EV_P) |
620 | { |
674 | { |
621 | int pri; |
675 | int pri; |
622 | |
676 | |
623 | for (pri = NUMPRI; pri--; ) |
677 | for (pri = NUMPRI; pri--; ) |
624 | while (pendingcnt [pri]) |
678 | while (pendingcnt [pri]) |
… | |
… | |
626 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
680 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
627 | |
681 | |
628 | if (p->w) |
682 | if (p->w) |
629 | { |
683 | { |
630 | p->w->pending = 0; |
684 | p->w->pending = 0; |
631 | p->w->cb (p->w, p->events); |
685 | p->w->cb (EV_A_ p->w, p->events); |
632 | } |
686 | } |
633 | } |
687 | } |
634 | } |
688 | } |
635 | |
689 | |
636 | static void |
690 | static void |
637 | timers_reify (void) |
691 | timers_reify (EV_P) |
638 | { |
692 | { |
639 | while (timercnt && timers [0]->at <= now) |
693 | while (timercnt && timers [0]->at <= mn_now) |
640 | { |
694 | { |
641 | struct ev_timer *w = timers [0]; |
695 | struct ev_timer *w = timers [0]; |
642 | |
696 | |
643 | /* first reschedule or stop timer */ |
697 | /* first reschedule or stop timer */ |
644 | if (w->repeat) |
698 | if (w->repeat) |
645 | { |
699 | { |
646 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
700 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
647 | w->at = now + w->repeat; |
701 | w->at = mn_now + w->repeat; |
648 | downheap ((WT *)timers, timercnt, 0); |
702 | downheap ((WT *)timers, timercnt, 0); |
649 | } |
703 | } |
650 | else |
704 | else |
651 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
705 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
652 | |
706 | |
653 | event ((W)w, EV_TIMEOUT); |
707 | event (EV_A_ (W)w, EV_TIMEOUT); |
654 | } |
708 | } |
655 | } |
709 | } |
656 | |
710 | |
657 | static void |
711 | static void |
658 | periodics_reify (void) |
712 | periodics_reify (EV_P) |
659 | { |
713 | { |
660 | while (periodiccnt && periodics [0]->at <= ev_now) |
714 | while (periodiccnt && periodics [0]->at <= rt_now) |
661 | { |
715 | { |
662 | struct ev_periodic *w = periodics [0]; |
716 | struct ev_periodic *w = periodics [0]; |
663 | |
717 | |
664 | /* first reschedule or stop timer */ |
718 | /* first reschedule or stop timer */ |
665 | if (w->interval) |
719 | if (w->interval) |
666 | { |
720 | { |
667 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
721 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
668 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
722 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
669 | downheap ((WT *)periodics, periodiccnt, 0); |
723 | downheap ((WT *)periodics, periodiccnt, 0); |
670 | } |
724 | } |
671 | else |
725 | else |
672 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
726 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
673 | |
727 | |
674 | event ((W)w, EV_PERIODIC); |
728 | event (EV_A_ (W)w, EV_PERIODIC); |
675 | } |
729 | } |
676 | } |
730 | } |
677 | |
731 | |
678 | static void |
732 | static void |
679 | periodics_reschedule (ev_tstamp diff) |
733 | periodics_reschedule (EV_P) |
680 | { |
734 | { |
681 | int i; |
735 | int i; |
682 | |
736 | |
683 | /* adjust periodics after time jump */ |
737 | /* adjust periodics after time jump */ |
684 | for (i = 0; i < periodiccnt; ++i) |
738 | for (i = 0; i < periodiccnt; ++i) |
685 | { |
739 | { |
686 | struct ev_periodic *w = periodics [i]; |
740 | struct ev_periodic *w = periodics [i]; |
687 | |
741 | |
688 | if (w->interval) |
742 | if (w->interval) |
689 | { |
743 | { |
690 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
744 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
691 | |
745 | |
692 | if (fabs (diff) >= 1e-4) |
746 | if (fabs (diff) >= 1e-4) |
693 | { |
747 | { |
694 | ev_periodic_stop (w); |
748 | ev_periodic_stop (EV_A_ w); |
695 | ev_periodic_start (w); |
749 | ev_periodic_start (EV_A_ w); |
696 | |
750 | |
697 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
751 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
698 | } |
752 | } |
699 | } |
753 | } |
700 | } |
754 | } |
701 | } |
755 | } |
702 | |
756 | |
703 | static int |
757 | inline int |
704 | time_update_monotonic (void) |
758 | time_update_monotonic (EV_P) |
705 | { |
759 | { |
706 | now = get_clock (); |
760 | mn_now = get_clock (); |
707 | |
761 | |
708 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
762 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
709 | { |
763 | { |
710 | ev_now = now + diff; |
764 | rt_now = rtmn_diff + mn_now; |
711 | return 0; |
765 | return 0; |
712 | } |
766 | } |
713 | else |
767 | else |
714 | { |
768 | { |
715 | now_floor = now; |
769 | now_floor = mn_now; |
716 | ev_now = ev_time (); |
770 | rt_now = ev_time (); |
717 | return 1; |
771 | return 1; |
718 | } |
772 | } |
719 | } |
773 | } |
720 | |
774 | |
721 | static void |
775 | static void |
722 | time_update (void) |
776 | time_update (EV_P) |
723 | { |
777 | { |
724 | int i; |
778 | int i; |
725 | |
779 | |
726 | #if EV_USE_MONOTONIC |
780 | #if EV_USE_MONOTONIC |
727 | if (expect_true (have_monotonic)) |
781 | if (expect_true (have_monotonic)) |
728 | { |
782 | { |
729 | if (time_update_monotonic ()) |
783 | if (time_update_monotonic (EV_A)) |
730 | { |
784 | { |
731 | ev_tstamp odiff = diff; |
785 | ev_tstamp odiff = rtmn_diff; |
732 | |
786 | |
733 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
787 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
734 | { |
788 | { |
735 | diff = ev_now - now; |
789 | rtmn_diff = rt_now - mn_now; |
736 | |
790 | |
737 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
791 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
738 | return; /* all is well */ |
792 | return; /* all is well */ |
739 | |
793 | |
740 | ev_now = ev_time (); |
794 | rt_now = ev_time (); |
741 | now = get_clock (); |
795 | mn_now = get_clock (); |
742 | now_floor = now; |
796 | now_floor = mn_now; |
743 | } |
797 | } |
744 | |
798 | |
745 | periodics_reschedule (diff - odiff); |
799 | periodics_reschedule (EV_A); |
746 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
800 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
801 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
747 | } |
802 | } |
748 | } |
803 | } |
749 | else |
804 | else |
750 | #endif |
805 | #endif |
751 | { |
806 | { |
752 | ev_now = ev_time (); |
807 | rt_now = ev_time (); |
753 | |
808 | |
754 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
809 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
755 | { |
810 | { |
756 | periodics_reschedule (ev_now - now); |
811 | periodics_reschedule (EV_A); |
757 | |
812 | |
758 | /* adjust timers. this is easy, as the offset is the same for all */ |
813 | /* adjust timers. this is easy, as the offset is the same for all */ |
759 | for (i = 0; i < timercnt; ++i) |
814 | for (i = 0; i < timercnt; ++i) |
760 | timers [i]->at += diff; |
815 | timers [i]->at += rt_now - mn_now; |
761 | } |
816 | } |
762 | |
817 | |
763 | now = ev_now; |
818 | mn_now = rt_now; |
764 | } |
819 | } |
765 | } |
820 | } |
766 | |
821 | |
767 | int ev_loop_done; |
822 | void |
|
|
823 | ev_ref (EV_P) |
|
|
824 | { |
|
|
825 | ++activecnt; |
|
|
826 | } |
768 | |
827 | |
|
|
828 | void |
|
|
829 | ev_unref (EV_P) |
|
|
830 | { |
|
|
831 | --activecnt; |
|
|
832 | } |
|
|
833 | |
|
|
834 | static int loop_done; |
|
|
835 | |
|
|
836 | void |
769 | void ev_loop (int flags) |
837 | ev_loop (EV_P_ int flags) |
770 | { |
838 | { |
771 | double block; |
839 | double block; |
772 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
840 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
773 | |
841 | |
774 | do |
842 | do |
775 | { |
843 | { |
776 | /* queue check watchers (and execute them) */ |
844 | /* queue check watchers (and execute them) */ |
777 | if (expect_false (preparecnt)) |
845 | if (expect_false (preparecnt)) |
778 | { |
846 | { |
779 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
847 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
780 | call_pending (); |
848 | call_pending (EV_A); |
781 | } |
849 | } |
782 | |
850 | |
783 | /* update fd-related kernel structures */ |
851 | /* update fd-related kernel structures */ |
784 | fd_reify (); |
852 | fd_reify (EV_A); |
785 | |
853 | |
786 | /* calculate blocking time */ |
854 | /* calculate blocking time */ |
787 | |
855 | |
788 | /* we only need this for !monotonic clockor timers, but as we basically |
856 | /* we only need this for !monotonic clockor timers, but as we basically |
789 | always have timers, we just calculate it always */ |
857 | always have timers, we just calculate it always */ |
790 | #if EV_USE_MONOTONIC |
858 | #if EV_USE_MONOTONIC |
791 | if (expect_true (have_monotonic)) |
859 | if (expect_true (have_monotonic)) |
792 | time_update_monotonic (); |
860 | time_update_monotonic (EV_A); |
793 | else |
861 | else |
794 | #endif |
862 | #endif |
795 | { |
863 | { |
796 | ev_now = ev_time (); |
864 | rt_now = ev_time (); |
797 | now = ev_now; |
865 | mn_now = rt_now; |
798 | } |
866 | } |
799 | |
867 | |
800 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
868 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
801 | block = 0.; |
869 | block = 0.; |
802 | else |
870 | else |
803 | { |
871 | { |
804 | block = MAX_BLOCKTIME; |
872 | block = MAX_BLOCKTIME; |
805 | |
873 | |
806 | if (timercnt) |
874 | if (timercnt) |
807 | { |
875 | { |
808 | ev_tstamp to = timers [0]->at - now + method_fudge; |
876 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
809 | if (block > to) block = to; |
877 | if (block > to) block = to; |
810 | } |
878 | } |
811 | |
879 | |
812 | if (periodiccnt) |
880 | if (periodiccnt) |
813 | { |
881 | { |
814 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
882 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
815 | if (block > to) block = to; |
883 | if (block > to) block = to; |
816 | } |
884 | } |
817 | |
885 | |
818 | if (block < 0.) block = 0.; |
886 | if (block < 0.) block = 0.; |
819 | } |
887 | } |
820 | |
888 | |
821 | method_poll (block); |
889 | method_poll (EV_A_ block); |
822 | |
890 | |
823 | /* update ev_now, do magic */ |
891 | /* update rt_now, do magic */ |
824 | time_update (); |
892 | time_update (EV_A); |
825 | |
893 | |
826 | /* queue pending timers and reschedule them */ |
894 | /* queue pending timers and reschedule them */ |
827 | timers_reify (); /* relative timers called last */ |
895 | timers_reify (EV_A); /* relative timers called last */ |
828 | periodics_reify (); /* absolute timers called first */ |
896 | periodics_reify (EV_A); /* absolute timers called first */ |
829 | |
897 | |
830 | /* queue idle watchers unless io or timers are pending */ |
898 | /* queue idle watchers unless io or timers are pending */ |
831 | if (!pendingcnt) |
899 | if (!pendingcnt) |
832 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
900 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
833 | |
901 | |
834 | /* queue check watchers, to be executed first */ |
902 | /* queue check watchers, to be executed first */ |
835 | if (checkcnt) |
903 | if (checkcnt) |
836 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
904 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
837 | |
905 | |
838 | call_pending (); |
906 | call_pending (EV_A); |
839 | } |
907 | } |
840 | while (!ev_loop_done); |
908 | while (activecnt && !loop_done); |
841 | |
909 | |
842 | if (ev_loop_done != 2) |
910 | if (loop_done != 2) |
843 | ev_loop_done = 0; |
911 | loop_done = 0; |
|
|
912 | } |
|
|
913 | |
|
|
914 | void |
|
|
915 | ev_unloop (EV_P_ int how) |
|
|
916 | { |
|
|
917 | loop_done = how; |
844 | } |
918 | } |
845 | |
919 | |
846 | /*****************************************************************************/ |
920 | /*****************************************************************************/ |
847 | |
921 | |
848 | static void |
922 | inline void |
849 | wlist_add (WL *head, WL elem) |
923 | wlist_add (WL *head, WL elem) |
850 | { |
924 | { |
851 | elem->next = *head; |
925 | elem->next = *head; |
852 | *head = elem; |
926 | *head = elem; |
853 | } |
927 | } |
854 | |
928 | |
855 | static void |
929 | inline void |
856 | wlist_del (WL *head, WL elem) |
930 | wlist_del (WL *head, WL elem) |
857 | { |
931 | { |
858 | while (*head) |
932 | while (*head) |
859 | { |
933 | { |
860 | if (*head == elem) |
934 | if (*head == elem) |
… | |
… | |
865 | |
939 | |
866 | head = &(*head)->next; |
940 | head = &(*head)->next; |
867 | } |
941 | } |
868 | } |
942 | } |
869 | |
943 | |
870 | static void |
944 | inline void |
871 | ev_clear_pending (W w) |
945 | ev_clear_pending (EV_P_ W w) |
872 | { |
946 | { |
873 | if (w->pending) |
947 | if (w->pending) |
874 | { |
948 | { |
875 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
949 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
876 | w->pending = 0; |
950 | w->pending = 0; |
877 | } |
951 | } |
878 | } |
952 | } |
879 | |
953 | |
880 | static void |
954 | inline void |
881 | ev_start (W w, int active) |
955 | ev_start (EV_P_ W w, int active) |
882 | { |
956 | { |
883 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
957 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
884 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
958 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
885 | |
959 | |
886 | w->active = active; |
960 | w->active = active; |
|
|
961 | ev_ref (EV_A); |
887 | } |
962 | } |
888 | |
963 | |
889 | static void |
964 | inline void |
890 | ev_stop (W w) |
965 | ev_stop (EV_P_ W w) |
891 | { |
966 | { |
|
|
967 | ev_unref (EV_A); |
892 | w->active = 0; |
968 | w->active = 0; |
893 | } |
969 | } |
894 | |
970 | |
895 | /*****************************************************************************/ |
971 | /*****************************************************************************/ |
896 | |
972 | |
897 | void |
973 | void |
898 | ev_io_start (struct ev_io *w) |
974 | ev_io_start (EV_P_ struct ev_io *w) |
899 | { |
975 | { |
900 | int fd = w->fd; |
976 | int fd = w->fd; |
901 | |
977 | |
902 | if (ev_is_active (w)) |
978 | if (ev_is_active (w)) |
903 | return; |
979 | return; |
904 | |
980 | |
905 | assert (("ev_io_start called with negative fd", fd >= 0)); |
981 | assert (("ev_io_start called with negative fd", fd >= 0)); |
906 | |
982 | |
907 | ev_start ((W)w, 1); |
983 | ev_start (EV_A_ (W)w, 1); |
908 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
984 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
909 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
985 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
910 | |
986 | |
911 | fd_change (fd); |
987 | fd_change (EV_A_ fd); |
912 | } |
988 | } |
913 | |
989 | |
914 | void |
990 | void |
915 | ev_io_stop (struct ev_io *w) |
991 | ev_io_stop (EV_P_ struct ev_io *w) |
916 | { |
992 | { |
917 | ev_clear_pending ((W)w); |
993 | ev_clear_pending (EV_A_ (W)w); |
918 | if (!ev_is_active (w)) |
994 | if (!ev_is_active (w)) |
919 | return; |
995 | return; |
920 | |
996 | |
921 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
997 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
922 | ev_stop ((W)w); |
998 | ev_stop (EV_A_ (W)w); |
923 | |
999 | |
924 | fd_change (w->fd); |
1000 | fd_change (EV_A_ w->fd); |
925 | } |
1001 | } |
926 | |
1002 | |
927 | void |
1003 | void |
928 | ev_timer_start (struct ev_timer *w) |
1004 | ev_timer_start (EV_P_ struct ev_timer *w) |
929 | { |
1005 | { |
930 | if (ev_is_active (w)) |
1006 | if (ev_is_active (w)) |
931 | return; |
1007 | return; |
932 | |
1008 | |
933 | w->at += now; |
1009 | w->at += mn_now; |
934 | |
1010 | |
935 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1011 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
936 | |
1012 | |
937 | ev_start ((W)w, ++timercnt); |
1013 | ev_start (EV_A_ (W)w, ++timercnt); |
938 | array_needsize (timers, timermax, timercnt, ); |
1014 | array_needsize (timers, timermax, timercnt, ); |
939 | timers [timercnt - 1] = w; |
1015 | timers [timercnt - 1] = w; |
940 | upheap ((WT *)timers, timercnt - 1); |
1016 | upheap ((WT *)timers, timercnt - 1); |
941 | } |
1017 | } |
942 | |
1018 | |
943 | void |
1019 | void |
944 | ev_timer_stop (struct ev_timer *w) |
1020 | ev_timer_stop (EV_P_ struct ev_timer *w) |
945 | { |
1021 | { |
946 | ev_clear_pending ((W)w); |
1022 | ev_clear_pending (EV_A_ (W)w); |
947 | if (!ev_is_active (w)) |
1023 | if (!ev_is_active (w)) |
948 | return; |
1024 | return; |
949 | |
1025 | |
950 | if (w->active < timercnt--) |
1026 | if (w->active < timercnt--) |
951 | { |
1027 | { |
… | |
… | |
953 | downheap ((WT *)timers, timercnt, w->active - 1); |
1029 | downheap ((WT *)timers, timercnt, w->active - 1); |
954 | } |
1030 | } |
955 | |
1031 | |
956 | w->at = w->repeat; |
1032 | w->at = w->repeat; |
957 | |
1033 | |
958 | ev_stop ((W)w); |
1034 | ev_stop (EV_A_ (W)w); |
959 | } |
1035 | } |
960 | |
1036 | |
961 | void |
1037 | void |
962 | ev_timer_again (struct ev_timer *w) |
1038 | ev_timer_again (EV_P_ struct ev_timer *w) |
963 | { |
1039 | { |
964 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
965 | { |
1041 | { |
966 | if (w->repeat) |
1042 | if (w->repeat) |
967 | { |
1043 | { |
968 | w->at = now + w->repeat; |
1044 | w->at = mn_now + w->repeat; |
969 | downheap ((WT *)timers, timercnt, w->active - 1); |
1045 | downheap ((WT *)timers, timercnt, w->active - 1); |
970 | } |
1046 | } |
971 | else |
1047 | else |
972 | ev_timer_stop (w); |
1048 | ev_timer_stop (EV_A_ w); |
973 | } |
1049 | } |
974 | else if (w->repeat) |
1050 | else if (w->repeat) |
975 | ev_timer_start (w); |
1051 | ev_timer_start (EV_A_ w); |
976 | } |
1052 | } |
977 | |
1053 | |
978 | void |
1054 | void |
979 | ev_periodic_start (struct ev_periodic *w) |
1055 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
980 | { |
1056 | { |
981 | if (ev_is_active (w)) |
1057 | if (ev_is_active (w)) |
982 | return; |
1058 | return; |
983 | |
1059 | |
984 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1060 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
985 | |
1061 | |
986 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1062 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
987 | if (w->interval) |
1063 | if (w->interval) |
988 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1064 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
989 | |
1065 | |
990 | ev_start ((W)w, ++periodiccnt); |
1066 | ev_start (EV_A_ (W)w, ++periodiccnt); |
991 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1067 | array_needsize (periodics, periodicmax, periodiccnt, ); |
992 | periodics [periodiccnt - 1] = w; |
1068 | periodics [periodiccnt - 1] = w; |
993 | upheap ((WT *)periodics, periodiccnt - 1); |
1069 | upheap ((WT *)periodics, periodiccnt - 1); |
994 | } |
1070 | } |
995 | |
1071 | |
996 | void |
1072 | void |
997 | ev_periodic_stop (struct ev_periodic *w) |
1073 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
998 | { |
1074 | { |
999 | ev_clear_pending ((W)w); |
1075 | ev_clear_pending (EV_A_ (W)w); |
1000 | if (!ev_is_active (w)) |
1076 | if (!ev_is_active (w)) |
1001 | return; |
1077 | return; |
1002 | |
1078 | |
1003 | if (w->active < periodiccnt--) |
1079 | if (w->active < periodiccnt--) |
1004 | { |
1080 | { |
1005 | periodics [w->active - 1] = periodics [periodiccnt]; |
1081 | periodics [w->active - 1] = periodics [periodiccnt]; |
1006 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1082 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1007 | } |
1083 | } |
1008 | |
1084 | |
1009 | ev_stop ((W)w); |
1085 | ev_stop (EV_A_ (W)w); |
1010 | } |
1086 | } |
1011 | |
1087 | |
|
|
1088 | #ifndef SA_RESTART |
|
|
1089 | # define SA_RESTART 0 |
|
|
1090 | #endif |
|
|
1091 | |
1012 | void |
1092 | void |
1013 | ev_signal_start (struct ev_signal *w) |
1093 | ev_signal_start (EV_P_ struct ev_signal *w) |
1014 | { |
1094 | { |
1015 | if (ev_is_active (w)) |
1095 | if (ev_is_active (w)) |
1016 | return; |
1096 | return; |
1017 | |
1097 | |
1018 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1098 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1019 | |
1099 | |
1020 | ev_start ((W)w, 1); |
1100 | ev_start (EV_A_ (W)w, 1); |
1021 | array_needsize (signals, signalmax, w->signum, signals_init); |
1101 | array_needsize (signals, signalmax, w->signum, signals_init); |
1022 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1102 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1023 | |
1103 | |
1024 | if (!w->next) |
1104 | if (!w->next) |
1025 | { |
1105 | { |
1026 | struct sigaction sa; |
1106 | struct sigaction sa; |
1027 | sa.sa_handler = sighandler; |
1107 | sa.sa_handler = sighandler; |
1028 | sigfillset (&sa.sa_mask); |
1108 | sigfillset (&sa.sa_mask); |
1029 | sa.sa_flags = 0; |
1109 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1030 | sigaction (w->signum, &sa, 0); |
1110 | sigaction (w->signum, &sa, 0); |
1031 | } |
1111 | } |
1032 | } |
1112 | } |
1033 | |
1113 | |
1034 | void |
1114 | void |
1035 | ev_signal_stop (struct ev_signal *w) |
1115 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1036 | { |
1116 | { |
1037 | ev_clear_pending ((W)w); |
1117 | ev_clear_pending (EV_A_ (W)w); |
1038 | if (!ev_is_active (w)) |
1118 | if (!ev_is_active (w)) |
1039 | return; |
1119 | return; |
1040 | |
1120 | |
1041 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1121 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1042 | ev_stop ((W)w); |
1122 | ev_stop (EV_A_ (W)w); |
1043 | |
1123 | |
1044 | if (!signals [w->signum - 1].head) |
1124 | if (!signals [w->signum - 1].head) |
1045 | signal (w->signum, SIG_DFL); |
1125 | signal (w->signum, SIG_DFL); |
1046 | } |
1126 | } |
1047 | |
1127 | |
1048 | void |
1128 | void |
1049 | ev_idle_start (struct ev_idle *w) |
1129 | ev_idle_start (EV_P_ struct ev_idle *w) |
1050 | { |
1130 | { |
1051 | if (ev_is_active (w)) |
1131 | if (ev_is_active (w)) |
1052 | return; |
1132 | return; |
1053 | |
1133 | |
1054 | ev_start ((W)w, ++idlecnt); |
1134 | ev_start (EV_A_ (W)w, ++idlecnt); |
1055 | array_needsize (idles, idlemax, idlecnt, ); |
1135 | array_needsize (idles, idlemax, idlecnt, ); |
1056 | idles [idlecnt - 1] = w; |
1136 | idles [idlecnt - 1] = w; |
1057 | } |
1137 | } |
1058 | |
1138 | |
1059 | void |
1139 | void |
1060 | ev_idle_stop (struct ev_idle *w) |
1140 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1061 | { |
1141 | { |
1062 | ev_clear_pending ((W)w); |
1142 | ev_clear_pending (EV_A_ (W)w); |
1063 | if (ev_is_active (w)) |
1143 | if (ev_is_active (w)) |
1064 | return; |
1144 | return; |
1065 | |
1145 | |
1066 | idles [w->active - 1] = idles [--idlecnt]; |
1146 | idles [w->active - 1] = idles [--idlecnt]; |
1067 | ev_stop ((W)w); |
1147 | ev_stop (EV_A_ (W)w); |
1068 | } |
1148 | } |
1069 | |
1149 | |
1070 | void |
1150 | void |
1071 | ev_prepare_start (struct ev_prepare *w) |
1151 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1072 | { |
1152 | { |
1073 | if (ev_is_active (w)) |
1153 | if (ev_is_active (w)) |
1074 | return; |
1154 | return; |
1075 | |
1155 | |
1076 | ev_start ((W)w, ++preparecnt); |
1156 | ev_start (EV_A_ (W)w, ++preparecnt); |
1077 | array_needsize (prepares, preparemax, preparecnt, ); |
1157 | array_needsize (prepares, preparemax, preparecnt, ); |
1078 | prepares [preparecnt - 1] = w; |
1158 | prepares [preparecnt - 1] = w; |
1079 | } |
1159 | } |
1080 | |
1160 | |
1081 | void |
1161 | void |
1082 | ev_prepare_stop (struct ev_prepare *w) |
1162 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1083 | { |
1163 | { |
1084 | ev_clear_pending ((W)w); |
1164 | ev_clear_pending (EV_A_ (W)w); |
1085 | if (ev_is_active (w)) |
1165 | if (ev_is_active (w)) |
1086 | return; |
1166 | return; |
1087 | |
1167 | |
1088 | prepares [w->active - 1] = prepares [--preparecnt]; |
1168 | prepares [w->active - 1] = prepares [--preparecnt]; |
1089 | ev_stop ((W)w); |
1169 | ev_stop (EV_A_ (W)w); |
1090 | } |
1170 | } |
1091 | |
1171 | |
1092 | void |
1172 | void |
1093 | ev_check_start (struct ev_check *w) |
1173 | ev_check_start (EV_P_ struct ev_check *w) |
1094 | { |
1174 | { |
1095 | if (ev_is_active (w)) |
1175 | if (ev_is_active (w)) |
1096 | return; |
1176 | return; |
1097 | |
1177 | |
1098 | ev_start ((W)w, ++checkcnt); |
1178 | ev_start (EV_A_ (W)w, ++checkcnt); |
1099 | array_needsize (checks, checkmax, checkcnt, ); |
1179 | array_needsize (checks, checkmax, checkcnt, ); |
1100 | checks [checkcnt - 1] = w; |
1180 | checks [checkcnt - 1] = w; |
1101 | } |
1181 | } |
1102 | |
1182 | |
1103 | void |
1183 | void |
1104 | ev_check_stop (struct ev_check *w) |
1184 | ev_check_stop (EV_P_ struct ev_check *w) |
1105 | { |
1185 | { |
1106 | ev_clear_pending ((W)w); |
1186 | ev_clear_pending (EV_A_ (W)w); |
1107 | if (ev_is_active (w)) |
1187 | if (ev_is_active (w)) |
1108 | return; |
1188 | return; |
1109 | |
1189 | |
1110 | checks [w->active - 1] = checks [--checkcnt]; |
1190 | checks [w->active - 1] = checks [--checkcnt]; |
1111 | ev_stop ((W)w); |
1191 | ev_stop (EV_A_ (W)w); |
1112 | } |
1192 | } |
1113 | |
1193 | |
1114 | void |
1194 | void |
1115 | ev_child_start (struct ev_child *w) |
1195 | ev_child_start (EV_P_ struct ev_child *w) |
1116 | { |
1196 | { |
1117 | if (ev_is_active (w)) |
1197 | if (ev_is_active (w)) |
1118 | return; |
1198 | return; |
1119 | |
1199 | |
1120 | ev_start ((W)w, 1); |
1200 | ev_start (EV_A_ (W)w, 1); |
1121 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1201 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1122 | } |
1202 | } |
1123 | |
1203 | |
1124 | void |
1204 | void |
1125 | ev_child_stop (struct ev_child *w) |
1205 | ev_child_stop (EV_P_ struct ev_child *w) |
1126 | { |
1206 | { |
1127 | ev_clear_pending ((W)w); |
1207 | ev_clear_pending (EV_A_ (W)w); |
1128 | if (ev_is_active (w)) |
1208 | if (ev_is_active (w)) |
1129 | return; |
1209 | return; |
1130 | |
1210 | |
1131 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1211 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1132 | ev_stop ((W)w); |
1212 | ev_stop (EV_A_ (W)w); |
1133 | } |
1213 | } |
1134 | |
1214 | |
1135 | /*****************************************************************************/ |
1215 | /*****************************************************************************/ |
1136 | |
1216 | |
1137 | struct ev_once |
1217 | struct ev_once |
… | |
… | |
1141 | void (*cb)(int revents, void *arg); |
1221 | void (*cb)(int revents, void *arg); |
1142 | void *arg; |
1222 | void *arg; |
1143 | }; |
1223 | }; |
1144 | |
1224 | |
1145 | static void |
1225 | static void |
1146 | once_cb (struct ev_once *once, int revents) |
1226 | once_cb (EV_P_ struct ev_once *once, int revents) |
1147 | { |
1227 | { |
1148 | void (*cb)(int revents, void *arg) = once->cb; |
1228 | void (*cb)(int revents, void *arg) = once->cb; |
1149 | void *arg = once->arg; |
1229 | void *arg = once->arg; |
1150 | |
1230 | |
1151 | ev_io_stop (&once->io); |
1231 | ev_io_stop (EV_A_ &once->io); |
1152 | ev_timer_stop (&once->to); |
1232 | ev_timer_stop (EV_A_ &once->to); |
1153 | free (once); |
1233 | free (once); |
1154 | |
1234 | |
1155 | cb (revents, arg); |
1235 | cb (revents, arg); |
1156 | } |
1236 | } |
1157 | |
1237 | |
1158 | static void |
1238 | static void |
1159 | once_cb_io (struct ev_io *w, int revents) |
1239 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1160 | { |
1240 | { |
1161 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1241 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1162 | } |
1242 | } |
1163 | |
1243 | |
1164 | static void |
1244 | static void |
1165 | once_cb_to (struct ev_timer *w, int revents) |
1245 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1166 | { |
1246 | { |
1167 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1247 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1168 | } |
1248 | } |
1169 | |
1249 | |
1170 | void |
1250 | void |
1171 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1251 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1172 | { |
1252 | { |
1173 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1253 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1174 | |
1254 | |
1175 | if (!once) |
1255 | if (!once) |
1176 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1256 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1181 | |
1261 | |
1182 | ev_watcher_init (&once->io, once_cb_io); |
1262 | ev_watcher_init (&once->io, once_cb_io); |
1183 | if (fd >= 0) |
1263 | if (fd >= 0) |
1184 | { |
1264 | { |
1185 | ev_io_set (&once->io, fd, events); |
1265 | ev_io_set (&once->io, fd, events); |
1186 | ev_io_start (&once->io); |
1266 | ev_io_start (EV_A_ &once->io); |
1187 | } |
1267 | } |
1188 | |
1268 | |
1189 | ev_watcher_init (&once->to, once_cb_to); |
1269 | ev_watcher_init (&once->to, once_cb_to); |
1190 | if (timeout >= 0.) |
1270 | if (timeout >= 0.) |
1191 | { |
1271 | { |
1192 | ev_timer_set (&once->to, timeout, 0.); |
1272 | ev_timer_set (&once->to, timeout, 0.); |
1193 | ev_timer_start (&once->to); |
1273 | ev_timer_start (EV_A_ &once->to); |
1194 | } |
1274 | } |
1195 | } |
1275 | } |
1196 | } |
1276 | } |
1197 | |
1277 | |
1198 | /*****************************************************************************/ |
1278 | /*****************************************************************************/ |