1 |
root |
1.17 |
/* |
2 |
root |
1.36 |
* libev event processing core, watcher management |
3 |
|
|
* |
4 |
root |
1.17 |
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions are |
9 |
|
|
* met: |
10 |
|
|
* |
11 |
|
|
* * Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* |
14 |
|
|
* * Redistributions in binary form must reproduce the above |
15 |
|
|
* copyright notice, this list of conditions and the following |
16 |
|
|
* disclaimer in the documentation and/or other materials provided |
17 |
|
|
* with the distribution. |
18 |
|
|
* |
19 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 |
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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. |
30 |
|
|
*/ |
31 |
root |
1.29 |
#if EV_USE_CONFIG_H |
32 |
|
|
# include "config.h" |
33 |
|
|
#endif |
34 |
root |
1.17 |
|
35 |
root |
1.1 |
#include <math.h> |
36 |
|
|
#include <stdlib.h> |
37 |
root |
1.7 |
#include <unistd.h> |
38 |
|
|
#include <fcntl.h> |
39 |
|
|
#include <signal.h> |
40 |
root |
1.16 |
#include <stddef.h> |
41 |
root |
1.1 |
|
42 |
|
|
#include <stdio.h> |
43 |
|
|
|
44 |
root |
1.4 |
#include <assert.h> |
45 |
root |
1.1 |
#include <errno.h> |
46 |
root |
1.22 |
#include <sys/types.h> |
47 |
|
|
#include <sys/wait.h> |
48 |
root |
1.1 |
#include <sys/time.h> |
49 |
|
|
#include <time.h> |
50 |
|
|
|
51 |
root |
1.40 |
/**/ |
52 |
|
|
|
53 |
root |
1.29 |
#ifndef EV_USE_MONOTONIC |
54 |
root |
1.37 |
# define EV_USE_MONOTONIC 1 |
55 |
|
|
#endif |
56 |
|
|
|
57 |
root |
1.29 |
#ifndef EV_USE_SELECT |
58 |
|
|
# define EV_USE_SELECT 1 |
59 |
root |
1.10 |
#endif |
60 |
|
|
|
61 |
root |
1.29 |
#ifndef EV_USE_EPOLL |
62 |
|
|
# define EV_USE_EPOLL 0 |
63 |
root |
1.10 |
#endif |
64 |
|
|
|
65 |
root |
1.40 |
#ifndef EV_USE_REALTIME |
66 |
|
|
# define EV_USE_REALTIME 1 |
67 |
|
|
#endif |
68 |
|
|
|
69 |
|
|
/**/ |
70 |
|
|
|
71 |
|
|
#ifndef CLOCK_MONOTONIC |
72 |
|
|
# undef EV_USE_MONOTONIC |
73 |
|
|
# define EV_USE_MONOTONIC 0 |
74 |
|
|
#endif |
75 |
|
|
|
76 |
root |
1.31 |
#ifndef CLOCK_REALTIME |
77 |
root |
1.40 |
# undef EV_USE_REALTIME |
78 |
root |
1.31 |
# define EV_USE_REALTIME 0 |
79 |
|
|
#endif |
80 |
root |
1.40 |
|
81 |
|
|
/**/ |
82 |
root |
1.1 |
|
83 |
root |
1.4 |
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
84 |
root |
1.40 |
#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
85 |
root |
1.31 |
#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
86 |
root |
1.40 |
/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
87 |
root |
1.1 |
|
88 |
|
|
#include "ev.h" |
89 |
|
|
|
90 |
root |
1.40 |
#if __GNUC__ >= 3 |
91 |
|
|
# define expect(expr,value) __builtin_expect ((expr),(value)) |
92 |
|
|
# define inline inline |
93 |
|
|
#else |
94 |
|
|
# define expect(expr,value) (expr) |
95 |
|
|
# define inline static |
96 |
|
|
#endif |
97 |
|
|
|
98 |
|
|
#define expect_false(expr) expect ((expr) != 0, 0) |
99 |
|
|
#define expect_true(expr) expect ((expr) != 0, 1) |
100 |
|
|
|
101 |
root |
1.10 |
typedef struct ev_watcher *W; |
102 |
|
|
typedef struct ev_watcher_list *WL; |
103 |
root |
1.12 |
typedef struct ev_watcher_time *WT; |
104 |
root |
1.10 |
|
105 |
root |
1.40 |
static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
106 |
root |
1.1 |
ev_tstamp ev_now; |
107 |
|
|
int ev_method; |
108 |
|
|
|
109 |
|
|
static int have_monotonic; /* runtime */ |
110 |
|
|
|
111 |
|
|
static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
112 |
root |
1.5 |
static void (*method_modify)(int fd, int oev, int nev); |
113 |
root |
1.1 |
static void (*method_poll)(ev_tstamp timeout); |
114 |
|
|
|
115 |
root |
1.8 |
/*****************************************************************************/ |
116 |
|
|
|
117 |
root |
1.1 |
ev_tstamp |
118 |
|
|
ev_time (void) |
119 |
|
|
{ |
120 |
root |
1.29 |
#if EV_USE_REALTIME |
121 |
root |
1.1 |
struct timespec ts; |
122 |
|
|
clock_gettime (CLOCK_REALTIME, &ts); |
123 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
124 |
|
|
#else |
125 |
|
|
struct timeval tv; |
126 |
|
|
gettimeofday (&tv, 0); |
127 |
|
|
return tv.tv_sec + tv.tv_usec * 1e-6; |
128 |
|
|
#endif |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
static ev_tstamp |
132 |
|
|
get_clock (void) |
133 |
|
|
{ |
134 |
root |
1.29 |
#if EV_USE_MONOTONIC |
135 |
root |
1.40 |
if (expect_true (have_monotonic)) |
136 |
root |
1.1 |
{ |
137 |
|
|
struct timespec ts; |
138 |
|
|
clock_gettime (CLOCK_MONOTONIC, &ts); |
139 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
140 |
|
|
} |
141 |
|
|
#endif |
142 |
|
|
|
143 |
|
|
return ev_time (); |
144 |
|
|
} |
145 |
|
|
|
146 |
root |
1.30 |
#define array_roundsize(base,n) ((n) | 4 & ~3) |
147 |
root |
1.29 |
|
148 |
root |
1.1 |
#define array_needsize(base,cur,cnt,init) \ |
149 |
root |
1.40 |
if (expect_false ((cnt) > cur)) \ |
150 |
root |
1.1 |
{ \ |
151 |
root |
1.23 |
int newcnt = cur; \ |
152 |
|
|
do \ |
153 |
|
|
{ \ |
154 |
root |
1.30 |
newcnt = array_roundsize (base, newcnt << 1); \ |
155 |
root |
1.23 |
} \ |
156 |
|
|
while ((cnt) > newcnt); \ |
157 |
|
|
\ |
158 |
root |
1.1 |
base = realloc (base, sizeof (*base) * (newcnt)); \ |
159 |
|
|
init (base + cur, newcnt - cur); \ |
160 |
|
|
cur = newcnt; \ |
161 |
|
|
} |
162 |
|
|
|
163 |
root |
1.8 |
/*****************************************************************************/ |
164 |
|
|
|
165 |
root |
1.1 |
typedef struct |
166 |
|
|
{ |
167 |
|
|
struct ev_io *head; |
168 |
root |
1.33 |
unsigned char events; |
169 |
|
|
unsigned char reify; |
170 |
root |
1.1 |
} ANFD; |
171 |
|
|
|
172 |
|
|
static ANFD *anfds; |
173 |
|
|
static int anfdmax; |
174 |
|
|
|
175 |
|
|
static void |
176 |
|
|
anfds_init (ANFD *base, int count) |
177 |
|
|
{ |
178 |
|
|
while (count--) |
179 |
|
|
{ |
180 |
root |
1.27 |
base->head = 0; |
181 |
|
|
base->events = EV_NONE; |
182 |
root |
1.33 |
base->reify = 0; |
183 |
|
|
|
184 |
root |
1.1 |
++base; |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
typedef struct |
189 |
|
|
{ |
190 |
root |
1.10 |
W w; |
191 |
root |
1.1 |
int events; |
192 |
|
|
} ANPENDING; |
193 |
|
|
|
194 |
|
|
static ANPENDING *pendings; |
195 |
|
|
static int pendingmax, pendingcnt; |
196 |
|
|
|
197 |
|
|
static void |
198 |
root |
1.10 |
event (W w, int events) |
199 |
root |
1.1 |
{ |
200 |
root |
1.32 |
if (w->pending) |
201 |
|
|
{ |
202 |
|
|
pendings [w->pending - 1].events |= events; |
203 |
|
|
return; |
204 |
|
|
} |
205 |
|
|
|
206 |
root |
1.30 |
w->pending = ++pendingcnt; |
207 |
|
|
array_needsize (pendings, pendingmax, pendingcnt, ); |
208 |
|
|
pendings [pendingcnt - 1].w = w; |
209 |
|
|
pendings [pendingcnt - 1].events = events; |
210 |
root |
1.1 |
} |
211 |
|
|
|
212 |
|
|
static void |
213 |
root |
1.27 |
queue_events (W *events, int eventcnt, int type) |
214 |
|
|
{ |
215 |
|
|
int i; |
216 |
|
|
|
217 |
|
|
for (i = 0; i < eventcnt; ++i) |
218 |
|
|
event (events [i], type); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
static void |
222 |
root |
1.1 |
fd_event (int fd, int events) |
223 |
|
|
{ |
224 |
|
|
ANFD *anfd = anfds + fd; |
225 |
|
|
struct ev_io *w; |
226 |
|
|
|
227 |
|
|
for (w = anfd->head; w; w = w->next) |
228 |
|
|
{ |
229 |
|
|
int ev = w->events & events; |
230 |
|
|
|
231 |
|
|
if (ev) |
232 |
root |
1.10 |
event ((W)w, ev); |
233 |
root |
1.1 |
} |
234 |
|
|
} |
235 |
|
|
|
236 |
root |
1.27 |
/*****************************************************************************/ |
237 |
|
|
|
238 |
|
|
static int *fdchanges; |
239 |
|
|
static int fdchangemax, fdchangecnt; |
240 |
|
|
|
241 |
root |
1.9 |
static void |
242 |
root |
1.27 |
fd_reify (void) |
243 |
root |
1.9 |
{ |
244 |
|
|
int i; |
245 |
|
|
|
246 |
root |
1.27 |
for (i = 0; i < fdchangecnt; ++i) |
247 |
|
|
{ |
248 |
|
|
int fd = fdchanges [i]; |
249 |
|
|
ANFD *anfd = anfds + fd; |
250 |
|
|
struct ev_io *w; |
251 |
|
|
|
252 |
|
|
int events = 0; |
253 |
|
|
|
254 |
|
|
for (w = anfd->head; w; w = w->next) |
255 |
|
|
events |= w->events; |
256 |
|
|
|
257 |
root |
1.33 |
anfd->reify = 0; |
258 |
root |
1.27 |
|
259 |
|
|
if (anfd->events != events) |
260 |
|
|
{ |
261 |
|
|
method_modify (fd, anfd->events, events); |
262 |
|
|
anfd->events = events; |
263 |
|
|
} |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
fdchangecnt = 0; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
static void |
270 |
|
|
fd_change (int fd) |
271 |
|
|
{ |
272 |
root |
1.33 |
if (anfds [fd].reify || fdchangecnt < 0) |
273 |
root |
1.27 |
return; |
274 |
|
|
|
275 |
root |
1.33 |
anfds [fd].reify = 1; |
276 |
root |
1.27 |
|
277 |
|
|
++fdchangecnt; |
278 |
|
|
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
279 |
|
|
fdchanges [fdchangecnt - 1] = fd; |
280 |
root |
1.9 |
} |
281 |
|
|
|
282 |
root |
1.19 |
/* called on EBADF to verify fds */ |
283 |
|
|
static void |
284 |
root |
1.24 |
fd_recheck (void) |
285 |
root |
1.19 |
{ |
286 |
|
|
int fd; |
287 |
|
|
|
288 |
|
|
for (fd = 0; fd < anfdmax; ++fd) |
289 |
root |
1.27 |
if (anfds [fd].events) |
290 |
root |
1.19 |
if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
291 |
|
|
while (anfds [fd].head) |
292 |
root |
1.24 |
{ |
293 |
root |
1.32 |
ev_io_stop (anfds [fd].head); |
294 |
root |
1.33 |
event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
295 |
root |
1.24 |
} |
296 |
root |
1.19 |
} |
297 |
|
|
|
298 |
root |
1.8 |
/*****************************************************************************/ |
299 |
|
|
|
300 |
root |
1.12 |
static struct ev_timer **timers; |
301 |
|
|
static int timermax, timercnt; |
302 |
root |
1.4 |
|
303 |
root |
1.12 |
static struct ev_periodic **periodics; |
304 |
|
|
static int periodicmax, periodiccnt; |
305 |
root |
1.1 |
|
306 |
|
|
static void |
307 |
root |
1.12 |
upheap (WT *timers, int k) |
308 |
root |
1.1 |
{ |
309 |
root |
1.12 |
WT w = timers [k]; |
310 |
root |
1.1 |
|
311 |
|
|
while (k && timers [k >> 1]->at > w->at) |
312 |
|
|
{ |
313 |
|
|
timers [k] = timers [k >> 1]; |
314 |
|
|
timers [k]->active = k + 1; |
315 |
|
|
k >>= 1; |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
timers [k] = w; |
319 |
|
|
timers [k]->active = k + 1; |
320 |
|
|
|
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
static void |
324 |
root |
1.12 |
downheap (WT *timers, int N, int k) |
325 |
root |
1.1 |
{ |
326 |
root |
1.12 |
WT w = timers [k]; |
327 |
root |
1.1 |
|
328 |
root |
1.4 |
while (k < (N >> 1)) |
329 |
root |
1.1 |
{ |
330 |
|
|
int j = k << 1; |
331 |
|
|
|
332 |
root |
1.4 |
if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
333 |
root |
1.1 |
++j; |
334 |
|
|
|
335 |
|
|
if (w->at <= timers [j]->at) |
336 |
|
|
break; |
337 |
|
|
|
338 |
|
|
timers [k] = timers [j]; |
339 |
root |
1.2 |
timers [k]->active = k + 1; |
340 |
root |
1.1 |
k = j; |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
timers [k] = w; |
344 |
|
|
timers [k]->active = k + 1; |
345 |
|
|
} |
346 |
|
|
|
347 |
root |
1.8 |
/*****************************************************************************/ |
348 |
|
|
|
349 |
root |
1.7 |
typedef struct |
350 |
|
|
{ |
351 |
|
|
struct ev_signal *head; |
352 |
root |
1.34 |
sig_atomic_t volatile gotsig; |
353 |
root |
1.7 |
} ANSIG; |
354 |
|
|
|
355 |
|
|
static ANSIG *signals; |
356 |
root |
1.4 |
static int signalmax; |
357 |
root |
1.1 |
|
358 |
root |
1.7 |
static int sigpipe [2]; |
359 |
root |
1.34 |
static sig_atomic_t volatile gotsig; |
360 |
root |
1.7 |
static struct ev_io sigev; |
361 |
|
|
|
362 |
root |
1.1 |
static void |
363 |
root |
1.7 |
signals_init (ANSIG *base, int count) |
364 |
root |
1.1 |
{ |
365 |
|
|
while (count--) |
366 |
root |
1.7 |
{ |
367 |
|
|
base->head = 0; |
368 |
|
|
base->gotsig = 0; |
369 |
root |
1.33 |
|
370 |
root |
1.7 |
++base; |
371 |
|
|
} |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
static void |
375 |
|
|
sighandler (int signum) |
376 |
|
|
{ |
377 |
|
|
signals [signum - 1].gotsig = 1; |
378 |
|
|
|
379 |
|
|
if (!gotsig) |
380 |
|
|
{ |
381 |
|
|
gotsig = 1; |
382 |
root |
1.34 |
write (sigpipe [1], &signum, 1); |
383 |
root |
1.7 |
} |
384 |
|
|
} |
385 |
|
|
|
386 |
|
|
static void |
387 |
|
|
sigcb (struct ev_io *iow, int revents) |
388 |
|
|
{ |
389 |
|
|
struct ev_signal *w; |
390 |
root |
1.38 |
int signum; |
391 |
root |
1.7 |
|
392 |
root |
1.34 |
read (sigpipe [0], &revents, 1); |
393 |
root |
1.7 |
gotsig = 0; |
394 |
|
|
|
395 |
root |
1.38 |
for (signum = signalmax; signum--; ) |
396 |
|
|
if (signals [signum].gotsig) |
397 |
root |
1.7 |
{ |
398 |
root |
1.38 |
signals [signum].gotsig = 0; |
399 |
root |
1.7 |
|
400 |
root |
1.38 |
for (w = signals [signum].head; w; w = w->next) |
401 |
root |
1.10 |
event ((W)w, EV_SIGNAL); |
402 |
root |
1.7 |
} |
403 |
|
|
} |
404 |
|
|
|
405 |
|
|
static void |
406 |
|
|
siginit (void) |
407 |
|
|
{ |
408 |
|
|
fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
409 |
|
|
fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
410 |
|
|
|
411 |
|
|
/* rather than sort out wether we really need nb, set it */ |
412 |
|
|
fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
413 |
|
|
fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
414 |
|
|
|
415 |
root |
1.28 |
ev_io_set (&sigev, sigpipe [0], EV_READ); |
416 |
|
|
ev_io_start (&sigev); |
417 |
root |
1.1 |
} |
418 |
|
|
|
419 |
root |
1.8 |
/*****************************************************************************/ |
420 |
|
|
|
421 |
root |
1.9 |
static struct ev_idle **idles; |
422 |
|
|
static int idlemax, idlecnt; |
423 |
|
|
|
424 |
root |
1.20 |
static struct ev_prepare **prepares; |
425 |
|
|
static int preparemax, preparecnt; |
426 |
|
|
|
427 |
root |
1.9 |
static struct ev_check **checks; |
428 |
|
|
static int checkmax, checkcnt; |
429 |
|
|
|
430 |
|
|
/*****************************************************************************/ |
431 |
|
|
|
432 |
root |
1.22 |
static struct ev_child *childs [PID_HASHSIZE]; |
433 |
|
|
static struct ev_signal childev; |
434 |
|
|
|
435 |
|
|
#ifndef WCONTINUED |
436 |
|
|
# define WCONTINUED 0 |
437 |
|
|
#endif |
438 |
|
|
|
439 |
|
|
static void |
440 |
|
|
childcb (struct ev_signal *sw, int revents) |
441 |
|
|
{ |
442 |
|
|
struct ev_child *w; |
443 |
|
|
int pid, status; |
444 |
|
|
|
445 |
|
|
while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
446 |
|
|
for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
447 |
root |
1.39 |
if (w->pid == pid || !w->pid) |
448 |
root |
1.22 |
{ |
449 |
|
|
w->status = status; |
450 |
|
|
event ((W)w, EV_CHILD); |
451 |
|
|
} |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
/*****************************************************************************/ |
455 |
|
|
|
456 |
root |
1.29 |
#if EV_USE_EPOLL |
457 |
root |
1.1 |
# include "ev_epoll.c" |
458 |
|
|
#endif |
459 |
root |
1.29 |
#if EV_USE_SELECT |
460 |
root |
1.1 |
# include "ev_select.c" |
461 |
|
|
#endif |
462 |
|
|
|
463 |
root |
1.24 |
int |
464 |
|
|
ev_version_major (void) |
465 |
|
|
{ |
466 |
|
|
return EV_VERSION_MAJOR; |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
int |
470 |
|
|
ev_version_minor (void) |
471 |
|
|
{ |
472 |
|
|
return EV_VERSION_MINOR; |
473 |
|
|
} |
474 |
|
|
|
475 |
root |
1.1 |
int ev_init (int flags) |
476 |
|
|
{ |
477 |
root |
1.23 |
if (!ev_method) |
478 |
|
|
{ |
479 |
root |
1.29 |
#if EV_USE_MONOTONIC |
480 |
root |
1.23 |
{ |
481 |
|
|
struct timespec ts; |
482 |
|
|
if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
483 |
|
|
have_monotonic = 1; |
484 |
|
|
} |
485 |
root |
1.1 |
#endif |
486 |
|
|
|
487 |
root |
1.40 |
ev_now = ev_time (); |
488 |
|
|
now = get_clock (); |
489 |
|
|
now_floor = now; |
490 |
|
|
diff = ev_now - now; |
491 |
root |
1.1 |
|
492 |
root |
1.23 |
if (pipe (sigpipe)) |
493 |
|
|
return 0; |
494 |
root |
1.7 |
|
495 |
root |
1.23 |
ev_method = EVMETHOD_NONE; |
496 |
root |
1.29 |
#if EV_USE_EPOLL |
497 |
root |
1.23 |
if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
498 |
root |
1.1 |
#endif |
499 |
root |
1.29 |
#if EV_USE_SELECT |
500 |
root |
1.23 |
if (ev_method == EVMETHOD_NONE) select_init (flags); |
501 |
root |
1.1 |
#endif |
502 |
|
|
|
503 |
root |
1.23 |
if (ev_method) |
504 |
|
|
{ |
505 |
root |
1.28 |
ev_watcher_init (&sigev, sigcb); |
506 |
root |
1.23 |
siginit (); |
507 |
root |
1.22 |
|
508 |
root |
1.28 |
ev_signal_init (&childev, childcb, SIGCHLD); |
509 |
|
|
ev_signal_start (&childev); |
510 |
root |
1.23 |
} |
511 |
root |
1.7 |
} |
512 |
|
|
|
513 |
root |
1.1 |
return ev_method; |
514 |
|
|
} |
515 |
|
|
|
516 |
root |
1.8 |
/*****************************************************************************/ |
517 |
|
|
|
518 |
root |
1.24 |
void |
519 |
root |
1.35 |
ev_fork_prepare (void) |
520 |
root |
1.1 |
{ |
521 |
root |
1.11 |
/* nop */ |
522 |
root |
1.1 |
} |
523 |
|
|
|
524 |
root |
1.24 |
void |
525 |
root |
1.35 |
ev_fork_parent (void) |
526 |
root |
1.1 |
{ |
527 |
root |
1.11 |
/* nop */ |
528 |
root |
1.1 |
} |
529 |
|
|
|
530 |
root |
1.24 |
void |
531 |
root |
1.35 |
ev_fork_child (void) |
532 |
root |
1.1 |
{ |
533 |
root |
1.29 |
#if EV_USE_EPOLL |
534 |
root |
1.5 |
if (ev_method == EVMETHOD_EPOLL) |
535 |
|
|
epoll_postfork_child (); |
536 |
root |
1.1 |
#endif |
537 |
root |
1.7 |
|
538 |
root |
1.28 |
ev_io_stop (&sigev); |
539 |
root |
1.7 |
close (sigpipe [0]); |
540 |
|
|
close (sigpipe [1]); |
541 |
|
|
pipe (sigpipe); |
542 |
|
|
siginit (); |
543 |
root |
1.1 |
} |
544 |
|
|
|
545 |
root |
1.8 |
/*****************************************************************************/ |
546 |
|
|
|
547 |
root |
1.1 |
static void |
548 |
root |
1.24 |
call_pending (void) |
549 |
root |
1.1 |
{ |
550 |
root |
1.18 |
while (pendingcnt) |
551 |
root |
1.1 |
{ |
552 |
root |
1.18 |
ANPENDING *p = pendings + --pendingcnt; |
553 |
root |
1.1 |
|
554 |
|
|
if (p->w) |
555 |
|
|
{ |
556 |
|
|
p->w->pending = 0; |
557 |
|
|
p->w->cb (p->w, p->events); |
558 |
|
|
} |
559 |
|
|
} |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
static void |
563 |
root |
1.24 |
timers_reify (void) |
564 |
root |
1.1 |
{ |
565 |
root |
1.4 |
while (timercnt && timers [0]->at <= now) |
566 |
root |
1.1 |
{ |
567 |
|
|
struct ev_timer *w = timers [0]; |
568 |
|
|
|
569 |
root |
1.4 |
/* first reschedule or stop timer */ |
570 |
root |
1.1 |
if (w->repeat) |
571 |
|
|
{ |
572 |
root |
1.33 |
assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
573 |
root |
1.12 |
w->at = now + w->repeat; |
574 |
|
|
downheap ((WT *)timers, timercnt, 0); |
575 |
|
|
} |
576 |
|
|
else |
577 |
root |
1.28 |
ev_timer_stop (w); /* nonrepeating: stop timer */ |
578 |
root |
1.30 |
|
579 |
|
|
event ((W)w, EV_TIMEOUT); |
580 |
root |
1.12 |
} |
581 |
|
|
} |
582 |
root |
1.4 |
|
583 |
root |
1.12 |
static void |
584 |
root |
1.24 |
periodics_reify (void) |
585 |
root |
1.12 |
{ |
586 |
|
|
while (periodiccnt && periodics [0]->at <= ev_now) |
587 |
|
|
{ |
588 |
|
|
struct ev_periodic *w = periodics [0]; |
589 |
root |
1.1 |
|
590 |
root |
1.12 |
/* first reschedule or stop timer */ |
591 |
|
|
if (w->interval) |
592 |
|
|
{ |
593 |
|
|
w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
594 |
root |
1.33 |
assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
595 |
root |
1.12 |
downheap ((WT *)periodics, periodiccnt, 0); |
596 |
root |
1.1 |
} |
597 |
|
|
else |
598 |
root |
1.28 |
ev_periodic_stop (w); /* nonrepeating: stop timer */ |
599 |
root |
1.12 |
|
600 |
root |
1.33 |
event ((W)w, EV_PERIODIC); |
601 |
root |
1.12 |
} |
602 |
|
|
} |
603 |
|
|
|
604 |
|
|
static void |
605 |
root |
1.13 |
periodics_reschedule (ev_tstamp diff) |
606 |
root |
1.12 |
{ |
607 |
|
|
int i; |
608 |
|
|
|
609 |
root |
1.13 |
/* adjust periodics after time jump */ |
610 |
root |
1.12 |
for (i = 0; i < periodiccnt; ++i) |
611 |
|
|
{ |
612 |
|
|
struct ev_periodic *w = periodics [i]; |
613 |
|
|
|
614 |
|
|
if (w->interval) |
615 |
root |
1.4 |
{ |
616 |
root |
1.12 |
ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
617 |
|
|
|
618 |
|
|
if (fabs (diff) >= 1e-4) |
619 |
|
|
{ |
620 |
root |
1.28 |
ev_periodic_stop (w); |
621 |
|
|
ev_periodic_start (w); |
622 |
root |
1.12 |
|
623 |
|
|
i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
624 |
|
|
} |
625 |
root |
1.4 |
} |
626 |
root |
1.12 |
} |
627 |
root |
1.1 |
} |
628 |
|
|
|
629 |
root |
1.40 |
static int |
630 |
|
|
time_update_monotonic (void) |
631 |
|
|
{ |
632 |
|
|
now = get_clock (); |
633 |
|
|
|
634 |
|
|
if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
635 |
|
|
{ |
636 |
|
|
ev_now = now + diff; |
637 |
|
|
return 0; |
638 |
|
|
} |
639 |
|
|
else |
640 |
|
|
{ |
641 |
|
|
now_floor = now; |
642 |
|
|
ev_now = ev_time (); |
643 |
|
|
return 1; |
644 |
|
|
} |
645 |
|
|
} |
646 |
|
|
|
647 |
root |
1.4 |
static void |
648 |
root |
1.24 |
time_update (void) |
649 |
root |
1.4 |
{ |
650 |
|
|
int i; |
651 |
root |
1.12 |
|
652 |
root |
1.40 |
#if EV_USE_MONOTONIC |
653 |
|
|
if (expect_true (have_monotonic)) |
654 |
|
|
{ |
655 |
|
|
if (time_update_monotonic ()) |
656 |
|
|
{ |
657 |
|
|
ev_tstamp odiff = diff; |
658 |
root |
1.4 |
|
659 |
root |
1.40 |
for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
660 |
|
|
{ |
661 |
|
|
diff = ev_now - now; |
662 |
root |
1.4 |
|
663 |
root |
1.40 |
if (fabs (odiff - diff) < MIN_TIMEJUMP) |
664 |
|
|
return; /* all is well */ |
665 |
root |
1.4 |
|
666 |
root |
1.40 |
ev_now = ev_time (); |
667 |
|
|
now = get_clock (); |
668 |
|
|
now_floor = now; |
669 |
|
|
} |
670 |
root |
1.4 |
|
671 |
root |
1.40 |
periodics_reschedule (diff - odiff); |
672 |
|
|
/* no timer adjustment, as the monotonic clock doesn't jump */ |
673 |
root |
1.4 |
} |
674 |
|
|
} |
675 |
|
|
else |
676 |
root |
1.40 |
#endif |
677 |
root |
1.4 |
{ |
678 |
root |
1.40 |
ev_now = ev_time (); |
679 |
|
|
|
680 |
|
|
if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
681 |
root |
1.13 |
{ |
682 |
|
|
periodics_reschedule (ev_now - now); |
683 |
|
|
|
684 |
|
|
/* adjust timers. this is easy, as the offset is the same for all */ |
685 |
|
|
for (i = 0; i < timercnt; ++i) |
686 |
|
|
timers [i]->at += diff; |
687 |
|
|
} |
688 |
root |
1.4 |
|
689 |
|
|
now = ev_now; |
690 |
|
|
} |
691 |
|
|
} |
692 |
|
|
|
693 |
root |
1.1 |
int ev_loop_done; |
694 |
|
|
|
695 |
root |
1.4 |
void ev_loop (int flags) |
696 |
root |
1.1 |
{ |
697 |
|
|
double block; |
698 |
root |
1.26 |
ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
699 |
root |
1.1 |
|
700 |
root |
1.20 |
do |
701 |
root |
1.9 |
{ |
702 |
root |
1.20 |
/* queue check watchers (and execute them) */ |
703 |
root |
1.40 |
if (expect_false (preparecnt)) |
704 |
root |
1.20 |
{ |
705 |
|
|
queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
706 |
|
|
call_pending (); |
707 |
|
|
} |
708 |
root |
1.9 |
|
709 |
root |
1.1 |
/* update fd-related kernel structures */ |
710 |
root |
1.5 |
fd_reify (); |
711 |
root |
1.1 |
|
712 |
|
|
/* calculate blocking time */ |
713 |
root |
1.12 |
|
714 |
root |
1.21 |
/* we only need this for !monotonic clockor timers, but as we basically |
715 |
|
|
always have timers, we just calculate it always */ |
716 |
root |
1.40 |
#if EV_USE_MONOTONIC |
717 |
|
|
if (expect_true (have_monotonic)) |
718 |
|
|
time_update_monotonic (); |
719 |
|
|
else |
720 |
|
|
#endif |
721 |
|
|
{ |
722 |
|
|
ev_now = ev_time (); |
723 |
|
|
now = ev_now; |
724 |
|
|
} |
725 |
root |
1.12 |
|
726 |
root |
1.9 |
if (flags & EVLOOP_NONBLOCK || idlecnt) |
727 |
root |
1.1 |
block = 0.; |
728 |
|
|
else |
729 |
|
|
{ |
730 |
root |
1.4 |
block = MAX_BLOCKTIME; |
731 |
|
|
|
732 |
root |
1.12 |
if (timercnt) |
733 |
root |
1.4 |
{ |
734 |
root |
1.40 |
ev_tstamp to = timers [0]->at - now + method_fudge; |
735 |
root |
1.4 |
if (block > to) block = to; |
736 |
|
|
} |
737 |
|
|
|
738 |
root |
1.12 |
if (periodiccnt) |
739 |
root |
1.4 |
{ |
740 |
root |
1.12 |
ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
741 |
root |
1.4 |
if (block > to) block = to; |
742 |
|
|
} |
743 |
|
|
|
744 |
root |
1.1 |
if (block < 0.) block = 0.; |
745 |
|
|
} |
746 |
|
|
|
747 |
|
|
method_poll (block); |
748 |
|
|
|
749 |
root |
1.4 |
/* update ev_now, do magic */ |
750 |
|
|
time_update (); |
751 |
|
|
|
752 |
root |
1.9 |
/* queue pending timers and reschedule them */ |
753 |
root |
1.20 |
timers_reify (); /* relative timers called last */ |
754 |
|
|
periodics_reify (); /* absolute timers called first */ |
755 |
root |
1.1 |
|
756 |
root |
1.9 |
/* queue idle watchers unless io or timers are pending */ |
757 |
|
|
if (!pendingcnt) |
758 |
root |
1.10 |
queue_events ((W *)idles, idlecnt, EV_IDLE); |
759 |
root |
1.9 |
|
760 |
root |
1.20 |
/* queue check watchers, to be executed first */ |
761 |
|
|
if (checkcnt) |
762 |
|
|
queue_events ((W *)checks, checkcnt, EV_CHECK); |
763 |
root |
1.9 |
|
764 |
root |
1.1 |
call_pending (); |
765 |
|
|
} |
766 |
|
|
while (!ev_loop_done); |
767 |
root |
1.13 |
|
768 |
|
|
if (ev_loop_done != 2) |
769 |
|
|
ev_loop_done = 0; |
770 |
root |
1.1 |
} |
771 |
|
|
|
772 |
root |
1.8 |
/*****************************************************************************/ |
773 |
|
|
|
774 |
root |
1.1 |
static void |
775 |
root |
1.10 |
wlist_add (WL *head, WL elem) |
776 |
root |
1.1 |
{ |
777 |
|
|
elem->next = *head; |
778 |
|
|
*head = elem; |
779 |
|
|
} |
780 |
|
|
|
781 |
|
|
static void |
782 |
root |
1.10 |
wlist_del (WL *head, WL elem) |
783 |
root |
1.1 |
{ |
784 |
|
|
while (*head) |
785 |
|
|
{ |
786 |
|
|
if (*head == elem) |
787 |
|
|
{ |
788 |
|
|
*head = elem->next; |
789 |
|
|
return; |
790 |
|
|
} |
791 |
|
|
|
792 |
|
|
head = &(*head)->next; |
793 |
|
|
} |
794 |
|
|
} |
795 |
|
|
|
796 |
|
|
static void |
797 |
root |
1.33 |
ev_clear_pending (W w) |
798 |
root |
1.16 |
{ |
799 |
|
|
if (w->pending) |
800 |
|
|
{ |
801 |
|
|
pendings [w->pending - 1].w = 0; |
802 |
|
|
w->pending = 0; |
803 |
|
|
} |
804 |
|
|
} |
805 |
|
|
|
806 |
|
|
static void |
807 |
root |
1.10 |
ev_start (W w, int active) |
808 |
root |
1.1 |
{ |
809 |
|
|
w->active = active; |
810 |
|
|
} |
811 |
|
|
|
812 |
|
|
static void |
813 |
root |
1.10 |
ev_stop (W w) |
814 |
root |
1.1 |
{ |
815 |
|
|
w->active = 0; |
816 |
|
|
} |
817 |
|
|
|
818 |
root |
1.8 |
/*****************************************************************************/ |
819 |
|
|
|
820 |
root |
1.1 |
void |
821 |
root |
1.28 |
ev_io_start (struct ev_io *w) |
822 |
root |
1.1 |
{ |
823 |
root |
1.37 |
int fd = w->fd; |
824 |
|
|
|
825 |
root |
1.1 |
if (ev_is_active (w)) |
826 |
|
|
return; |
827 |
|
|
|
828 |
root |
1.33 |
assert (("ev_io_start called with negative fd", fd >= 0)); |
829 |
|
|
|
830 |
root |
1.10 |
ev_start ((W)w, 1); |
831 |
root |
1.1 |
array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
832 |
root |
1.10 |
wlist_add ((WL *)&anfds[fd].head, (WL)w); |
833 |
root |
1.1 |
|
834 |
root |
1.27 |
fd_change (fd); |
835 |
root |
1.1 |
} |
836 |
|
|
|
837 |
|
|
void |
838 |
root |
1.28 |
ev_io_stop (struct ev_io *w) |
839 |
root |
1.1 |
{ |
840 |
root |
1.33 |
ev_clear_pending ((W)w); |
841 |
root |
1.1 |
if (!ev_is_active (w)) |
842 |
|
|
return; |
843 |
|
|
|
844 |
root |
1.10 |
wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
845 |
|
|
ev_stop ((W)w); |
846 |
root |
1.1 |
|
847 |
root |
1.27 |
fd_change (w->fd); |
848 |
root |
1.1 |
} |
849 |
|
|
|
850 |
|
|
void |
851 |
root |
1.28 |
ev_timer_start (struct ev_timer *w) |
852 |
root |
1.1 |
{ |
853 |
|
|
if (ev_is_active (w)) |
854 |
|
|
return; |
855 |
|
|
|
856 |
root |
1.12 |
w->at += now; |
857 |
|
|
|
858 |
root |
1.33 |
assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
859 |
root |
1.13 |
|
860 |
root |
1.12 |
ev_start ((W)w, ++timercnt); |
861 |
|
|
array_needsize (timers, timermax, timercnt, ); |
862 |
|
|
timers [timercnt - 1] = w; |
863 |
|
|
upheap ((WT *)timers, timercnt - 1); |
864 |
|
|
} |
865 |
|
|
|
866 |
|
|
void |
867 |
root |
1.28 |
ev_timer_stop (struct ev_timer *w) |
868 |
root |
1.12 |
{ |
869 |
root |
1.33 |
ev_clear_pending ((W)w); |
870 |
root |
1.12 |
if (!ev_is_active (w)) |
871 |
|
|
return; |
872 |
|
|
|
873 |
|
|
if (w->active < timercnt--) |
874 |
root |
1.1 |
{ |
875 |
root |
1.12 |
timers [w->active - 1] = timers [timercnt]; |
876 |
|
|
downheap ((WT *)timers, timercnt, w->active - 1); |
877 |
|
|
} |
878 |
root |
1.4 |
|
879 |
root |
1.14 |
w->at = w->repeat; |
880 |
|
|
|
881 |
root |
1.12 |
ev_stop ((W)w); |
882 |
|
|
} |
883 |
root |
1.4 |
|
884 |
root |
1.12 |
void |
885 |
root |
1.28 |
ev_timer_again (struct ev_timer *w) |
886 |
root |
1.14 |
{ |
887 |
|
|
if (ev_is_active (w)) |
888 |
|
|
{ |
889 |
|
|
if (w->repeat) |
890 |
|
|
{ |
891 |
|
|
w->at = now + w->repeat; |
892 |
|
|
downheap ((WT *)timers, timercnt, w->active - 1); |
893 |
|
|
} |
894 |
|
|
else |
895 |
root |
1.28 |
ev_timer_stop (w); |
896 |
root |
1.14 |
} |
897 |
|
|
else if (w->repeat) |
898 |
root |
1.28 |
ev_timer_start (w); |
899 |
root |
1.14 |
} |
900 |
|
|
|
901 |
|
|
void |
902 |
root |
1.28 |
ev_periodic_start (struct ev_periodic *w) |
903 |
root |
1.12 |
{ |
904 |
|
|
if (ev_is_active (w)) |
905 |
|
|
return; |
906 |
root |
1.1 |
|
907 |
root |
1.33 |
assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
908 |
root |
1.13 |
|
909 |
root |
1.12 |
/* this formula differs from the one in periodic_reify because we do not always round up */ |
910 |
|
|
if (w->interval) |
911 |
|
|
w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
912 |
|
|
|
913 |
|
|
ev_start ((W)w, ++periodiccnt); |
914 |
|
|
array_needsize (periodics, periodicmax, periodiccnt, ); |
915 |
|
|
periodics [periodiccnt - 1] = w; |
916 |
|
|
upheap ((WT *)periodics, periodiccnt - 1); |
917 |
root |
1.1 |
} |
918 |
|
|
|
919 |
|
|
void |
920 |
root |
1.28 |
ev_periodic_stop (struct ev_periodic *w) |
921 |
root |
1.1 |
{ |
922 |
root |
1.33 |
ev_clear_pending ((W)w); |
923 |
root |
1.1 |
if (!ev_is_active (w)) |
924 |
|
|
return; |
925 |
|
|
|
926 |
root |
1.12 |
if (w->active < periodiccnt--) |
927 |
root |
1.2 |
{ |
928 |
root |
1.12 |
periodics [w->active - 1] = periodics [periodiccnt]; |
929 |
|
|
downheap ((WT *)periodics, periodiccnt, w->active - 1); |
930 |
root |
1.2 |
} |
931 |
|
|
|
932 |
root |
1.10 |
ev_stop ((W)w); |
933 |
root |
1.1 |
} |
934 |
|
|
|
935 |
|
|
void |
936 |
root |
1.28 |
ev_signal_start (struct ev_signal *w) |
937 |
root |
1.1 |
{ |
938 |
|
|
if (ev_is_active (w)) |
939 |
|
|
return; |
940 |
|
|
|
941 |
root |
1.33 |
assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
942 |
|
|
|
943 |
root |
1.10 |
ev_start ((W)w, 1); |
944 |
root |
1.1 |
array_needsize (signals, signalmax, w->signum, signals_init); |
945 |
root |
1.10 |
wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
946 |
root |
1.7 |
|
947 |
|
|
if (!w->next) |
948 |
|
|
{ |
949 |
|
|
struct sigaction sa; |
950 |
|
|
sa.sa_handler = sighandler; |
951 |
|
|
sigfillset (&sa.sa_mask); |
952 |
|
|
sa.sa_flags = 0; |
953 |
|
|
sigaction (w->signum, &sa, 0); |
954 |
|
|
} |
955 |
root |
1.1 |
} |
956 |
|
|
|
957 |
|
|
void |
958 |
root |
1.28 |
ev_signal_stop (struct ev_signal *w) |
959 |
root |
1.1 |
{ |
960 |
root |
1.33 |
ev_clear_pending ((W)w); |
961 |
root |
1.1 |
if (!ev_is_active (w)) |
962 |
|
|
return; |
963 |
|
|
|
964 |
root |
1.10 |
wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
965 |
|
|
ev_stop ((W)w); |
966 |
root |
1.7 |
|
967 |
|
|
if (!signals [w->signum - 1].head) |
968 |
|
|
signal (w->signum, SIG_DFL); |
969 |
root |
1.1 |
} |
970 |
|
|
|
971 |
root |
1.28 |
void |
972 |
|
|
ev_idle_start (struct ev_idle *w) |
973 |
root |
1.9 |
{ |
974 |
|
|
if (ev_is_active (w)) |
975 |
|
|
return; |
976 |
|
|
|
977 |
root |
1.10 |
ev_start ((W)w, ++idlecnt); |
978 |
root |
1.9 |
array_needsize (idles, idlemax, idlecnt, ); |
979 |
|
|
idles [idlecnt - 1] = w; |
980 |
|
|
} |
981 |
|
|
|
982 |
root |
1.28 |
void |
983 |
|
|
ev_idle_stop (struct ev_idle *w) |
984 |
root |
1.9 |
{ |
985 |
root |
1.33 |
ev_clear_pending ((W)w); |
986 |
root |
1.16 |
if (ev_is_active (w)) |
987 |
|
|
return; |
988 |
|
|
|
989 |
root |
1.9 |
idles [w->active - 1] = idles [--idlecnt]; |
990 |
root |
1.10 |
ev_stop ((W)w); |
991 |
root |
1.9 |
} |
992 |
|
|
|
993 |
root |
1.28 |
void |
994 |
|
|
ev_prepare_start (struct ev_prepare *w) |
995 |
root |
1.20 |
{ |
996 |
|
|
if (ev_is_active (w)) |
997 |
|
|
return; |
998 |
|
|
|
999 |
|
|
ev_start ((W)w, ++preparecnt); |
1000 |
|
|
array_needsize (prepares, preparemax, preparecnt, ); |
1001 |
|
|
prepares [preparecnt - 1] = w; |
1002 |
|
|
} |
1003 |
|
|
|
1004 |
root |
1.28 |
void |
1005 |
|
|
ev_prepare_stop (struct ev_prepare *w) |
1006 |
root |
1.20 |
{ |
1007 |
root |
1.33 |
ev_clear_pending ((W)w); |
1008 |
root |
1.20 |
if (ev_is_active (w)) |
1009 |
|
|
return; |
1010 |
|
|
|
1011 |
|
|
prepares [w->active - 1] = prepares [--preparecnt]; |
1012 |
|
|
ev_stop ((W)w); |
1013 |
|
|
} |
1014 |
|
|
|
1015 |
root |
1.28 |
void |
1016 |
|
|
ev_check_start (struct ev_check *w) |
1017 |
root |
1.9 |
{ |
1018 |
|
|
if (ev_is_active (w)) |
1019 |
|
|
return; |
1020 |
|
|
|
1021 |
root |
1.10 |
ev_start ((W)w, ++checkcnt); |
1022 |
root |
1.9 |
array_needsize (checks, checkmax, checkcnt, ); |
1023 |
|
|
checks [checkcnt - 1] = w; |
1024 |
|
|
} |
1025 |
|
|
|
1026 |
root |
1.28 |
void |
1027 |
|
|
ev_check_stop (struct ev_check *w) |
1028 |
root |
1.9 |
{ |
1029 |
root |
1.33 |
ev_clear_pending ((W)w); |
1030 |
root |
1.16 |
if (ev_is_active (w)) |
1031 |
|
|
return; |
1032 |
|
|
|
1033 |
root |
1.9 |
checks [w->active - 1] = checks [--checkcnt]; |
1034 |
root |
1.10 |
ev_stop ((W)w); |
1035 |
root |
1.9 |
} |
1036 |
|
|
|
1037 |
root |
1.28 |
void |
1038 |
|
|
ev_child_start (struct ev_child *w) |
1039 |
root |
1.22 |
{ |
1040 |
|
|
if (ev_is_active (w)) |
1041 |
|
|
return; |
1042 |
|
|
|
1043 |
|
|
ev_start ((W)w, 1); |
1044 |
|
|
wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1045 |
|
|
} |
1046 |
|
|
|
1047 |
root |
1.28 |
void |
1048 |
|
|
ev_child_stop (struct ev_child *w) |
1049 |
root |
1.22 |
{ |
1050 |
root |
1.33 |
ev_clear_pending ((W)w); |
1051 |
root |
1.22 |
if (ev_is_active (w)) |
1052 |
|
|
return; |
1053 |
|
|
|
1054 |
|
|
wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1055 |
|
|
ev_stop ((W)w); |
1056 |
|
|
} |
1057 |
|
|
|
1058 |
root |
1.1 |
/*****************************************************************************/ |
1059 |
root |
1.10 |
|
1060 |
root |
1.16 |
struct ev_once |
1061 |
|
|
{ |
1062 |
|
|
struct ev_io io; |
1063 |
|
|
struct ev_timer to; |
1064 |
|
|
void (*cb)(int revents, void *arg); |
1065 |
|
|
void *arg; |
1066 |
|
|
}; |
1067 |
|
|
|
1068 |
|
|
static void |
1069 |
|
|
once_cb (struct ev_once *once, int revents) |
1070 |
|
|
{ |
1071 |
|
|
void (*cb)(int revents, void *arg) = once->cb; |
1072 |
|
|
void *arg = once->arg; |
1073 |
|
|
|
1074 |
root |
1.28 |
ev_io_stop (&once->io); |
1075 |
|
|
ev_timer_stop (&once->to); |
1076 |
root |
1.16 |
free (once); |
1077 |
|
|
|
1078 |
|
|
cb (revents, arg); |
1079 |
|
|
} |
1080 |
|
|
|
1081 |
|
|
static void |
1082 |
|
|
once_cb_io (struct ev_io *w, int revents) |
1083 |
|
|
{ |
1084 |
|
|
once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1085 |
|
|
} |
1086 |
|
|
|
1087 |
|
|
static void |
1088 |
|
|
once_cb_to (struct ev_timer *w, int revents) |
1089 |
|
|
{ |
1090 |
|
|
once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1091 |
|
|
} |
1092 |
|
|
|
1093 |
|
|
void |
1094 |
|
|
ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1095 |
|
|
{ |
1096 |
|
|
struct ev_once *once = malloc (sizeof (struct ev_once)); |
1097 |
|
|
|
1098 |
|
|
if (!once) |
1099 |
root |
1.29 |
cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1100 |
root |
1.16 |
else |
1101 |
|
|
{ |
1102 |
|
|
once->cb = cb; |
1103 |
|
|
once->arg = arg; |
1104 |
|
|
|
1105 |
root |
1.28 |
ev_watcher_init (&once->io, once_cb_io); |
1106 |
root |
1.16 |
if (fd >= 0) |
1107 |
|
|
{ |
1108 |
root |
1.28 |
ev_io_set (&once->io, fd, events); |
1109 |
|
|
ev_io_start (&once->io); |
1110 |
root |
1.16 |
} |
1111 |
|
|
|
1112 |
root |
1.28 |
ev_watcher_init (&once->to, once_cb_to); |
1113 |
root |
1.16 |
if (timeout >= 0.) |
1114 |
|
|
{ |
1115 |
root |
1.28 |
ev_timer_set (&once->to, timeout, 0.); |
1116 |
|
|
ev_timer_start (&once->to); |
1117 |
root |
1.16 |
} |
1118 |
|
|
} |
1119 |
|
|
} |
1120 |
|
|
|
1121 |
|
|
/*****************************************************************************/ |
1122 |
|
|
|
1123 |
root |
1.13 |
#if 0 |
1124 |
root |
1.12 |
|
1125 |
|
|
struct ev_io wio; |
1126 |
root |
1.1 |
|
1127 |
|
|
static void |
1128 |
|
|
sin_cb (struct ev_io *w, int revents) |
1129 |
|
|
{ |
1130 |
|
|
fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
1131 |
|
|
} |
1132 |
|
|
|
1133 |
|
|
static void |
1134 |
|
|
ocb (struct ev_timer *w, int revents) |
1135 |
|
|
{ |
1136 |
root |
1.4 |
//fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1137 |
root |
1.28 |
ev_timer_stop (w); |
1138 |
|
|
ev_timer_start (w); |
1139 |
root |
1.1 |
} |
1140 |
|
|
|
1141 |
root |
1.7 |
static void |
1142 |
|
|
scb (struct ev_signal *w, int revents) |
1143 |
|
|
{ |
1144 |
|
|
fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1145 |
root |
1.28 |
ev_io_stop (&wio); |
1146 |
|
|
ev_io_start (&wio); |
1147 |
root |
1.7 |
} |
1148 |
|
|
|
1149 |
root |
1.9 |
static void |
1150 |
|
|
gcb (struct ev_signal *w, int revents) |
1151 |
|
|
{ |
1152 |
|
|
fprintf (stderr, "generic %x\n", revents); |
1153 |
root |
1.12 |
|
1154 |
root |
1.9 |
} |
1155 |
|
|
|
1156 |
root |
1.1 |
int main (void) |
1157 |
|
|
{ |
1158 |
|
|
ev_init (0); |
1159 |
|
|
|
1160 |
root |
1.28 |
ev_io_init (&wio, sin_cb, 0, EV_READ); |
1161 |
|
|
ev_io_start (&wio); |
1162 |
root |
1.1 |
|
1163 |
root |
1.4 |
struct ev_timer t[10000]; |
1164 |
root |
1.2 |
|
1165 |
root |
1.9 |
#if 0 |
1166 |
root |
1.2 |
int i; |
1167 |
root |
1.4 |
for (i = 0; i < 10000; ++i) |
1168 |
root |
1.2 |
{ |
1169 |
|
|
struct ev_timer *w = t + i; |
1170 |
root |
1.28 |
ev_watcher_init (w, ocb, i); |
1171 |
|
|
ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1172 |
|
|
ev_timer_start (w); |
1173 |
root |
1.2 |
if (drand48 () < 0.5) |
1174 |
root |
1.28 |
ev_timer_stop (w); |
1175 |
root |
1.2 |
} |
1176 |
root |
1.4 |
#endif |
1177 |
|
|
|
1178 |
|
|
struct ev_timer t1; |
1179 |
root |
1.28 |
ev_timer_init (&t1, ocb, 5, 10); |
1180 |
|
|
ev_timer_start (&t1); |
1181 |
root |
1.1 |
|
1182 |
root |
1.7 |
struct ev_signal sig; |
1183 |
root |
1.28 |
ev_signal_init (&sig, scb, SIGQUIT); |
1184 |
|
|
ev_signal_start (&sig); |
1185 |
root |
1.7 |
|
1186 |
root |
1.9 |
struct ev_check cw; |
1187 |
root |
1.28 |
ev_check_init (&cw, gcb); |
1188 |
|
|
ev_check_start (&cw); |
1189 |
root |
1.9 |
|
1190 |
|
|
struct ev_idle iw; |
1191 |
root |
1.28 |
ev_idle_init (&iw, gcb); |
1192 |
|
|
ev_idle_start (&iw); |
1193 |
root |
1.9 |
|
1194 |
root |
1.1 |
ev_loop (0); |
1195 |
|
|
|
1196 |
|
|
return 0; |
1197 |
|
|
} |
1198 |
|
|
|
1199 |
|
|
#endif |
1200 |
|
|
|
1201 |
|
|
|
1202 |
|
|
|
1203 |
|
|
|