3 .\" $OpenBSD: event.3,v 1.4 2002/07/12 18:50:48 provos Exp $
5 .\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org>
6 .\" All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
17 .\" 3. The name of the author may not be used to endorse or promote products
18 .\" derived from this software without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 .Nm event_base_dispatch ,
43 .Nm event_base_loopexit ,
44 .Nm event_base_loopbreak ,
52 .Nm event_initialized ,
53 .Nm event_priority_init ,
54 .Nm event_priority_set ,
59 .Nm evtimer_initialized ,
64 .Nm signal_initialized ,
66 .Nm bufferevent_free ,
67 .Nm bufferevent_write ,
68 .Nm bufferevent_write_buffer ,
69 .Nm bufferevent_read ,
70 .Nm bufferevent_enable ,
71 .Nm bufferevent_disable ,
72 .Nm bufferevent_settimeout ,
73 .Nm bufferevent_base_set ,
77 .Nm evbuffer_add_buffer ,
78 .Nm evbuffer_add_printf ,
79 .Nm evbuffer_add_vprintf ,
84 .Nm evbuffer_readline ,
86 .Nm evhttp_bind_socket ,
88 .Nd execute a function when a specific event occurs
90 .Fd #include <sys/time.h>
91 .Fd #include <event.h>
92 .Ft "struct event_base *"
93 .Fn "event_init" "void"
95 .Fn "event_dispatch" "void"
97 .Fn "event_loop" "int flags"
99 .Fn "event_loopexit" "struct timeval *tv"
101 .Fn "event_loopbreak" "void"
103 .Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg"
105 .Fn "event_base_dispatch" "struct event_base *base"
107 .Fn "event_base_loop" "struct event_base *base" "int flags"
109 .Fn "event_base_loopexit" "struct event_base *base" "struct timeval *tv"
111 .Fn "event_base_loopbreak" "struct event_base *base"
113 .Fn "event_base_set" "struct event_base *base" "struct event *"
115 .Fn "event_base_free" "struct event_base *base"
117 .Fn "event_add" "struct event *ev" "struct timeval *tv"
119 .Fn "event_del" "struct event *ev"
121 .Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
123 .Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
125 .Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
127 .Fn "event_initialized" "struct event *ev"
129 .Fn "event_priority_init" "int npriorities"
131 .Fn "event_priority_set" "struct event *ev" "int priority"
133 .Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg"
135 .Fn "evtimer_add" "struct event *ev" "struct timeval *"
137 .Fn "evtimer_del" "struct event *ev"
139 .Fn "evtimer_pending" "struct event *ev" "struct timeval *tv"
141 .Fn "evtimer_initialized" "struct event *ev"
143 .Fn "signal_set" "struct event *ev" "int signal" "void (*fn)(int, short, void *)" "void *arg"
145 .Fn "signal_add" "struct event *ev" "struct timeval *"
147 .Fn "signal_del" "struct event *ev"
149 .Fn "signal_pending" "struct event *ev" "struct timeval *tv"
151 .Fn "signal_initialized" "struct event *ev"
152 .Ft "struct bufferevent *"
153 .Fn "bufferevent_new" "int fd" "evbuffercb readcb" "evbuffercb writecb" "everrorcb" "void *cbarg"
155 .Fn "bufferevent_free" "struct bufferevent *bufev"
157 .Fn "bufferevent_write" "struct bufferevent *bufev" "void *data" "size_t size"
159 .Fn "bufferevent_write_buffer" "struct bufferevent *bufev" "struct evbuffer *buf"
161 .Fn "bufferevent_read" "struct bufferevent *bufev" "void *data" "size_t size"
163 .Fn "bufferevent_enable" "struct bufferevent *bufev" "short event"
165 .Fn "bufferevent_disable" "struct bufferevent *bufev" "short event"
167 .Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write"
169 .Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev"
170 .Ft "struct evbuffer *"
171 .Fn "evbuffer_new" "void"
173 .Fn "evbuffer_free" "struct evbuffer *buf"
175 .Fn "evbuffer_add" "struct evbuffer *buf" "const void *data" "size_t size"
177 .Fn "evbuffer_add_buffer" "struct evbuffer *dst" "struct evbuffer *src"
179 .Fn "evbuffer_add_printf" "struct evbuffer *buf" "const char *fmt" "..."
181 .Fn "evbuffer_add_vprintf" "struct evbuffer *buf" "const char *fmt" "va_list ap"
183 .Fn "evbuffer_drain" "struct evbuffer *buf" "size_t size"
185 .Fn "evbuffer_write" "struct evbuffer *buf" "int fd"
187 .Fn "evbuffer_read" "struct evbuffer *buf" "int fd" "int size"
189 .Fn "evbuffer_find" "struct evbuffer *buf" "const u_char *data" "size_t size"
191 .Fn "evbuffer_readline" "struct evbuffer *buf"
192 .Ft "struct evhttp *"
193 .Fn "evhttp_new" "struct event_base *base"
195 .Fn "evhttp_bind_socket" "struct evhttp *http" "const char *address" "u_short port"
197 .Fn "evhttp_free" "struct evhttp *http"
199 .Fa (*event_sigcb)(void) ;
200 .Ft volatile sig_atomic_t
205 API provides a mechanism to execute a function when a specific event
206 on a file descriptor occurs or after a given time has passed.
210 API needs to be initialized with
212 before it can be used.
214 In order to process events, an application needs to call
216 This function only returns on error, and should replace the event core
217 of the application program.
221 prepares the event structure
223 to be used in future calls to
227 The event will be prepared to call the function specified by the
231 argument indicating the file descriptor, a
233 argument indicating the type of event, and a
235 argument given in the
240 indicates the file descriptor that should be monitored for events.
241 The events can be either
245 indicating that an application can read or write from the file descriptor
246 respectively without blocking.
250 will be called with the file descriptor that triggered the event and
251 the type of event which will be either
257 Additionally, an event which has registered interest in more than one of the
258 preceeding events, via bitwise-OR to
260 can provide its callback function with a bitwise-OR of more than one triggered
270 Once initialized, the
272 structure can be used repeatedly with
276 and does not need to be reinitialized unless the function called and/or
277 the argument to it are to be changed.
280 structure has been added to libevent using
282 the structure must persist until the event occurs (assuming
284 is not set) or is removed
287 You may not reuse the same
289 structure for multiple monitored descriptors; each descriptor
295 schedules the execution of the
297 event when the event specified in
299 occurs or in at least the time specified in the
305 no timeout occurs and the function will only be called
306 if a matching event occurs on the file descriptor.
309 argument must be already initialized by
311 and may not be used in calls to
313 until it has timed out or been removed with
317 argument already has a scheduled timeout, the old timeout will be
318 replaced by the new one.
322 will cancel the event in the argument
324 If the event has already executed or has never been added
325 the call will have no effect.
331 .Fn evtimer_initialized ,
334 are abbreviations for common situations where only a timeout is required.
335 The file descriptor passed will be \-1, and the event type will be
342 .Fn signal_initialized ,
346 The event type will be a persistent
353 In order to avoid races in signal handlers, the
355 API provides two variables:
362 to indicate that a signal has been received.
365 to a callback function.
366 After the signal handler sets
369 will execute the callback function to process received signals.
370 The callback returns 1 when no events are registered any more.
371 It can return \-1 to indicate an error to the
384 However, it schedules a callback to be called exactly once and does not
385 require the caller to prepare an
388 This function supports
396 function can be used to check if the event specified by
405 the expiration time of the event will be returned in
409 .Fn event_initialized
410 macro can be used to check if an event has been initialized.
414 function provides an interface for single pass execution of pending
423 function exits from the event loop. The next
426 given timer expires will complete normally (handling all queued events) then
427 exit without blocking for events again. Subsequent invocations of
429 will proceed normally.
432 function exits from the event loop immediately.
434 will abort after the next event is completed;
436 is typically invoked from this event's callback. This behavior is analogous
437 to the "break;" statement. Subsequent invocations of
439 will proceed normally.
441 It is the responsibility of the caller to provide these functions with
442 pre-allocated event structures.
447 schedules all active events with the same priority.
448 However, sometimes it is desirable to process some events with a higher
449 priority than others.
452 supports strict priority queues.
453 Active events with a lower priority are always processed before events
454 with a higher priority.
456 The number of different priorities can be set initially with the
457 .Fn event_priority_init
459 This function should be called before the first call to
462 .Fn event_priority_set
463 function can be used to assign a priority to an event.
466 assigns the middle priority to all events unless their priority
468 .Sh THREAD SAFE EVENTS
470 has experimental support for thread-safe events.
471 When initializing the library via
473 an event base is returned.
474 This event base can be used in conjunction with calls to
476 .Fn event_base_dispatch ,
477 .Fn event_base_loop ,
478 .Fn event_base_loopexit ,
479 .Fn bufferevent_base_set
481 .Fn event_base_free .
483 should be called after preparing an event with
487 assigns the provided event to the most recently created event base.
488 .Fn bufferevent_base_set
489 should be called after preparing a bufferevent with
490 .Fn bufferevent_new .
492 should be used to free memory associated with the event base
493 when it is no longer needed.
496 provides an abstraction on top of the regular event callbacks.
497 This abstraction is called a
498 .Va "buffered event" .
499 A buffered event provides input and output buffers that get filled
500 and drained automatically.
501 The user of a buffered event no longer deals directly with the IO,
502 but instead is reading from input and writing to output buffers.
504 A new bufferevent is created by
505 .Fn bufferevent_new .
508 specifies the file descriptor from which data is read and written to.
509 This file descriptor is not allowed to be a
511 The next three parameters are callbacks.
512 The read and write callback have the following form:
514 .Fn "(*cb)" "struct bufferevent *bufev" "void *arg" .
515 The error callback has the following form:
517 .Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" .
518 The argument is specified by the fourth parameter
521 .Fa bufferevent struct
522 pointer is returned on success, NULL on error.
523 Both the read and the write callback may be NULL.
524 The error callback has to be always provided.
526 Once initialized, the bufferevent structure can be used repeatedly with
527 bufferevent_enable() and bufferevent_disable().
528 The flags parameter can be a combination of
532 When read enabled the bufferevent will try to read from the file
533 descriptor and call the read callback.
534 The write callback is executed
535 whenever the output buffer is drained below the write low watermark,
541 .Fn bufferevent_write
542 function can be used to write data to the file descriptor.
543 The data is appended to the output buffer and written to the descriptor
544 automatically as it becomes available for writing.
545 .Fn bufferevent_write
546 returns 0 on success or \-1 on failure.
549 function is used to read data from the input buffer,
550 returning the amount of data read.
552 If multiple bases are in use, bufferevent_base_set() must be called before
553 enabling the bufferevent for the first time.
554 .Sh NON-BLOCKING HTTP SUPPORT
556 provides a very thin HTTP layer that can be used both to host an HTTP
557 server and also to make HTTP requests.
558 An HTTP server can be created by calling
560 It can be bound to any port and address with the
561 .Fn evhttp_bind_socket
563 When the HTTP server is no longer used, it can be freed via
566 To be notified of HTTP requests, a user needs to register callbacks with the
568 This can be done by calling
570 The second argument is the URI for which a callback is being registered.
571 The corresponding callback will receive an
572 .Va struct evhttp_request
573 object that contains all information about the request.
575 This section does not document all the possible function calls; please
578 for the public interfaces.
580 It is possible to disable support for
581 .Va epoll , kqueue , devpoll , poll
584 by setting the environment variable
585 .Va EVENT_NOEPOLL , EVENT_NOKQUEUE , EVENT_NODEVPOLL , EVENT_NOPOLL
589 By setting the environment variable
590 .Va EVENT_SHOW_METHOD ,
592 displays the kernel notification method that it uses.
594 Upon successful completion
599 Otherwise, \-1 is returned and the global variable errno is
600 set to indicate the error.
610 API manpage is based on the
612 manpage by Artur Grabowski.
615 to Windows is due to Michael A. Davis.
616 Support for real-time signals is due to Taral.
620 library was written by Niels Provos.
622 This documentation is neither complete nor authoritative.
623 If you are in doubt about the usage of this API then
624 check the source code to find out how it works, write
625 up the missing piece of documentation and send it to
626 me for inclusion in this man page.