… | |
… | |
2 | |
2 | |
3 | libev - a high performance full-featured event loop written in C |
3 | libev - a high performance full-featured event loop written in C |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
|
|
7 | /* this is the only header you need */ |
7 | #include <ev.h> |
8 | #include <ev.h> |
|
|
9 | |
|
|
10 | /* what follows is a fully working example program */ |
|
|
11 | ev_io stdin_watcher; |
|
|
12 | ev_timer timeout_watcher; |
|
|
13 | |
|
|
14 | /* called when data readable on stdin */ |
|
|
15 | static void |
|
|
16 | stdin_cb (EV_P_ struct ev_io *w, int revents) |
|
|
17 | { |
|
|
18 | /* puts ("stdin ready"); */ |
|
|
19 | ev_io_stop (EV_A_ w); /* just a syntax example */ |
|
|
20 | ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ |
|
|
21 | } |
|
|
22 | |
|
|
23 | static void |
|
|
24 | timeout_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
25 | { |
|
|
26 | /* puts ("timeout"); */ |
|
|
27 | ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ |
|
|
28 | } |
|
|
29 | |
|
|
30 | int |
|
|
31 | main (void) |
|
|
32 | { |
|
|
33 | struct ev_loop *loop = ev_default_loop (0); |
|
|
34 | |
|
|
35 | /* initialise an io watcher, then start it */ |
|
|
36 | ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
|
|
37 | ev_io_start (loop, &stdin_watcher); |
|
|
38 | |
|
|
39 | /* simple non-repeating 5.5 second timeout */ |
|
|
40 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
41 | ev_timer_start (loop, &timeout_watcher); |
|
|
42 | |
|
|
43 | /* loop till timeout or data ready */ |
|
|
44 | ev_loop (loop, 0); |
|
|
45 | |
|
|
46 | return 0; |
|
|
47 | } |
8 | |
48 | |
9 | =head1 DESCRIPTION |
49 | =head1 DESCRIPTION |
10 | |
50 | |
11 | Libev is an event loop: you register interest in certain events (such as a |
51 | Libev is an event loop: you register interest in certain events (such as a |
12 | file descriptor being readable or a timeout occuring), and it will manage |
52 | file descriptor being readable or a timeout occuring), and it will manage |