ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.51 by root, Tue Nov 27 19:23:31 2007 UTC vs.
Revision 1.53 by root, Tue Nov 27 20:15:02 2007 UTC

2 2
3libev - a high performance full-featured event loop written in C 3libev - 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
11Libev is an event loop: you register interest in certain events (such as a 51Libev is an event loop: you register interest in certain events (such as a
12file descriptor being readable or a timeout occuring), and it will manage 52file descriptor being readable or a timeout occuring), and it will manage
115C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for 155C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for
116recommended ones. 156recommended ones.
117 157
118See the description of C<ev_embed> watchers for more info. 158See the description of C<ev_embed> watchers for more info.
119 159
120=item ev_set_allocator (void *(*cb)(void *ptr, long size)) 160=item ev_set_allocator (void *(*cb)(void *ptr, size_t size))
121 161
122Sets the allocation function to use (the prototype is similar to the 162Sets the allocation function to use (the prototype and semantics are
123realloc C function, the semantics are identical). It is used to allocate 163identical to the realloc C function). It is used to allocate and free
124and free memory (no surprises here). If it returns zero when memory 164memory (no surprises here). If it returns zero when memory needs to be
125needs to be allocated, the library might abort or take some potentially 165allocated, the library might abort or take some potentially destructive
126destructive action. The default is your system realloc function. 166action. The default is your system realloc function.
127 167
128You could override this function in high-availability programs to, say, 168You could override this function in high-availability programs to, say,
129free some memory if it cannot allocate memory, to use a special allocator, 169free some memory if it cannot allocate memory, to use a special allocator,
130or even to sleep a while and retry until some memory is available. 170or even to sleep a while and retry until some memory is available.
131 171
132Example: replace the libev allocator with one that waits a bit and then 172Example: replace the libev allocator with one that waits a bit and then
133retries: better than mine). 173retries: better than mine).
134 174
135 static void * 175 static void *
136 persistent_realloc (void *ptr, long size) 176 persistent_realloc (void *ptr, size_t size)
137 { 177 {
138 for (;;) 178 for (;;)
139 { 179 {
140 void *newptr = realloc (ptr, size); 180 void *newptr = realloc (ptr, size);
141 181

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines