1c43e99fdSEd Maste /* 2c43e99fdSEd Maste * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 3c43e99fdSEd Maste * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4c43e99fdSEd Maste * 5c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without 6c43e99fdSEd Maste * modification, are permitted provided that the following conditions 7c43e99fdSEd Maste * are met: 8c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright 9c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer. 10c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright 11c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the 12c43e99fdSEd Maste * documentation and/or other materials provided with the distribution. 13c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products 14c43e99fdSEd Maste * derived from this software without specific prior written permission. 15c43e99fdSEd Maste * 16c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26c43e99fdSEd Maste */ 27c43e99fdSEd Maste #ifndef EVENT2_EVENT_H_INCLUDED_ 28c43e99fdSEd Maste #define EVENT2_EVENT_H_INCLUDED_ 29c43e99fdSEd Maste 30c43e99fdSEd Maste /** 31c43e99fdSEd Maste @mainpage 32c43e99fdSEd Maste 33c43e99fdSEd Maste @section intro Introduction 34c43e99fdSEd Maste 35c43e99fdSEd Maste Libevent is an event notification library for developing scalable network 36c43e99fdSEd Maste servers. The Libevent API provides a mechanism to execute a callback 37c43e99fdSEd Maste function when a specific event occurs on a file descriptor or after a 38c43e99fdSEd Maste timeout has been reached. Furthermore, Libevent also support callbacks due 39c43e99fdSEd Maste to signals or regular timeouts. 40c43e99fdSEd Maste 41c43e99fdSEd Maste Libevent is meant to replace the event loop found in event driven network 42c43e99fdSEd Maste servers. An application just needs to call event_base_dispatch() and then add or 43c43e99fdSEd Maste remove events dynamically without having to change the event loop. 44c43e99fdSEd Maste 45c43e99fdSEd Maste 46c43e99fdSEd Maste Currently, Libevent supports /dev/poll, kqueue(2), select(2), poll(2), 47c43e99fdSEd Maste epoll(4), and evports. The internal event mechanism is completely 48c43e99fdSEd Maste independent of the exposed event API, and a simple update of Libevent can 49c43e99fdSEd Maste provide new functionality without having to redesign the applications. As a 50c43e99fdSEd Maste result, Libevent allows for portable application development and provides 51c43e99fdSEd Maste the most scalable event notification mechanism available on an operating 52c43e99fdSEd Maste system. Libevent can also be used for multithreaded programs. Libevent 53c43e99fdSEd Maste should compile on Linux, *BSD, Mac OS X, Solaris and, Windows. 54c43e99fdSEd Maste 55c43e99fdSEd Maste @section usage Standard usage 56c43e99fdSEd Maste 57c43e99fdSEd Maste Every program that uses Libevent must include the <event2/event.h> 58c43e99fdSEd Maste header, and pass the -levent flag to the linker. (You can instead link 59c43e99fdSEd Maste -levent_core if you only want the main event and buffered IO-based code, 60c43e99fdSEd Maste and don't want to link any protocol code.) 61c43e99fdSEd Maste 62c43e99fdSEd Maste @section setup Library setup 63c43e99fdSEd Maste 64c43e99fdSEd Maste Before you call any other Libevent functions, you need to set up the 65c43e99fdSEd Maste library. If you're going to use Libevent from multiple threads in a 66c43e99fdSEd Maste multithreaded application, you need to initialize thread support -- 67c43e99fdSEd Maste typically by using evthread_use_pthreads() or 68c43e99fdSEd Maste evthread_use_windows_threads(). See <event2/thread.h> for more 69c43e99fdSEd Maste information. 70c43e99fdSEd Maste 71c43e99fdSEd Maste This is also the point where you can replace Libevent's memory 72c43e99fdSEd Maste management functions with event_set_mem_functions, and enable debug mode 73c43e99fdSEd Maste with event_enable_debug_mode(). 74c43e99fdSEd Maste 75c43e99fdSEd Maste @section base Creating an event base 76c43e99fdSEd Maste 77c43e99fdSEd Maste Next, you need to create an event_base structure, using event_base_new() 78c43e99fdSEd Maste or event_base_new_with_config(). The event_base is responsible for 79c43e99fdSEd Maste keeping track of which events are "pending" (that is to say, being 80c43e99fdSEd Maste watched to see if they become active) and which events are "active". 81c43e99fdSEd Maste Every event is associated with a single event_base. 82c43e99fdSEd Maste 83c43e99fdSEd Maste @section event Event notification 84c43e99fdSEd Maste 85c43e99fdSEd Maste For each file descriptor that you wish to monitor, you must create an 86c43e99fdSEd Maste event structure with event_new(). (You may also declare an event 87c43e99fdSEd Maste structure and call event_assign() to initialize the members of the 88c43e99fdSEd Maste structure.) To enable notification, you add the structure to the list 89c43e99fdSEd Maste of monitored events by calling event_add(). The event structure must 90c43e99fdSEd Maste remain allocated as long as it is active, so it should generally be 91c43e99fdSEd Maste allocated on the heap. 92c43e99fdSEd Maste 93c43e99fdSEd Maste @section loop Dispatching events. 94c43e99fdSEd Maste 95c43e99fdSEd Maste Finally, you call event_base_dispatch() to loop and dispatch events. 96c43e99fdSEd Maste You can also use event_base_loop() for more fine-grained control. 97c43e99fdSEd Maste 98c43e99fdSEd Maste Currently, only one thread can be dispatching a given event_base at a 99c43e99fdSEd Maste time. If you want to run events in multiple threads at once, you can 100c43e99fdSEd Maste either have a single event_base whose events add work to a work queue, 101c43e99fdSEd Maste or you can create multiple event_base objects. 102c43e99fdSEd Maste 103c43e99fdSEd Maste @section bufferevent I/O Buffers 104c43e99fdSEd Maste 105c43e99fdSEd Maste Libevent provides a buffered I/O abstraction on top of the regular event 106c43e99fdSEd Maste callbacks. This abstraction is called a bufferevent. A bufferevent 107c43e99fdSEd Maste provides input and output buffers that get filled and drained 108c43e99fdSEd Maste automatically. The user of a buffered event no longer deals directly 109c43e99fdSEd Maste with the I/O, but instead is reading from input and writing to output 110c43e99fdSEd Maste buffers. 111c43e99fdSEd Maste 112c43e99fdSEd Maste Once initialized via bufferevent_socket_new(), the bufferevent structure 113c43e99fdSEd Maste can be used repeatedly with bufferevent_enable() and 114c43e99fdSEd Maste bufferevent_disable(). Instead of reading and writing directly to a 115c43e99fdSEd Maste socket, you would call bufferevent_read() and bufferevent_write(). 116c43e99fdSEd Maste 117c43e99fdSEd Maste When read enabled the bufferevent will try to read from the file descriptor 118c43e99fdSEd Maste and call the read callback. The write callback is executed whenever the 119c43e99fdSEd Maste output buffer is drained below the write low watermark, which is 0 by 120c43e99fdSEd Maste default. 121c43e99fdSEd Maste 122c43e99fdSEd Maste See <event2/bufferevent*.h> for more information. 123c43e99fdSEd Maste 124c43e99fdSEd Maste @section timers Timers 125c43e99fdSEd Maste 126c43e99fdSEd Maste Libevent can also be used to create timers that invoke a callback after a 127c43e99fdSEd Maste certain amount of time has expired. The evtimer_new() macro returns 128c43e99fdSEd Maste an event struct to use as a timer. To activate the timer, call 129c43e99fdSEd Maste evtimer_add(). Timers can be deactivated by calling evtimer_del(). 130c43e99fdSEd Maste (These macros are thin wrappers around event_new(), event_add(), 131c43e99fdSEd Maste and event_del(); you can also use those instead.) 132c43e99fdSEd Maste 133c43e99fdSEd Maste @section evdns Asynchronous DNS resolution 134c43e99fdSEd Maste 135c43e99fdSEd Maste Libevent provides an asynchronous DNS resolver that should be used instead 136c43e99fdSEd Maste of the standard DNS resolver functions. See the <event2/dns.h> 137c43e99fdSEd Maste functions for more detail. 138c43e99fdSEd Maste 139c43e99fdSEd Maste @section evhttp Event-driven HTTP servers 140c43e99fdSEd Maste 141c43e99fdSEd Maste Libevent provides a very simple event-driven HTTP server that can be 142c43e99fdSEd Maste embedded in your program and used to service HTTP requests. 143c43e99fdSEd Maste 144c43e99fdSEd Maste To use this capability, you need to include the <event2/http.h> header in your 145c43e99fdSEd Maste program. See that header for more information. 146c43e99fdSEd Maste 147c43e99fdSEd Maste @section evrpc A framework for RPC servers and clients 148c43e99fdSEd Maste 149c43e99fdSEd Maste Libevent provides a framework for creating RPC servers and clients. It 150c43e99fdSEd Maste takes care of marshaling and unmarshaling all data structures. 151c43e99fdSEd Maste 152c43e99fdSEd Maste @section api API Reference 153c43e99fdSEd Maste 154c43e99fdSEd Maste To browse the complete documentation of the libevent API, click on any of 155c43e99fdSEd Maste the following links. 156c43e99fdSEd Maste 157c43e99fdSEd Maste event2/event.h 158c43e99fdSEd Maste The primary libevent header 159c43e99fdSEd Maste 160c43e99fdSEd Maste event2/thread.h 161c43e99fdSEd Maste Functions for use by multithreaded programs 162c43e99fdSEd Maste 163c43e99fdSEd Maste event2/buffer.h and event2/bufferevent.h 164c43e99fdSEd Maste Buffer management for network reading and writing 165c43e99fdSEd Maste 166c43e99fdSEd Maste event2/util.h 167c43e99fdSEd Maste Utility functions for portable nonblocking network code 168c43e99fdSEd Maste 169c43e99fdSEd Maste event2/dns.h 170c43e99fdSEd Maste Asynchronous DNS resolution 171c43e99fdSEd Maste 172c43e99fdSEd Maste event2/http.h 173c43e99fdSEd Maste An embedded libevent-based HTTP server 174c43e99fdSEd Maste 175c43e99fdSEd Maste event2/rpc.h 176c43e99fdSEd Maste A framework for creating RPC servers and clients 177c43e99fdSEd Maste 178c43e99fdSEd Maste */ 179c43e99fdSEd Maste 180c43e99fdSEd Maste /** @file event2/event.h 181c43e99fdSEd Maste 182c43e99fdSEd Maste Core functions for waiting for and receiving events, and using event bases. 183c43e99fdSEd Maste */ 184c43e99fdSEd Maste 185c43e99fdSEd Maste #include <event2/visibility.h> 186c43e99fdSEd Maste 187c43e99fdSEd Maste #ifdef __cplusplus 188c43e99fdSEd Maste extern "C" { 189c43e99fdSEd Maste #endif 190c43e99fdSEd Maste 191c43e99fdSEd Maste #include <event2/event-config.h> 192c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TYPES_H 193c43e99fdSEd Maste #include <sys/types.h> 194c43e99fdSEd Maste #endif 195c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H 196c43e99fdSEd Maste #include <sys/time.h> 197c43e99fdSEd Maste #endif 198c43e99fdSEd Maste 199c43e99fdSEd Maste #include <stdio.h> 200c43e99fdSEd Maste 201c43e99fdSEd Maste /* For int types. */ 202c43e99fdSEd Maste #include <event2/util.h> 203c43e99fdSEd Maste 204c43e99fdSEd Maste /** 205c43e99fdSEd Maste * Structure to hold information and state for a Libevent dispatch loop. 206c43e99fdSEd Maste * 207c43e99fdSEd Maste * The event_base lies at the center of Libevent; every application will 208c43e99fdSEd Maste * have one. It keeps track of all pending and active events, and 209c43e99fdSEd Maste * notifies your application of the active ones. 210c43e99fdSEd Maste * 211c43e99fdSEd Maste * This is an opaque structure; you can allocate one using 212c43e99fdSEd Maste * event_base_new() or event_base_new_with_config(). 213c43e99fdSEd Maste * 214c43e99fdSEd Maste * @see event_base_new(), event_base_free(), event_base_loop(), 215c43e99fdSEd Maste * event_base_new_with_config() 216c43e99fdSEd Maste */ 217c43e99fdSEd Maste struct event_base 218c43e99fdSEd Maste #ifdef EVENT_IN_DOXYGEN_ 219c43e99fdSEd Maste {/*Empty body so that doxygen will generate documentation here.*/} 220c43e99fdSEd Maste #endif 221c43e99fdSEd Maste ; 222c43e99fdSEd Maste 223c43e99fdSEd Maste /** 224c43e99fdSEd Maste * @struct event 225c43e99fdSEd Maste * 226c43e99fdSEd Maste * Structure to represent a single event. 227c43e99fdSEd Maste * 228c43e99fdSEd Maste * An event can have some underlying condition it represents: a socket 229c43e99fdSEd Maste * becoming readable or writeable (or both), or a signal becoming raised. 230c43e99fdSEd Maste * (An event that represents no underlying condition is still useful: you 231c43e99fdSEd Maste * can use one to implement a timer, or to communicate between threads.) 232c43e99fdSEd Maste * 233c43e99fdSEd Maste * Generally, you can create events with event_new(), then make them 234c43e99fdSEd Maste * pending with event_add(). As your event_base runs, it will run the 235*b50261e2SCy Schubert * callbacks of an events whose conditions are triggered. When you no 236c43e99fdSEd Maste * longer want the event, free it with event_free(). 237c43e99fdSEd Maste * 238c43e99fdSEd Maste * In more depth: 239c43e99fdSEd Maste * 240c43e99fdSEd Maste * An event may be "pending" (one whose condition we are watching), 241c43e99fdSEd Maste * "active" (one whose condition has triggered and whose callback is about 242c43e99fdSEd Maste * to run), neither, or both. Events come into existence via 243c43e99fdSEd Maste * event_assign() or event_new(), and are then neither active nor pending. 244c43e99fdSEd Maste * 245c43e99fdSEd Maste * To make an event pending, pass it to event_add(). When doing so, you 246c43e99fdSEd Maste * can also set a timeout for the event. 247c43e99fdSEd Maste * 248c43e99fdSEd Maste * Events become active during an event_base_loop() call when either their 249c43e99fdSEd Maste * condition has triggered, or when their timeout has elapsed. You can 250c43e99fdSEd Maste * also activate an event manually using event_active(). The even_base 251c43e99fdSEd Maste * loop will run the callbacks of active events; after it has done so, it 252c43e99fdSEd Maste * marks them as no longer active. 253c43e99fdSEd Maste * 254c43e99fdSEd Maste * You can make an event non-pending by passing it to event_del(). This 255c43e99fdSEd Maste * also makes the event non-active. 256c43e99fdSEd Maste * 257c43e99fdSEd Maste * Events can be "persistent" or "non-persistent". A non-persistent event 258c43e99fdSEd Maste * becomes non-pending as soon as it is triggered: thus, it only runs at 259c43e99fdSEd Maste * most once per call to event_add(). A persistent event remains pending 260c43e99fdSEd Maste * even when it becomes active: you'll need to event_del() it manually in 261c43e99fdSEd Maste * order to make it non-pending. When a persistent event with a timeout 262c43e99fdSEd Maste * becomes active, its timeout is reset: this means you can use persistent 263c43e99fdSEd Maste * events to implement periodic timeouts. 264c43e99fdSEd Maste * 265c43e99fdSEd Maste * This should be treated as an opaque structure; you should never read or 266c43e99fdSEd Maste * write any of its fields directly. For backward compatibility with old 267c43e99fdSEd Maste * code, it is defined in the event2/event_struct.h header; including this 268c43e99fdSEd Maste * header may make your code incompatible with other versions of Libevent. 269c43e99fdSEd Maste * 270c43e99fdSEd Maste * @see event_new(), event_free(), event_assign(), event_get_assignment(), 271c43e99fdSEd Maste * event_add(), event_del(), event_active(), event_pending(), 272c43e99fdSEd Maste * event_get_fd(), event_get_base(), event_get_events(), 273c43e99fdSEd Maste * event_get_callback(), event_get_callback_arg(), 274c43e99fdSEd Maste * event_priority_set() 275c43e99fdSEd Maste */ 276c43e99fdSEd Maste struct event 277c43e99fdSEd Maste #ifdef EVENT_IN_DOXYGEN_ 278c43e99fdSEd Maste {/*Empty body so that doxygen will generate documentation here.*/} 279c43e99fdSEd Maste #endif 280c43e99fdSEd Maste ; 281c43e99fdSEd Maste 282c43e99fdSEd Maste /** 283c43e99fdSEd Maste * Configuration for an event_base. 284c43e99fdSEd Maste * 285c43e99fdSEd Maste * There are many options that can be used to alter the behavior and 286c43e99fdSEd Maste * implementation of an event_base. To avoid having to pass them all in a 287c43e99fdSEd Maste * complex many-argument constructor, we provide an abstract data type 288*b50261e2SCy Schubert * where you set up configuration information before passing it to 289c43e99fdSEd Maste * event_base_new_with_config(). 290c43e99fdSEd Maste * 291c43e99fdSEd Maste * @see event_config_new(), event_config_free(), event_base_new_with_config(), 292c43e99fdSEd Maste * event_config_avoid_method(), event_config_require_features(), 293c43e99fdSEd Maste * event_config_set_flag(), event_config_set_num_cpus_hint() 294c43e99fdSEd Maste */ 295c43e99fdSEd Maste struct event_config 296c43e99fdSEd Maste #ifdef EVENT_IN_DOXYGEN_ 297c43e99fdSEd Maste {/*Empty body so that doxygen will generate documentation here.*/} 298c43e99fdSEd Maste #endif 299c43e99fdSEd Maste ; 300c43e99fdSEd Maste 301c43e99fdSEd Maste /** 302c43e99fdSEd Maste * Enable some relatively expensive debugging checks in Libevent that 303c43e99fdSEd Maste * would normally be turned off. Generally, these checks cause code that 304c43e99fdSEd Maste * would otherwise crash mysteriously to fail earlier with an assertion 305c43e99fdSEd Maste * failure. Note that this method MUST be called before any events or 306c43e99fdSEd Maste * event_bases have been created. 307c43e99fdSEd Maste * 308c43e99fdSEd Maste * Debug mode can currently catch the following errors: 309c43e99fdSEd Maste * An event is re-assigned while it is added 310c43e99fdSEd Maste * Any function is called on a non-assigned event 311c43e99fdSEd Maste * 312c43e99fdSEd Maste * Note that debugging mode uses memory to track every event that has been 313c43e99fdSEd Maste * initialized (via event_assign, event_set, or event_new) but not yet 314c43e99fdSEd Maste * released (via event_free or event_debug_unassign). If you want to use 315c43e99fdSEd Maste * debug mode, and you find yourself running out of memory, you will need 316c43e99fdSEd Maste * to use event_debug_unassign to explicitly stop tracking events that 317c43e99fdSEd Maste * are no longer considered set-up. 318c43e99fdSEd Maste * 319c43e99fdSEd Maste * @see event_debug_unassign() 320c43e99fdSEd Maste */ 321c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 322c43e99fdSEd Maste void event_enable_debug_mode(void); 323c43e99fdSEd Maste 324c43e99fdSEd Maste /** 325c43e99fdSEd Maste * When debugging mode is enabled, informs Libevent that an event should no 326c43e99fdSEd Maste * longer be considered as assigned. When debugging mode is not enabled, does 327c43e99fdSEd Maste * nothing. 328c43e99fdSEd Maste * 329c43e99fdSEd Maste * This function must only be called on a non-added event. 330c43e99fdSEd Maste * 331c43e99fdSEd Maste * @see event_enable_debug_mode() 332c43e99fdSEd Maste */ 333c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 334c43e99fdSEd Maste void event_debug_unassign(struct event *); 335c43e99fdSEd Maste 336c43e99fdSEd Maste /** 337c43e99fdSEd Maste * Create and return a new event_base to use with the rest of Libevent. 338c43e99fdSEd Maste * 339c43e99fdSEd Maste * @return a new event_base on success, or NULL on failure. 340c43e99fdSEd Maste * 341c43e99fdSEd Maste * @see event_base_free(), event_base_new_with_config() 342c43e99fdSEd Maste */ 343c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 344c43e99fdSEd Maste struct event_base *event_base_new(void); 345c43e99fdSEd Maste 346c43e99fdSEd Maste /** 347c43e99fdSEd Maste Reinitialize the event base after a fork 348c43e99fdSEd Maste 349c43e99fdSEd Maste Some event mechanisms do not survive across fork. The event base needs 350c43e99fdSEd Maste to be reinitialized with the event_reinit() function. 351c43e99fdSEd Maste 352c43e99fdSEd Maste @param base the event base that needs to be re-initialized 353c43e99fdSEd Maste @return 0 if successful, or -1 if some events could not be re-added. 354c43e99fdSEd Maste @see event_base_new() 355c43e99fdSEd Maste */ 356c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 357c43e99fdSEd Maste int event_reinit(struct event_base *base); 358c43e99fdSEd Maste 359c43e99fdSEd Maste /** 360c43e99fdSEd Maste Event dispatching loop 361c43e99fdSEd Maste 362c43e99fdSEd Maste This loop will run the event base until either there are no more pending or 363c43e99fdSEd Maste active, or until something calls event_base_loopbreak() or 364c43e99fdSEd Maste event_base_loopexit(). 365c43e99fdSEd Maste 366c43e99fdSEd Maste @param base the event_base structure returned by event_base_new() or 367c43e99fdSEd Maste event_base_new_with_config() 368c43e99fdSEd Maste @return 0 if successful, -1 if an error occurred, or 1 if we exited because 369c43e99fdSEd Maste no events were pending or active. 370c43e99fdSEd Maste @see event_base_loop() 371c43e99fdSEd Maste */ 372c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 373c43e99fdSEd Maste int event_base_dispatch(struct event_base *); 374c43e99fdSEd Maste 375c43e99fdSEd Maste /** 376c43e99fdSEd Maste Get the kernel event notification mechanism used by Libevent. 377c43e99fdSEd Maste 378c43e99fdSEd Maste @param eb the event_base structure returned by event_base_new() 379c43e99fdSEd Maste @return a string identifying the kernel event mechanism (kqueue, epoll, etc.) 380c43e99fdSEd Maste */ 381c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 382c43e99fdSEd Maste const char *event_base_get_method(const struct event_base *); 383c43e99fdSEd Maste 384c43e99fdSEd Maste /** 385c43e99fdSEd Maste Gets all event notification mechanisms supported by Libevent. 386c43e99fdSEd Maste 387c43e99fdSEd Maste This functions returns the event mechanism in order preferred by 388c43e99fdSEd Maste Libevent. Note that this list will include all backends that 389c43e99fdSEd Maste Libevent has compiled-in support for, and will not necessarily check 390c43e99fdSEd Maste your OS to see whether it has the required resources. 391c43e99fdSEd Maste 392c43e99fdSEd Maste @return an array with pointers to the names of support methods. 393c43e99fdSEd Maste The end of the array is indicated by a NULL pointer. If an 394c43e99fdSEd Maste error is encountered NULL is returned. 395c43e99fdSEd Maste */ 396c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 397c43e99fdSEd Maste const char **event_get_supported_methods(void); 398c43e99fdSEd Maste 399c43e99fdSEd Maste /** Query the current monotonic time from a the timer for a struct 400c43e99fdSEd Maste * event_base. 401c43e99fdSEd Maste */ 402c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 403c43e99fdSEd Maste int event_gettime_monotonic(struct event_base *base, struct timeval *tp); 404c43e99fdSEd Maste 405c43e99fdSEd Maste /** 406c43e99fdSEd Maste @name event type flag 407c43e99fdSEd Maste 408c43e99fdSEd Maste Flags to pass to event_base_get_num_events() to specify the kinds of events 409c43e99fdSEd Maste we want to aggregate counts for 410c43e99fdSEd Maste */ 411c43e99fdSEd Maste /**@{*/ 412c43e99fdSEd Maste /** count the number of active events, which have been triggered.*/ 413c43e99fdSEd Maste #define EVENT_BASE_COUNT_ACTIVE 1U 414c43e99fdSEd Maste /** count the number of virtual events, which is used to represent an internal 415c43e99fdSEd Maste * condition, other than a pending event, that keeps the loop from exiting. */ 416c43e99fdSEd Maste #define EVENT_BASE_COUNT_VIRTUAL 2U 417c43e99fdSEd Maste /** count the number of events which have been added to event base, including 418c43e99fdSEd Maste * internal events. */ 419c43e99fdSEd Maste #define EVENT_BASE_COUNT_ADDED 4U 420c43e99fdSEd Maste /**@}*/ 421c43e99fdSEd Maste 422c43e99fdSEd Maste /** 423c43e99fdSEd Maste Gets the number of events in event_base, as specified in the flags. 424c43e99fdSEd Maste 425c43e99fdSEd Maste Since event base has some internal events added to make some of its 426c43e99fdSEd Maste functionalities work, EVENT_BASE_COUNT_ADDED may return more than the 427c43e99fdSEd Maste number of events you added using event_add(). 428c43e99fdSEd Maste 429c43e99fdSEd Maste If you pass EVENT_BASE_COUNT_ACTIVE and EVENT_BASE_COUNT_ADDED together, an 430c43e99fdSEd Maste active event will be counted twice. However, this might not be the case in 431c43e99fdSEd Maste future libevent versions. The return value is an indication of the work 432c43e99fdSEd Maste load, but the user shouldn't rely on the exact value as this may change in 433c43e99fdSEd Maste the future. 434c43e99fdSEd Maste 435c43e99fdSEd Maste @param eb the event_base structure returned by event_base_new() 436c43e99fdSEd Maste @param flags a bitwise combination of the kinds of events to aggregate 437c43e99fdSEd Maste counts for 438c43e99fdSEd Maste @return the number of events specified in the flags 439c43e99fdSEd Maste */ 440c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 441c43e99fdSEd Maste int event_base_get_num_events(struct event_base *, unsigned int); 442c43e99fdSEd Maste 443c43e99fdSEd Maste /** 444c43e99fdSEd Maste Get the maximum number of events in a given event_base as specified in the 445c43e99fdSEd Maste flags. 446c43e99fdSEd Maste 447c43e99fdSEd Maste @param eb the event_base structure returned by event_base_new() 448c43e99fdSEd Maste @param flags a bitwise combination of the kinds of events to aggregate 449c43e99fdSEd Maste counts for 450c43e99fdSEd Maste @param clear option used to reset the maximum count. 451c43e99fdSEd Maste @return the number of events specified in the flags 452c43e99fdSEd Maste */ 453c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 454c43e99fdSEd Maste int event_base_get_max_events(struct event_base *, unsigned int, int); 455c43e99fdSEd Maste 456c43e99fdSEd Maste /** 457c43e99fdSEd Maste Allocates a new event configuration object. 458c43e99fdSEd Maste 459c43e99fdSEd Maste The event configuration object can be used to change the behavior of 460c43e99fdSEd Maste an event base. 461c43e99fdSEd Maste 462c43e99fdSEd Maste @return an event_config object that can be used to store configuration, or 463c43e99fdSEd Maste NULL if an error is encountered. 464c43e99fdSEd Maste @see event_base_new_with_config(), event_config_free(), event_config 465c43e99fdSEd Maste */ 466c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 467c43e99fdSEd Maste struct event_config *event_config_new(void); 468c43e99fdSEd Maste 469c43e99fdSEd Maste /** 470c43e99fdSEd Maste Deallocates all memory associated with an event configuration object 471c43e99fdSEd Maste 472c43e99fdSEd Maste @param cfg the event configuration object to be freed. 473c43e99fdSEd Maste */ 474c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 475c43e99fdSEd Maste void event_config_free(struct event_config *cfg); 476c43e99fdSEd Maste 477c43e99fdSEd Maste /** 478c43e99fdSEd Maste Enters an event method that should be avoided into the configuration. 479c43e99fdSEd Maste 480c43e99fdSEd Maste This can be used to avoid event mechanisms that do not support certain 481c43e99fdSEd Maste file descriptor types, or for debugging to avoid certain event 482c43e99fdSEd Maste mechanisms. An application can make use of multiple event bases to 483c43e99fdSEd Maste accommodate incompatible file descriptor types. 484c43e99fdSEd Maste 485c43e99fdSEd Maste @param cfg the event configuration object 486c43e99fdSEd Maste @param method the name of the event method to avoid 487c43e99fdSEd Maste @return 0 on success, -1 on failure. 488c43e99fdSEd Maste */ 489c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 490c43e99fdSEd Maste int event_config_avoid_method(struct event_config *cfg, const char *method); 491c43e99fdSEd Maste 492c43e99fdSEd Maste /** 493c43e99fdSEd Maste A flag used to describe which features an event_base (must) provide. 494c43e99fdSEd Maste 495c43e99fdSEd Maste Because of OS limitations, not every Libevent backend supports every 496c43e99fdSEd Maste possible feature. You can use this type with 497c43e99fdSEd Maste event_config_require_features() to tell Libevent to only proceed if your 498c43e99fdSEd Maste event_base implements a given feature, and you can receive this type from 499c43e99fdSEd Maste event_base_get_features() to see which features are available. 500c43e99fdSEd Maste */ 501c43e99fdSEd Maste enum event_method_feature { 502c43e99fdSEd Maste /** Require an event method that allows edge-triggered events with EV_ET. */ 503c43e99fdSEd Maste EV_FEATURE_ET = 0x01, 504c43e99fdSEd Maste /** Require an event method where having one event triggered among 505c43e99fdSEd Maste * many is [approximately] an O(1) operation. This excludes (for 506c43e99fdSEd Maste * example) select and poll, which are approximately O(N) for N 507c43e99fdSEd Maste * equal to the total number of possible events. */ 508c43e99fdSEd Maste EV_FEATURE_O1 = 0x02, 509c43e99fdSEd Maste /** Require an event method that allows file descriptors as well as 510c43e99fdSEd Maste * sockets. */ 511c43e99fdSEd Maste EV_FEATURE_FDS = 0x04, 512c43e99fdSEd Maste /** Require an event method that allows you to use EV_CLOSED to detect 513c43e99fdSEd Maste * connection close without the necessity of reading all the pending data. 514c43e99fdSEd Maste * 515c43e99fdSEd Maste * Methods that do support EV_CLOSED may not be able to provide support on 516c43e99fdSEd Maste * all kernel versions. 517c43e99fdSEd Maste **/ 518c43e99fdSEd Maste EV_FEATURE_EARLY_CLOSE = 0x08 519c43e99fdSEd Maste }; 520c43e99fdSEd Maste 521c43e99fdSEd Maste /** 522c43e99fdSEd Maste A flag passed to event_config_set_flag(). 523c43e99fdSEd Maste 524c43e99fdSEd Maste These flags change the behavior of an allocated event_base. 525c43e99fdSEd Maste 526c43e99fdSEd Maste @see event_config_set_flag(), event_base_new_with_config(), 527c43e99fdSEd Maste event_method_feature 528c43e99fdSEd Maste */ 529c43e99fdSEd Maste enum event_base_config_flag { 530c43e99fdSEd Maste /** Do not allocate a lock for the event base, even if we have 531c43e99fdSEd Maste locking set up. 532c43e99fdSEd Maste 533c43e99fdSEd Maste Setting this option will make it unsafe and nonfunctional to call 534c43e99fdSEd Maste functions on the base concurrently from multiple threads. 535c43e99fdSEd Maste */ 536c43e99fdSEd Maste EVENT_BASE_FLAG_NOLOCK = 0x01, 537c43e99fdSEd Maste /** Do not check the EVENT_* environment variables when configuring 538c43e99fdSEd Maste an event_base */ 539c43e99fdSEd Maste EVENT_BASE_FLAG_IGNORE_ENV = 0x02, 540c43e99fdSEd Maste /** Windows only: enable the IOCP dispatcher at startup 541c43e99fdSEd Maste 542c43e99fdSEd Maste If this flag is set then bufferevent_socket_new() and 543c43e99fdSEd Maste evconn_listener_new() will use IOCP-backed implementations 544c43e99fdSEd Maste instead of the usual select-based one on Windows. 545c43e99fdSEd Maste */ 546c43e99fdSEd Maste EVENT_BASE_FLAG_STARTUP_IOCP = 0x04, 547c43e99fdSEd Maste /** Instead of checking the current time every time the event loop is 548c43e99fdSEd Maste ready to run timeout callbacks, check after each timeout callback. 549c43e99fdSEd Maste */ 550c43e99fdSEd Maste EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08, 551c43e99fdSEd Maste 552c43e99fdSEd Maste /** If we are using the epoll backend, this flag says that it is 553c43e99fdSEd Maste safe to use Libevent's internal change-list code to batch up 554c43e99fdSEd Maste adds and deletes in order to try to do as few syscalls as 555c43e99fdSEd Maste possible. Setting this flag can make your code run faster, but 556c43e99fdSEd Maste it may trigger a Linux bug: it is not safe to use this flag 557c43e99fdSEd Maste if you have any fds cloned by dup() or its variants. Doing so 558c43e99fdSEd Maste will produce strange and hard-to-diagnose bugs. 559c43e99fdSEd Maste 560c43e99fdSEd Maste This flag can also be activated by setting the 561c43e99fdSEd Maste EVENT_EPOLL_USE_CHANGELIST environment variable. 562c43e99fdSEd Maste 563c43e99fdSEd Maste This flag has no effect if you wind up using a backend other than 564c43e99fdSEd Maste epoll. 565c43e99fdSEd Maste */ 566c43e99fdSEd Maste EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10, 567c43e99fdSEd Maste 568c43e99fdSEd Maste /** Ordinarily, Libevent implements its time and timeout code using 569c43e99fdSEd Maste the fastest monotonic timer that we have. If this flag is set, 570c43e99fdSEd Maste however, we use less efficient more precise timer, assuming one is 571c43e99fdSEd Maste present. 572c43e99fdSEd Maste */ 573c43e99fdSEd Maste EVENT_BASE_FLAG_PRECISE_TIMER = 0x20 574c43e99fdSEd Maste }; 575c43e99fdSEd Maste 576c43e99fdSEd Maste /** 577c43e99fdSEd Maste Return a bitmask of the features implemented by an event base. This 578c43e99fdSEd Maste will be a bitwise OR of one or more of the values of 579c43e99fdSEd Maste event_method_feature 580c43e99fdSEd Maste 581c43e99fdSEd Maste @see event_method_feature 582c43e99fdSEd Maste */ 583c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 584c43e99fdSEd Maste int event_base_get_features(const struct event_base *base); 585c43e99fdSEd Maste 586c43e99fdSEd Maste /** 587c43e99fdSEd Maste Enters a required event method feature that the application demands. 588c43e99fdSEd Maste 589c43e99fdSEd Maste Note that not every feature or combination of features is supported 590c43e99fdSEd Maste on every platform. Code that requests features should be prepared 591c43e99fdSEd Maste to handle the case where event_base_new_with_config() returns NULL, as in: 592c43e99fdSEd Maste <pre> 593c43e99fdSEd Maste event_config_require_features(cfg, EV_FEATURE_ET); 594c43e99fdSEd Maste base = event_base_new_with_config(cfg); 595c43e99fdSEd Maste if (base == NULL) { 596c43e99fdSEd Maste // We can't get edge-triggered behavior here. 597c43e99fdSEd Maste event_config_require_features(cfg, 0); 598c43e99fdSEd Maste base = event_base_new_with_config(cfg); 599c43e99fdSEd Maste } 600c43e99fdSEd Maste </pre> 601c43e99fdSEd Maste 602c43e99fdSEd Maste @param cfg the event configuration object 603c43e99fdSEd Maste @param feature a bitfield of one or more event_method_feature values. 604c43e99fdSEd Maste Replaces values from previous calls to this function. 605c43e99fdSEd Maste @return 0 on success, -1 on failure. 606c43e99fdSEd Maste @see event_method_feature, event_base_new_with_config() 607c43e99fdSEd Maste */ 608c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 609c43e99fdSEd Maste int event_config_require_features(struct event_config *cfg, int feature); 610c43e99fdSEd Maste 611c43e99fdSEd Maste /** 612c43e99fdSEd Maste * Sets one or more flags to configure what parts of the eventual event_base 613c43e99fdSEd Maste * will be initialized, and how they'll work. 614c43e99fdSEd Maste * 615c43e99fdSEd Maste * @see event_base_config_flags, event_base_new_with_config() 616c43e99fdSEd Maste **/ 617c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 618c43e99fdSEd Maste int event_config_set_flag(struct event_config *cfg, int flag); 619c43e99fdSEd Maste 620c43e99fdSEd Maste /** 621c43e99fdSEd Maste * Records a hint for the number of CPUs in the system. This is used for 622c43e99fdSEd Maste * tuning thread pools, etc, for optimal performance. In Libevent 2.0, 623c43e99fdSEd Maste * it is only on Windows, and only when IOCP is in use. 624c43e99fdSEd Maste * 625c43e99fdSEd Maste * @param cfg the event configuration object 626c43e99fdSEd Maste * @param cpus the number of cpus 627c43e99fdSEd Maste * @return 0 on success, -1 on failure. 628c43e99fdSEd Maste */ 629c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 630c43e99fdSEd Maste int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus); 631c43e99fdSEd Maste 632c43e99fdSEd Maste /** 633c43e99fdSEd Maste * Record an interval and/or a number of callbacks after which the event base 634c43e99fdSEd Maste * should check for new events. By default, the event base will run as many 635*b50261e2SCy Schubert * events are as activated at the highest activated priority before checking 636c43e99fdSEd Maste * for new events. If you configure it by setting max_interval, it will check 637c43e99fdSEd Maste * the time after each callback, and not allow more than max_interval to 638c43e99fdSEd Maste * elapse before checking for new events. If you configure it by setting 639c43e99fdSEd Maste * max_callbacks to a value >= 0, it will run no more than max_callbacks 640c43e99fdSEd Maste * callbacks before checking for new events. 641c43e99fdSEd Maste * 642c43e99fdSEd Maste * This option can decrease the latency of high-priority events, and 643c43e99fdSEd Maste * avoid priority inversions where multiple low-priority events keep us from 644c43e99fdSEd Maste * polling for high-priority events, but at the expense of slightly decreasing 645c43e99fdSEd Maste * the throughput. Use it with caution! 646c43e99fdSEd Maste * 647c43e99fdSEd Maste * @param cfg The event_base configuration object. 648c43e99fdSEd Maste * @param max_interval An interval after which Libevent should stop running 649c43e99fdSEd Maste * callbacks and check for more events, or NULL if there should be 650c43e99fdSEd Maste * no such interval. 651c43e99fdSEd Maste * @param max_callbacks A number of callbacks after which Libevent should 652c43e99fdSEd Maste * stop running callbacks and check for more events, or -1 if there 653c43e99fdSEd Maste * should be no such limit. 654c43e99fdSEd Maste * @param min_priority A priority below which max_interval and max_callbacks 655c43e99fdSEd Maste * should not be enforced. If this is set to 0, they are enforced 656c43e99fdSEd Maste * for events of every priority; if it's set to 1, they're enforced 657c43e99fdSEd Maste * for events of priority 1 and above, and so on. 658c43e99fdSEd Maste * @return 0 on success, -1 on failure. 659c43e99fdSEd Maste **/ 660c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 661c43e99fdSEd Maste int event_config_set_max_dispatch_interval(struct event_config *cfg, 662c43e99fdSEd Maste const struct timeval *max_interval, int max_callbacks, 663c43e99fdSEd Maste int min_priority); 664c43e99fdSEd Maste 665c43e99fdSEd Maste /** 666c43e99fdSEd Maste Initialize the event API. 667c43e99fdSEd Maste 668c43e99fdSEd Maste Use event_base_new_with_config() to initialize a new event base, taking 669c43e99fdSEd Maste the specified configuration under consideration. The configuration object 670c43e99fdSEd Maste can currently be used to avoid certain event notification mechanisms. 671c43e99fdSEd Maste 672c43e99fdSEd Maste @param cfg the event configuration object 673c43e99fdSEd Maste @return an initialized event_base that can be used to registering events, 674c43e99fdSEd Maste or NULL if no event base can be created with the requested event_config. 675c43e99fdSEd Maste @see event_base_new(), event_base_free(), event_init(), event_assign() 676c43e99fdSEd Maste */ 677c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 678c43e99fdSEd Maste struct event_base *event_base_new_with_config(const struct event_config *); 679c43e99fdSEd Maste 680c43e99fdSEd Maste /** 681c43e99fdSEd Maste Deallocate all memory associated with an event_base, and free the base. 682c43e99fdSEd Maste 683c43e99fdSEd Maste Note that this function will not close any fds or free any memory passed 684c43e99fdSEd Maste to event_new as the argument to callback. 685c43e99fdSEd Maste 686c43e99fdSEd Maste If there are any pending finalizer callbacks, this function will invoke 687c43e99fdSEd Maste them. 688c43e99fdSEd Maste 689c43e99fdSEd Maste @param eb an event_base to be freed 690c43e99fdSEd Maste */ 691c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 692c43e99fdSEd Maste void event_base_free(struct event_base *); 693c43e99fdSEd Maste 694c43e99fdSEd Maste /** 695*b50261e2SCy Schubert As event_base_free, but do not run finalizers. 696c43e99fdSEd Maste */ 697c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 698c43e99fdSEd Maste void event_base_free_nofinalize(struct event_base *); 699c43e99fdSEd Maste 700c43e99fdSEd Maste /** @name Log severities 701c43e99fdSEd Maste */ 702c43e99fdSEd Maste /**@{*/ 703c43e99fdSEd Maste #define EVENT_LOG_DEBUG 0 704c43e99fdSEd Maste #define EVENT_LOG_MSG 1 705c43e99fdSEd Maste #define EVENT_LOG_WARN 2 706c43e99fdSEd Maste #define EVENT_LOG_ERR 3 707c43e99fdSEd Maste /**@}*/ 708c43e99fdSEd Maste 709c43e99fdSEd Maste /* Obsolete names: these are deprecated, but older programs might use them. 710c43e99fdSEd Maste * They violate the reserved-identifier namespace. */ 711c43e99fdSEd Maste #define _EVENT_LOG_DEBUG EVENT_LOG_DEBUG 712c43e99fdSEd Maste #define _EVENT_LOG_MSG EVENT_LOG_MSG 713c43e99fdSEd Maste #define _EVENT_LOG_WARN EVENT_LOG_WARN 714c43e99fdSEd Maste #define _EVENT_LOG_ERR EVENT_LOG_ERR 715c43e99fdSEd Maste 716c43e99fdSEd Maste /** 717c43e99fdSEd Maste A callback function used to intercept Libevent's log messages. 718c43e99fdSEd Maste 719c43e99fdSEd Maste @see event_set_log_callback 720c43e99fdSEd Maste */ 721c43e99fdSEd Maste typedef void (*event_log_cb)(int severity, const char *msg); 722c43e99fdSEd Maste /** 723c43e99fdSEd Maste Redirect Libevent's log messages. 724c43e99fdSEd Maste 725c43e99fdSEd Maste @param cb a function taking two arguments: an integer severity between 726c43e99fdSEd Maste EVENT_LOG_DEBUG and EVENT_LOG_ERR, and a string. If cb is NULL, 727c43e99fdSEd Maste then the default log is used. 728c43e99fdSEd Maste 729c43e99fdSEd Maste NOTE: The function you provide *must not* call any other libevent 730c43e99fdSEd Maste functionality. Doing so can produce undefined behavior. 731c43e99fdSEd Maste */ 732c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 733c43e99fdSEd Maste void event_set_log_callback(event_log_cb cb); 734c43e99fdSEd Maste 735c43e99fdSEd Maste /** 736c43e99fdSEd Maste A function to be called if Libevent encounters a fatal internal error. 737c43e99fdSEd Maste 738c43e99fdSEd Maste @see event_set_fatal_callback 739c43e99fdSEd Maste */ 740c43e99fdSEd Maste typedef void (*event_fatal_cb)(int err); 741c43e99fdSEd Maste 742c43e99fdSEd Maste /** 743c43e99fdSEd Maste Override Libevent's behavior in the event of a fatal internal error. 744c43e99fdSEd Maste 745c43e99fdSEd Maste By default, Libevent will call exit(1) if a programming error makes it 746c43e99fdSEd Maste impossible to continue correct operation. This function allows you to supply 747c43e99fdSEd Maste another callback instead. Note that if the function is ever invoked, 748c43e99fdSEd Maste something is wrong with your program, or with Libevent: any subsequent calls 749c43e99fdSEd Maste to Libevent may result in undefined behavior. 750c43e99fdSEd Maste 751c43e99fdSEd Maste Libevent will (almost) always log an EVENT_LOG_ERR message before calling 752c43e99fdSEd Maste this function; look at the last log message to see why Libevent has died. 753c43e99fdSEd Maste */ 754c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 755c43e99fdSEd Maste void event_set_fatal_callback(event_fatal_cb cb); 756c43e99fdSEd Maste 757c43e99fdSEd Maste #define EVENT_DBG_ALL 0xffffffffu 758c43e99fdSEd Maste #define EVENT_DBG_NONE 0 759c43e99fdSEd Maste 760c43e99fdSEd Maste /** 761c43e99fdSEd Maste Turn on debugging logs and have them sent to the default log handler. 762c43e99fdSEd Maste 763c43e99fdSEd Maste This is a global setting; if you are going to call it, you must call this 764c43e99fdSEd Maste before any calls that create an event-base. You must call it before any 765c43e99fdSEd Maste multithreaded use of Libevent. 766c43e99fdSEd Maste 767c43e99fdSEd Maste Debug logs are verbose. 768c43e99fdSEd Maste 769c43e99fdSEd Maste @param which Controls which debug messages are turned on. This option is 770c43e99fdSEd Maste unused for now; for forward compatibility, you must pass in the constant 771c43e99fdSEd Maste "EVENT_DBG_ALL" to turn debugging logs on, or "EVENT_DBG_NONE" to turn 772c43e99fdSEd Maste debugging logs off. 773c43e99fdSEd Maste */ 774c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 775c43e99fdSEd Maste void event_enable_debug_logging(ev_uint32_t which); 776c43e99fdSEd Maste 777c43e99fdSEd Maste /** 778c43e99fdSEd Maste Associate a different event base with an event. 779c43e99fdSEd Maste 780c43e99fdSEd Maste The event to be associated must not be currently active or pending. 781c43e99fdSEd Maste 782c43e99fdSEd Maste @param eb the event base 783c43e99fdSEd Maste @param ev the event 784c43e99fdSEd Maste @return 0 on success, -1 on failure. 785c43e99fdSEd Maste */ 786c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 787c43e99fdSEd Maste int event_base_set(struct event_base *, struct event *); 788c43e99fdSEd Maste 789c43e99fdSEd Maste /** @name Loop flags 790c43e99fdSEd Maste 791c43e99fdSEd Maste These flags control the behavior of event_base_loop(). 792c43e99fdSEd Maste */ 793c43e99fdSEd Maste /**@{*/ 794c43e99fdSEd Maste /** Block until we have an active event, then exit once all active events 795c43e99fdSEd Maste * have had their callbacks run. */ 796c43e99fdSEd Maste #define EVLOOP_ONCE 0x01 797c43e99fdSEd Maste /** Do not block: see which events are ready now, run the callbacks 798c43e99fdSEd Maste * of the highest-priority ones, then exit. */ 799c43e99fdSEd Maste #define EVLOOP_NONBLOCK 0x02 800c43e99fdSEd Maste /** Do not exit the loop because we have no pending events. Instead, keep 801c43e99fdSEd Maste * running until event_base_loopexit() or event_base_loopbreak() makes us 802c43e99fdSEd Maste * stop. 803c43e99fdSEd Maste */ 804c43e99fdSEd Maste #define EVLOOP_NO_EXIT_ON_EMPTY 0x04 805c43e99fdSEd Maste /**@}*/ 806c43e99fdSEd Maste 807c43e99fdSEd Maste /** 808c43e99fdSEd Maste Wait for events to become active, and run their callbacks. 809c43e99fdSEd Maste 810c43e99fdSEd Maste This is a more flexible version of event_base_dispatch(). 811c43e99fdSEd Maste 812c43e99fdSEd Maste By default, this loop will run the event base until either there are no more 813c43e99fdSEd Maste pending or active events, or until something calls event_base_loopbreak() or 814c43e99fdSEd Maste event_base_loopexit(). You can override this behavior with the 'flags' 815c43e99fdSEd Maste argument. 816c43e99fdSEd Maste 817c43e99fdSEd Maste @param eb the event_base structure returned by event_base_new() or 818c43e99fdSEd Maste event_base_new_with_config() 819c43e99fdSEd Maste @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK 820c43e99fdSEd Maste @return 0 if successful, -1 if an error occurred, or 1 if we exited because 821c43e99fdSEd Maste no events were pending or active. 822c43e99fdSEd Maste @see event_base_loopexit(), event_base_dispatch(), EVLOOP_ONCE, 823c43e99fdSEd Maste EVLOOP_NONBLOCK 824c43e99fdSEd Maste */ 825c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 826c43e99fdSEd Maste int event_base_loop(struct event_base *, int); 827c43e99fdSEd Maste 828c43e99fdSEd Maste /** 829c43e99fdSEd Maste Exit the event loop after the specified time 830c43e99fdSEd Maste 831c43e99fdSEd Maste The next event_base_loop() iteration after the given timer expires will 832c43e99fdSEd Maste complete normally (handling all queued events) then exit without 833c43e99fdSEd Maste blocking for events again. 834c43e99fdSEd Maste 835c43e99fdSEd Maste Subsequent invocations of event_base_loop() will proceed normally. 836c43e99fdSEd Maste 837c43e99fdSEd Maste @param eb the event_base structure returned by event_init() 838c43e99fdSEd Maste @param tv the amount of time after which the loop should terminate, 839c43e99fdSEd Maste or NULL to exit after running all currently active events. 840c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 841c43e99fdSEd Maste @see event_base_loopbreak() 842c43e99fdSEd Maste */ 843c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 844c43e99fdSEd Maste int event_base_loopexit(struct event_base *, const struct timeval *); 845c43e99fdSEd Maste 846c43e99fdSEd Maste /** 847c43e99fdSEd Maste Abort the active event_base_loop() immediately. 848c43e99fdSEd Maste 849c43e99fdSEd Maste event_base_loop() will abort the loop after the next event is completed; 850c43e99fdSEd Maste event_base_loopbreak() is typically invoked from this event's callback. 851c43e99fdSEd Maste This behavior is analogous to the "break;" statement. 852c43e99fdSEd Maste 853c43e99fdSEd Maste Subsequent invocations of event_base_loop() will proceed normally. 854c43e99fdSEd Maste 855c43e99fdSEd Maste @param eb the event_base structure returned by event_init() 856c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 857c43e99fdSEd Maste @see event_base_loopexit() 858c43e99fdSEd Maste */ 859c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 860c43e99fdSEd Maste int event_base_loopbreak(struct event_base *); 861c43e99fdSEd Maste 862c43e99fdSEd Maste /** 863c43e99fdSEd Maste Tell the active event_base_loop() to scan for new events immediately. 864c43e99fdSEd Maste 865c43e99fdSEd Maste Calling this function makes the currently active event_base_loop() 866c43e99fdSEd Maste start the loop over again (scanning for new events) after the current 867c43e99fdSEd Maste event callback finishes. If the event loop is not running, this 868c43e99fdSEd Maste function has no effect. 869c43e99fdSEd Maste 870c43e99fdSEd Maste event_base_loopbreak() is typically invoked from this event's callback. 871c43e99fdSEd Maste This behavior is analogous to the "continue;" statement. 872c43e99fdSEd Maste 873c43e99fdSEd Maste Subsequent invocations of event loop will proceed normally. 874c43e99fdSEd Maste 875c43e99fdSEd Maste @param eb the event_base structure returned by event_init() 876c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 877c43e99fdSEd Maste @see event_base_loopbreak() 878c43e99fdSEd Maste */ 879c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 880c43e99fdSEd Maste int event_base_loopcontinue(struct event_base *); 881c43e99fdSEd Maste 882c43e99fdSEd Maste /** 883c43e99fdSEd Maste Checks if the event loop was told to exit by event_base_loopexit(). 884c43e99fdSEd Maste 885c43e99fdSEd Maste This function will return true for an event_base at every point after 886c43e99fdSEd Maste event_loopexit() is called, until the event loop is next entered. 887c43e99fdSEd Maste 888c43e99fdSEd Maste @param eb the event_base structure returned by event_init() 889c43e99fdSEd Maste @return true if event_base_loopexit() was called on this event base, 890c43e99fdSEd Maste or 0 otherwise 891c43e99fdSEd Maste @see event_base_loopexit() 892c43e99fdSEd Maste @see event_base_got_break() 893c43e99fdSEd Maste */ 894c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 895c43e99fdSEd Maste int event_base_got_exit(struct event_base *); 896c43e99fdSEd Maste 897c43e99fdSEd Maste /** 898c43e99fdSEd Maste Checks if the event loop was told to abort immediately by event_base_loopbreak(). 899c43e99fdSEd Maste 900c43e99fdSEd Maste This function will return true for an event_base at every point after 901c43e99fdSEd Maste event_base_loopbreak() is called, until the event loop is next entered. 902c43e99fdSEd Maste 903c43e99fdSEd Maste @param eb the event_base structure returned by event_init() 904c43e99fdSEd Maste @return true if event_base_loopbreak() was called on this event base, 905c43e99fdSEd Maste or 0 otherwise 906c43e99fdSEd Maste @see event_base_loopbreak() 907c43e99fdSEd Maste @see event_base_got_exit() 908c43e99fdSEd Maste */ 909c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 910c43e99fdSEd Maste int event_base_got_break(struct event_base *); 911c43e99fdSEd Maste 912c43e99fdSEd Maste /** 913c43e99fdSEd Maste * @name event flags 914c43e99fdSEd Maste * 915c43e99fdSEd Maste * Flags to pass to event_new(), event_assign(), event_pending(), and 916c43e99fdSEd Maste * anything else with an argument of the form "short events" 917c43e99fdSEd Maste */ 918c43e99fdSEd Maste /**@{*/ 919c43e99fdSEd Maste /** Indicates that a timeout has occurred. It's not necessary to pass 920c43e99fdSEd Maste * this flag to event_for new()/event_assign() to get a timeout. */ 921c43e99fdSEd Maste #define EV_TIMEOUT 0x01 922c43e99fdSEd Maste /** Wait for a socket or FD to become readable */ 923c43e99fdSEd Maste #define EV_READ 0x02 924c43e99fdSEd Maste /** Wait for a socket or FD to become writeable */ 925c43e99fdSEd Maste #define EV_WRITE 0x04 926c43e99fdSEd Maste /** Wait for a POSIX signal to be raised*/ 927c43e99fdSEd Maste #define EV_SIGNAL 0x08 928c43e99fdSEd Maste /** 929c43e99fdSEd Maste * Persistent event: won't get removed automatically when activated. 930c43e99fdSEd Maste * 931c43e99fdSEd Maste * When a persistent event with a timeout becomes activated, its timeout 932c43e99fdSEd Maste * is reset to 0. 933c43e99fdSEd Maste */ 934c43e99fdSEd Maste #define EV_PERSIST 0x10 935c43e99fdSEd Maste /** Select edge-triggered behavior, if supported by the backend. */ 936c43e99fdSEd Maste #define EV_ET 0x20 937c43e99fdSEd Maste /** 938c43e99fdSEd Maste * If this option is provided, then event_del() will not block in one thread 939c43e99fdSEd Maste * while waiting for the event callback to complete in another thread. 940c43e99fdSEd Maste * 941c43e99fdSEd Maste * To use this option safely, you may need to use event_finalize() or 942c43e99fdSEd Maste * event_free_finalize() in order to safely tear down an event in a 943c43e99fdSEd Maste * multithreaded application. See those functions for more information. 944c43e99fdSEd Maste **/ 945c43e99fdSEd Maste #define EV_FINALIZE 0x40 946c43e99fdSEd Maste /** 947c43e99fdSEd Maste * Detects connection close events. You can use this to detect when a 948c43e99fdSEd Maste * connection has been closed, without having to read all the pending data 949c43e99fdSEd Maste * from a connection. 950c43e99fdSEd Maste * 951c43e99fdSEd Maste * Not all backends support EV_CLOSED. To detect or require it, use the 952c43e99fdSEd Maste * feature flag EV_FEATURE_EARLY_CLOSE. 953c43e99fdSEd Maste **/ 954c43e99fdSEd Maste #define EV_CLOSED 0x80 955c43e99fdSEd Maste /**@}*/ 956c43e99fdSEd Maste 957c43e99fdSEd Maste /** 958c43e99fdSEd Maste @name evtimer_* macros 959c43e99fdSEd Maste 960*b50261e2SCy Schubert Aliases for working with one-shot timer events 961*b50261e2SCy Schubert If you need EV_PERSIST timer use event_*() functions. 962*b50261e2SCy Schubert */ 963c43e99fdSEd Maste /**@{*/ 964c43e99fdSEd Maste #define evtimer_assign(ev, b, cb, arg) \ 965c43e99fdSEd Maste event_assign((ev), (b), -1, 0, (cb), (arg)) 966c43e99fdSEd Maste #define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg)) 967c43e99fdSEd Maste #define evtimer_add(ev, tv) event_add((ev), (tv)) 968c43e99fdSEd Maste #define evtimer_del(ev) event_del(ev) 969c43e99fdSEd Maste #define evtimer_pending(ev, tv) event_pending((ev), EV_TIMEOUT, (tv)) 970c43e99fdSEd Maste #define evtimer_initialized(ev) event_initialized(ev) 971c43e99fdSEd Maste /**@}*/ 972c43e99fdSEd Maste 973c43e99fdSEd Maste /** 974c43e99fdSEd Maste @name evsignal_* macros 975c43e99fdSEd Maste 976c43e99fdSEd Maste Aliases for working with signal events 977c43e99fdSEd Maste */ 978c43e99fdSEd Maste /**@{*/ 979c43e99fdSEd Maste #define evsignal_add(ev, tv) event_add((ev), (tv)) 980c43e99fdSEd Maste #define evsignal_assign(ev, b, x, cb, arg) \ 981c43e99fdSEd Maste event_assign((ev), (b), (x), EV_SIGNAL|EV_PERSIST, cb, (arg)) 982c43e99fdSEd Maste #define evsignal_new(b, x, cb, arg) \ 983c43e99fdSEd Maste event_new((b), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg)) 984c43e99fdSEd Maste #define evsignal_del(ev) event_del(ev) 985c43e99fdSEd Maste #define evsignal_pending(ev, tv) event_pending((ev), EV_SIGNAL, (tv)) 986c43e99fdSEd Maste #define evsignal_initialized(ev) event_initialized(ev) 987c43e99fdSEd Maste /**@}*/ 988c43e99fdSEd Maste 989c43e99fdSEd Maste /** 990*b50261e2SCy Schubert @name evuser_* macros 991*b50261e2SCy Schubert 992*b50261e2SCy Schubert Aliases for working with user-triggered events 993*b50261e2SCy Schubert If you need EV_PERSIST event use event_*() functions. 994*b50261e2SCy Schubert */ 995*b50261e2SCy Schubert /**@{*/ 996*b50261e2SCy Schubert #define evuser_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg)) 997*b50261e2SCy Schubert #define evuser_del(ev) event_del(ev) 998*b50261e2SCy Schubert #define evuser_pending(ev, tv) event_pending((ev), 0, (tv)) 999*b50261e2SCy Schubert #define evuser_initialized(ev) event_initialized(ev) 1000*b50261e2SCy Schubert #define evuser_trigger(ev) event_active((ev), 0, 0) 1001*b50261e2SCy Schubert /**@}*/ 1002*b50261e2SCy Schubert 1003*b50261e2SCy Schubert /** 1004c43e99fdSEd Maste A callback function for an event. 1005c43e99fdSEd Maste 1006c43e99fdSEd Maste It receives three arguments: 1007c43e99fdSEd Maste 1008c43e99fdSEd Maste @param fd An fd or signal 1009c43e99fdSEd Maste @param events One or more EV_* flags 1010c43e99fdSEd Maste @param arg A user-supplied argument. 1011c43e99fdSEd Maste 1012c43e99fdSEd Maste @see event_new() 1013c43e99fdSEd Maste */ 1014c43e99fdSEd Maste typedef void (*event_callback_fn)(evutil_socket_t, short, void *); 1015c43e99fdSEd Maste 1016c43e99fdSEd Maste /** 1017c43e99fdSEd Maste Return a value used to specify that the event itself must be used as the callback argument. 1018c43e99fdSEd Maste 1019c43e99fdSEd Maste The function event_new() takes a callback argument which is passed 1020c43e99fdSEd Maste to the event's callback function. To specify that the argument to be 1021c43e99fdSEd Maste passed to the callback function is the event that event_new() returns, 1022c43e99fdSEd Maste pass in the return value of event_self_cbarg() as the callback argument 1023c43e99fdSEd Maste for event_new(). 1024c43e99fdSEd Maste 1025c43e99fdSEd Maste For example: 1026c43e99fdSEd Maste <pre> 1027c43e99fdSEd Maste struct event *ev = event_new(base, sock, events, callback, %event_self_cbarg()); 1028c43e99fdSEd Maste </pre> 1029c43e99fdSEd Maste 1030c43e99fdSEd Maste For consistency with event_new(), it is possible to pass the return value 1031c43e99fdSEd Maste of this function as the callback argument for event_assign() – this 1032c43e99fdSEd Maste achieves the same result as passing the event in directly. 1033c43e99fdSEd Maste 1034c43e99fdSEd Maste @return a value to be passed as the callback argument to event_new() or 1035c43e99fdSEd Maste event_assign(). 1036c43e99fdSEd Maste @see event_new(), event_assign() 1037c43e99fdSEd Maste */ 1038c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1039c43e99fdSEd Maste void *event_self_cbarg(void); 1040c43e99fdSEd Maste 1041c43e99fdSEd Maste /** 1042*b50261e2SCy Schubert Allocate and assign a new event structure, ready to be added. 1043c43e99fdSEd Maste 1044c43e99fdSEd Maste The function event_new() returns a new event that can be used in 1045c43e99fdSEd Maste future calls to event_add() and event_del(). The fd and events 1046c43e99fdSEd Maste arguments determine which conditions will trigger the event; the 1047c43e99fdSEd Maste callback and callback_arg arguments tell Libevent what to do when the 1048c43e99fdSEd Maste event becomes active. 1049c43e99fdSEd Maste 1050c43e99fdSEd Maste If events contains one of EV_READ, EV_WRITE, or EV_READ|EV_WRITE, then 1051c43e99fdSEd Maste fd is a file descriptor or socket that should get monitored for 1052c43e99fdSEd Maste readiness to read, readiness to write, or readiness for either operation 1053c43e99fdSEd Maste (respectively). If events contains EV_SIGNAL, then fd is a signal 1054c43e99fdSEd Maste number to wait for. If events contains none of those flags, then the 1055c43e99fdSEd Maste event can be triggered only by a timeout or by manual activation with 1056c43e99fdSEd Maste event_active(): In this case, fd must be -1. 1057c43e99fdSEd Maste 1058c43e99fdSEd Maste The EV_PERSIST flag can also be passed in the events argument: it makes 1059c43e99fdSEd Maste event_add() persistent until event_del() is called. 1060c43e99fdSEd Maste 1061c43e99fdSEd Maste The EV_ET flag is compatible with EV_READ and EV_WRITE, and supported 1062c43e99fdSEd Maste only by certain backends. It tells Libevent to use edge-triggered 1063c43e99fdSEd Maste events. 1064c43e99fdSEd Maste 1065c43e99fdSEd Maste The EV_TIMEOUT flag has no effect here. 1066c43e99fdSEd Maste 1067c43e99fdSEd Maste It is okay to have multiple events all listening on the same fds; but 1068*b50261e2SCy Schubert they must either all be edge-triggered, or all not be edge triggered. 1069c43e99fdSEd Maste 1070c43e99fdSEd Maste When the event becomes active, the event loop will run the provided 1071*b50261e2SCy Schubert callback function, with three arguments. The first will be the provided 1072c43e99fdSEd Maste fd value. The second will be a bitfield of the events that triggered: 1073c43e99fdSEd Maste EV_READ, EV_WRITE, or EV_SIGNAL. Here the EV_TIMEOUT flag indicates 1074c43e99fdSEd Maste that a timeout occurred, and EV_ET indicates that an edge-triggered 1075c43e99fdSEd Maste event occurred. The third event will be the callback_arg pointer that 1076c43e99fdSEd Maste you provide. 1077c43e99fdSEd Maste 1078c43e99fdSEd Maste @param base the event base to which the event should be attached. 1079c43e99fdSEd Maste @param fd the file descriptor or signal to be monitored, or -1. 1080c43e99fdSEd Maste @param events desired events to monitor: bitfield of EV_READ, EV_WRITE, 1081c43e99fdSEd Maste EV_SIGNAL, EV_PERSIST, EV_ET. 1082c43e99fdSEd Maste @param callback callback function to be invoked when the event occurs 1083c43e99fdSEd Maste @param callback_arg an argument to be passed to the callback function 1084c43e99fdSEd Maste 1085c43e99fdSEd Maste @return a newly allocated struct event that must later be freed with 1086*b50261e2SCy Schubert event_free() or NULL if an error occurred. 1087c43e99fdSEd Maste @see event_free(), event_add(), event_del(), event_assign() 1088c43e99fdSEd Maste */ 1089c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1090c43e99fdSEd Maste struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *); 1091c43e99fdSEd Maste 1092c43e99fdSEd Maste 1093c43e99fdSEd Maste /** 1094c43e99fdSEd Maste Prepare a new, already-allocated event structure to be added. 1095c43e99fdSEd Maste 1096c43e99fdSEd Maste The function event_assign() prepares the event structure ev to be used 1097c43e99fdSEd Maste in future calls to event_add() and event_del(). Unlike event_new(), it 1098c43e99fdSEd Maste doesn't allocate memory itself: it requires that you have already 1099c43e99fdSEd Maste allocated a struct event, probably on the heap. Doing this will 1100c43e99fdSEd Maste typically make your code depend on the size of the event structure, and 1101c43e99fdSEd Maste thereby create incompatibility with future versions of Libevent. 1102c43e99fdSEd Maste 1103c43e99fdSEd Maste The easiest way to avoid this problem is just to use event_new() and 1104c43e99fdSEd Maste event_free() instead. 1105c43e99fdSEd Maste 1106c43e99fdSEd Maste A slightly harder way to future-proof your code is to use 1107c43e99fdSEd Maste event_get_struct_event_size() to determine the required size of an event 1108c43e99fdSEd Maste at runtime. 1109c43e99fdSEd Maste 1110c43e99fdSEd Maste Note that it is NOT safe to call this function on an event that is 1111c43e99fdSEd Maste active or pending. Doing so WILL corrupt internal data structures in 1112c43e99fdSEd Maste Libevent, and lead to strange, hard-to-diagnose bugs. You _can_ use 1113c43e99fdSEd Maste event_assign to change an existing event, but only if it is not active 1114c43e99fdSEd Maste or pending! 1115c43e99fdSEd Maste 1116c43e99fdSEd Maste The arguments for this function, and the behavior of the events that it 1117c43e99fdSEd Maste makes, are as for event_new(). 1118c43e99fdSEd Maste 1119c43e99fdSEd Maste @param ev an event struct to be modified 1120c43e99fdSEd Maste @param base the event base to which ev should be attached. 1121c43e99fdSEd Maste @param fd the file descriptor to be monitored 1122c43e99fdSEd Maste @param events desired events to monitor; can be EV_READ and/or EV_WRITE 1123c43e99fdSEd Maste @param callback callback function to be invoked when the event occurs 1124c43e99fdSEd Maste @param callback_arg an argument to be passed to the callback function 1125c43e99fdSEd Maste 1126c43e99fdSEd Maste @return 0 if success, or -1 on invalid arguments. 1127c43e99fdSEd Maste 1128c43e99fdSEd Maste @see event_new(), event_add(), event_del(), event_base_once(), 1129c43e99fdSEd Maste event_get_struct_event_size() 1130c43e99fdSEd Maste */ 1131c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1132c43e99fdSEd Maste int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *); 1133c43e99fdSEd Maste 1134c43e99fdSEd Maste /** 1135c43e99fdSEd Maste Deallocate a struct event * returned by event_new(). 1136c43e99fdSEd Maste 1137*b50261e2SCy Schubert If the event is pending or active, this function makes it non-pending 1138*b50261e2SCy Schubert and non-active first. 1139c43e99fdSEd Maste */ 1140c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1141c43e99fdSEd Maste void event_free(struct event *); 1142c43e99fdSEd Maste 1143c43e99fdSEd Maste /** 1144c43e99fdSEd Maste * Callback type for event_finalize and event_free_finalize(). 1145c43e99fdSEd Maste **/ 1146c43e99fdSEd Maste typedef void (*event_finalize_callback_fn)(struct event *, void *); 1147c43e99fdSEd Maste /** 1148c43e99fdSEd Maste @name Finalization functions 1149c43e99fdSEd Maste 1150c43e99fdSEd Maste These functions are used to safely tear down an event in a multithreaded 1151c43e99fdSEd Maste application. If you construct your events with EV_FINALIZE to avoid 1152c43e99fdSEd Maste deadlocks, you will need a way to remove an event in the certainty that 1153c43e99fdSEd Maste it will definitely not be running its callback when you deallocate it 1154c43e99fdSEd Maste and its callback argument. 1155c43e99fdSEd Maste 1156c43e99fdSEd Maste To do this, call one of event_finalize() or event_free_finalize with 1157c43e99fdSEd Maste 0 for its first argument, the event to tear down as its second argument, 1158c43e99fdSEd Maste and a callback function as its third argument. The callback will be 1159c43e99fdSEd Maste invoked as part of the event loop, with the event's priority. 1160c43e99fdSEd Maste 1161c43e99fdSEd Maste After you call a finalizer function, event_add() and event_active() will 1162c43e99fdSEd Maste no longer work on the event, and event_del() will produce a no-op. You 1163c43e99fdSEd Maste must not try to change the event's fields with event_assign() or 1164c43e99fdSEd Maste event_set() while the finalize callback is in progress. Once the 1165c43e99fdSEd Maste callback has been invoked, you should treat the event structure as 1166c43e99fdSEd Maste containing uninitialized memory. 1167c43e99fdSEd Maste 1168c43e99fdSEd Maste The event_free_finalize() function frees the event after it's finalized; 1169c43e99fdSEd Maste event_finalize() does not. 1170c43e99fdSEd Maste 1171c43e99fdSEd Maste A finalizer callback must not make events pending or active. It must not 1172*b50261e2SCy Schubert add events, activate events, or attempt to "resuscitate" the event being 1173c43e99fdSEd Maste finalized in any way. 1174c43e99fdSEd Maste 1175*b50261e2SCy Schubert @return 0 on success, -1 on failure. 1176c43e99fdSEd Maste */ 1177c43e99fdSEd Maste /**@{*/ 1178c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1179c43e99fdSEd Maste int event_finalize(unsigned, struct event *, event_finalize_callback_fn); 1180c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1181c43e99fdSEd Maste int event_free_finalize(unsigned, struct event *, event_finalize_callback_fn); 1182c43e99fdSEd Maste /**@}*/ 1183c43e99fdSEd Maste 1184c43e99fdSEd Maste /** 1185c43e99fdSEd Maste Schedule a one-time event 1186c43e99fdSEd Maste 1187c43e99fdSEd Maste The function event_base_once() is similar to event_new(). However, it 1188c43e99fdSEd Maste schedules a callback to be called exactly once, and does not require the 1189c43e99fdSEd Maste caller to prepare an event structure. 1190c43e99fdSEd Maste 1191c43e99fdSEd Maste Note that in Libevent 2.0 and earlier, if the event is never triggered, the 1192c43e99fdSEd Maste internal memory used to hold it will never be freed. In Libevent 2.1, 1193c43e99fdSEd Maste the internal memory will get freed by event_base_free() if the event 1194c43e99fdSEd Maste is never triggered. The 'arg' value, however, will not get freed in either 1195c43e99fdSEd Maste case--you'll need to free that on your own if you want it to go away. 1196c43e99fdSEd Maste 1197c43e99fdSEd Maste @param base an event_base 1198c43e99fdSEd Maste @param fd a file descriptor to monitor, or -1 for no fd. 1199c43e99fdSEd Maste @param events event(s) to monitor; can be any of EV_READ | 1200c43e99fdSEd Maste EV_WRITE, or EV_TIMEOUT 1201c43e99fdSEd Maste @param callback callback function to be invoked when the event occurs 1202c43e99fdSEd Maste @param arg an argument to be passed to the callback function 1203c43e99fdSEd Maste @param timeout the maximum amount of time to wait for the event. NULL 1204c43e99fdSEd Maste makes an EV_READ/EV_WRITE event make forever; NULL makes an 1205*b50261e2SCy Schubert EV_TIMEOUT event success immediately. 1206c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 1207c43e99fdSEd Maste */ 1208c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1209c43e99fdSEd Maste int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *); 1210c43e99fdSEd Maste 1211c43e99fdSEd Maste /** 1212c43e99fdSEd Maste Add an event to the set of pending events. 1213c43e99fdSEd Maste 1214c43e99fdSEd Maste The function event_add() schedules the execution of the event 'ev' when the 1215c43e99fdSEd Maste condition specified by event_assign() or event_new() occurs, or when the time 1216*b50261e2SCy Schubert specified in timeout has elapsed. If a timeout is NULL, no timeout 1217c43e99fdSEd Maste occurs and the function will only be 1218c43e99fdSEd Maste called if a matching event occurs. The event in the 1219c43e99fdSEd Maste ev argument must be already initialized by event_assign() or event_new() 1220c43e99fdSEd Maste and may not be used 1221c43e99fdSEd Maste in calls to event_assign() until it is no longer pending. 1222c43e99fdSEd Maste 1223c43e99fdSEd Maste If the event in the ev argument already has a scheduled timeout, calling 1224c43e99fdSEd Maste event_add() replaces the old timeout with the new one if tv is non-NULL. 1225c43e99fdSEd Maste 1226c43e99fdSEd Maste @param ev an event struct initialized via event_assign() or event_new() 1227c43e99fdSEd Maste @param timeout the maximum amount of time to wait for the event, or NULL 1228c43e99fdSEd Maste to wait forever 1229c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 1230c43e99fdSEd Maste @see event_del(), event_assign(), event_new() 1231c43e99fdSEd Maste */ 1232c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1233c43e99fdSEd Maste int event_add(struct event *ev, const struct timeval *timeout); 1234c43e99fdSEd Maste 1235c43e99fdSEd Maste /** 1236c43e99fdSEd Maste Remove a timer from a pending event without removing the event itself. 1237c43e99fdSEd Maste 1238c43e99fdSEd Maste If the event has a scheduled timeout, this function unschedules it but 1239c43e99fdSEd Maste leaves the event otherwise pending. 1240c43e99fdSEd Maste 1241c43e99fdSEd Maste @param ev an event struct initialized via event_assign() or event_new() 1242*b50261e2SCy Schubert @return 0 on success, or -1 if an error occurred. 1243c43e99fdSEd Maste */ 1244c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1245c43e99fdSEd Maste int event_remove_timer(struct event *ev); 1246c43e99fdSEd Maste 1247c43e99fdSEd Maste /** 1248c43e99fdSEd Maste Remove an event from the set of monitored events. 1249c43e99fdSEd Maste 1250c43e99fdSEd Maste The function event_del() will cancel the event in the argument ev. If the 1251c43e99fdSEd Maste event has already executed or has never been added the call will have no 1252c43e99fdSEd Maste effect. 1253c43e99fdSEd Maste 1254c43e99fdSEd Maste @param ev an event struct to be removed from the working set 1255c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 1256c43e99fdSEd Maste @see event_add() 1257c43e99fdSEd Maste */ 1258c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1259c43e99fdSEd Maste int event_del(struct event *); 1260c43e99fdSEd Maste 1261c43e99fdSEd Maste /** 1262c43e99fdSEd Maste As event_del(), but never blocks while the event's callback is running 1263c43e99fdSEd Maste in another thread, even if the event was constructed without the 1264c43e99fdSEd Maste EV_FINALIZE flag. 1265c43e99fdSEd Maste */ 1266c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1267c43e99fdSEd Maste int event_del_noblock(struct event *ev); 1268c43e99fdSEd Maste /** 1269c43e99fdSEd Maste As event_del(), but always blocks while the event's callback is running 1270c43e99fdSEd Maste in another thread, even if the event was constructed with the 1271c43e99fdSEd Maste EV_FINALIZE flag. 1272c43e99fdSEd Maste */ 1273c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1274c43e99fdSEd Maste int event_del_block(struct event *ev); 1275c43e99fdSEd Maste 1276c43e99fdSEd Maste /** 1277c43e99fdSEd Maste Make an event active. 1278c43e99fdSEd Maste 1279c43e99fdSEd Maste You can use this function on a pending or a non-pending event to make it 1280c43e99fdSEd Maste active, so that its callback will be run by event_base_dispatch() or 1281c43e99fdSEd Maste event_base_loop(). 1282c43e99fdSEd Maste 1283c43e99fdSEd Maste One common use in multithreaded programs is to wake the thread running 1284c43e99fdSEd Maste event_base_loop() from another thread. 1285c43e99fdSEd Maste 1286c43e99fdSEd Maste @param ev an event to make active. 1287c43e99fdSEd Maste @param res a set of flags to pass to the event's callback. 1288c43e99fdSEd Maste @param ncalls an obsolete argument: this is ignored. 1289c43e99fdSEd Maste **/ 1290c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1291c43e99fdSEd Maste void event_active(struct event *ev, int res, short ncalls); 1292c43e99fdSEd Maste 1293c43e99fdSEd Maste /** 1294c43e99fdSEd Maste Checks if a specific event is pending or scheduled. 1295c43e99fdSEd Maste 1296c43e99fdSEd Maste @param ev an event struct previously passed to event_add() 1297c43e99fdSEd Maste @param events the requested event type; any of EV_TIMEOUT|EV_READ| 1298c43e99fdSEd Maste EV_WRITE|EV_SIGNAL 1299c43e99fdSEd Maste @param tv if this field is not NULL, and the event has a timeout, 1300c43e99fdSEd Maste this field is set to hold the time at which the timeout will 1301c43e99fdSEd Maste expire. 1302c43e99fdSEd Maste 1303c43e99fdSEd Maste @return true if the event is pending on any of the events in 'what', (that 1304c43e99fdSEd Maste is to say, it has been added), or 0 if the event is not added. 1305c43e99fdSEd Maste */ 1306c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1307c43e99fdSEd Maste int event_pending(const struct event *ev, short events, struct timeval *tv); 1308c43e99fdSEd Maste 1309c43e99fdSEd Maste /** 1310c43e99fdSEd Maste If called from within the callback for an event, returns that event. 1311c43e99fdSEd Maste 1312c43e99fdSEd Maste The behavior of this function is not defined when called from outside the 1313c43e99fdSEd Maste callback function for an event. 1314c43e99fdSEd Maste */ 1315c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1316c43e99fdSEd Maste struct event *event_base_get_running_event(struct event_base *base); 1317c43e99fdSEd Maste 1318c43e99fdSEd Maste /** 1319c43e99fdSEd Maste Test if an event structure might be initialized. 1320c43e99fdSEd Maste 1321c43e99fdSEd Maste The event_initialized() function can be used to check if an event has been 1322c43e99fdSEd Maste initialized. 1323c43e99fdSEd Maste 1324*b50261e2SCy Schubert Warning: This function is only useful for distinguishing a zeroed-out 1325c43e99fdSEd Maste piece of memory from an initialized event, it can easily be confused by 1326c43e99fdSEd Maste uninitialized memory. Thus, it should ONLY be used to distinguish an 1327c43e99fdSEd Maste initialized event from zero. 1328c43e99fdSEd Maste 1329c43e99fdSEd Maste @param ev an event structure to be tested 1330c43e99fdSEd Maste @return 1 if the structure might be initialized, or 0 if it has not been 1331c43e99fdSEd Maste initialized 1332c43e99fdSEd Maste */ 1333c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1334c43e99fdSEd Maste int event_initialized(const struct event *ev); 1335c43e99fdSEd Maste 1336c43e99fdSEd Maste /** 1337c43e99fdSEd Maste Get the signal number assigned to a signal event 1338c43e99fdSEd Maste */ 1339c43e99fdSEd Maste #define event_get_signal(ev) ((int)event_get_fd(ev)) 1340c43e99fdSEd Maste 1341c43e99fdSEd Maste /** 1342c43e99fdSEd Maste Get the socket or signal assigned to an event, or -1 if the event has 1343c43e99fdSEd Maste no socket. 1344c43e99fdSEd Maste */ 1345c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1346c43e99fdSEd Maste evutil_socket_t event_get_fd(const struct event *ev); 1347c43e99fdSEd Maste 1348c43e99fdSEd Maste /** 1349c43e99fdSEd Maste Get the event_base associated with an event. 1350c43e99fdSEd Maste */ 1351c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1352c43e99fdSEd Maste struct event_base *event_get_base(const struct event *ev); 1353c43e99fdSEd Maste 1354c43e99fdSEd Maste /** 1355c43e99fdSEd Maste Return the events (EV_READ, EV_WRITE, etc) assigned to an event. 1356c43e99fdSEd Maste */ 1357c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1358c43e99fdSEd Maste short event_get_events(const struct event *ev); 1359c43e99fdSEd Maste 1360c43e99fdSEd Maste /** 1361c43e99fdSEd Maste Return the callback assigned to an event. 1362c43e99fdSEd Maste */ 1363c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1364c43e99fdSEd Maste event_callback_fn event_get_callback(const struct event *ev); 1365c43e99fdSEd Maste 1366c43e99fdSEd Maste /** 1367c43e99fdSEd Maste Return the callback argument assigned to an event. 1368c43e99fdSEd Maste */ 1369c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1370c43e99fdSEd Maste void *event_get_callback_arg(const struct event *ev); 1371c43e99fdSEd Maste 1372c43e99fdSEd Maste /** 1373c43e99fdSEd Maste Return the priority of an event. 1374c43e99fdSEd Maste @see event_priority_init(), event_get_priority() 1375c43e99fdSEd Maste */ 1376c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1377c43e99fdSEd Maste int event_get_priority(const struct event *ev); 1378c43e99fdSEd Maste 1379c43e99fdSEd Maste /** 1380c43e99fdSEd Maste Extract _all_ of arguments given to construct a given event. The 1381c43e99fdSEd Maste event_base is copied into *base_out, the fd is copied into *fd_out, and so 1382c43e99fdSEd Maste on. 1383c43e99fdSEd Maste 1384c43e99fdSEd Maste If any of the "_out" arguments is NULL, it will be ignored. 1385c43e99fdSEd Maste */ 1386c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1387c43e99fdSEd Maste void event_get_assignment(const struct event *event, 1388c43e99fdSEd Maste struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, 1389c43e99fdSEd Maste event_callback_fn *callback_out, void **arg_out); 1390c43e99fdSEd Maste 1391c43e99fdSEd Maste /** 1392c43e99fdSEd Maste Return the size of struct event that the Libevent library was compiled 1393c43e99fdSEd Maste with. 1394c43e99fdSEd Maste 1395c43e99fdSEd Maste This will be NO GREATER than sizeof(struct event) if you're running with 1396c43e99fdSEd Maste the same version of Libevent that your application was built with, but 1397c43e99fdSEd Maste otherwise might not. 1398c43e99fdSEd Maste 1399c43e99fdSEd Maste Note that it might be SMALLER than sizeof(struct event) if some future 1400c43e99fdSEd Maste version of Libevent adds extra padding to the end of struct event. 1401c43e99fdSEd Maste We might do this to help ensure ABI-compatibility between different 1402c43e99fdSEd Maste versions of Libevent. 1403c43e99fdSEd Maste */ 1404c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1405c43e99fdSEd Maste size_t event_get_struct_event_size(void); 1406c43e99fdSEd Maste 1407c43e99fdSEd Maste /** 1408c43e99fdSEd Maste Get the Libevent version. 1409c43e99fdSEd Maste 1410c43e99fdSEd Maste Note that this will give you the version of the library that you're 1411c43e99fdSEd Maste currently linked against, not the version of the headers that you've 1412c43e99fdSEd Maste compiled against. 1413c43e99fdSEd Maste 1414c43e99fdSEd Maste @return a string containing the version number of Libevent 1415c43e99fdSEd Maste */ 1416c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1417c43e99fdSEd Maste const char *event_get_version(void); 1418c43e99fdSEd Maste 1419c43e99fdSEd Maste /** 1420c43e99fdSEd Maste Return a numeric representation of Libevent's version. 1421c43e99fdSEd Maste 1422c43e99fdSEd Maste Note that this will give you the version of the library that you're 1423c43e99fdSEd Maste currently linked against, not the version of the headers you've used to 1424c43e99fdSEd Maste compile. 1425c43e99fdSEd Maste 1426c43e99fdSEd Maste The format uses one byte each for the major, minor, and patchlevel parts of 1427c43e99fdSEd Maste the version number. The low-order byte is unused. For example, version 1428c43e99fdSEd Maste 2.0.1-alpha has a numeric representation of 0x02000100 1429c43e99fdSEd Maste */ 1430c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1431c43e99fdSEd Maste ev_uint32_t event_get_version_number(void); 1432c43e99fdSEd Maste 1433c43e99fdSEd Maste /** As event_get_version, but gives the version of Libevent's headers. */ 1434c43e99fdSEd Maste #define LIBEVENT_VERSION EVENT__VERSION 1435c43e99fdSEd Maste /** As event_get_version_number, but gives the version number of Libevent's 1436c43e99fdSEd Maste * headers. */ 1437c43e99fdSEd Maste #define LIBEVENT_VERSION_NUMBER EVENT__NUMERIC_VERSION 1438c43e99fdSEd Maste 1439c43e99fdSEd Maste /** Largest number of priorities that Libevent can support. */ 1440c43e99fdSEd Maste #define EVENT_MAX_PRIORITIES 256 1441c43e99fdSEd Maste /** 1442c43e99fdSEd Maste Set the number of different event priorities 1443c43e99fdSEd Maste 1444c43e99fdSEd Maste By default Libevent schedules all active events with the same priority. 1445c43e99fdSEd Maste However, some time it is desirable to process some events with a higher 1446c43e99fdSEd Maste priority than others. For that reason, Libevent supports strict priority 1447c43e99fdSEd Maste queues. Active events with a lower priority are always processed before 1448c43e99fdSEd Maste events with a higher priority. 1449c43e99fdSEd Maste 1450c43e99fdSEd Maste The number of different priorities can be set initially with the 1451c43e99fdSEd Maste event_base_priority_init() function. This function should be called 1452c43e99fdSEd Maste before the first call to event_base_dispatch(). The 1453c43e99fdSEd Maste event_priority_set() function can be used to assign a priority to an 1454c43e99fdSEd Maste event. By default, Libevent assigns the middle priority to all events 1455c43e99fdSEd Maste unless their priority is explicitly set. 1456c43e99fdSEd Maste 1457c43e99fdSEd Maste Note that urgent-priority events can starve less-urgent events: after 1458c43e99fdSEd Maste running all urgent-priority callbacks, Libevent checks for more urgent 1459c43e99fdSEd Maste events again, before running less-urgent events. Less-urgent events 1460c43e99fdSEd Maste will not have their callbacks run until there are no events more urgent 1461c43e99fdSEd Maste than them that want to be active. 1462c43e99fdSEd Maste 1463c43e99fdSEd Maste @param eb the event_base structure returned by event_base_new() 1464c43e99fdSEd Maste @param npriorities the maximum number of priorities 1465c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 1466c43e99fdSEd Maste @see event_priority_set() 1467c43e99fdSEd Maste */ 1468c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1469c43e99fdSEd Maste int event_base_priority_init(struct event_base *, int); 1470c43e99fdSEd Maste 1471c43e99fdSEd Maste /** 1472c43e99fdSEd Maste Get the number of different event priorities. 1473c43e99fdSEd Maste 1474c43e99fdSEd Maste @param eb the event_base structure returned by event_base_new() 1475c43e99fdSEd Maste @return Number of different event priorities 1476c43e99fdSEd Maste @see event_base_priority_init() 1477c43e99fdSEd Maste */ 1478c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1479c43e99fdSEd Maste int event_base_get_npriorities(struct event_base *eb); 1480c43e99fdSEd Maste 1481c43e99fdSEd Maste /** 1482c43e99fdSEd Maste Assign a priority to an event. 1483c43e99fdSEd Maste 1484c43e99fdSEd Maste @param ev an event struct 1485c43e99fdSEd Maste @param priority the new priority to be assigned 1486c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 1487c43e99fdSEd Maste @see event_priority_init(), event_get_priority() 1488c43e99fdSEd Maste */ 1489c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1490c43e99fdSEd Maste int event_priority_set(struct event *, int); 1491c43e99fdSEd Maste 1492c43e99fdSEd Maste /** 1493c43e99fdSEd Maste Prepare an event_base to use a large number of timeouts with the same 1494c43e99fdSEd Maste duration. 1495c43e99fdSEd Maste 1496c43e99fdSEd Maste Libevent's default scheduling algorithm is optimized for having a large 1497c43e99fdSEd Maste number of timeouts with their durations more or less randomly 1498c43e99fdSEd Maste distributed. But if you have a large number of timeouts that all have 1499c43e99fdSEd Maste the same duration (for example, if you have a large number of 1500c43e99fdSEd Maste connections that all have a 10-second timeout), then you can improve 1501c43e99fdSEd Maste Libevent's performance by telling Libevent about it. 1502c43e99fdSEd Maste 1503c43e99fdSEd Maste To do this, call this function with the common duration. It will return a 1504c43e99fdSEd Maste pointer to a different, opaque timeout value. (Don't depend on its actual 1505c43e99fdSEd Maste contents!) When you use this timeout value in event_add(), Libevent will 1506c43e99fdSEd Maste schedule the event more efficiently. 1507c43e99fdSEd Maste 1508c43e99fdSEd Maste (This optimization probably will not be worthwhile until you have thousands 1509c43e99fdSEd Maste or tens of thousands of events with the same timeout.) 1510c43e99fdSEd Maste */ 1511c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1512c43e99fdSEd Maste const struct timeval *event_base_init_common_timeout(struct event_base *base, 1513c43e99fdSEd Maste const struct timeval *duration); 1514c43e99fdSEd Maste 1515c43e99fdSEd Maste #if !defined(EVENT__DISABLE_MM_REPLACEMENT) || defined(EVENT_IN_DOXYGEN_) 1516c43e99fdSEd Maste /** 1517c43e99fdSEd Maste Override the functions that Libevent uses for memory management. 1518c43e99fdSEd Maste 1519c43e99fdSEd Maste Usually, Libevent uses the standard libc functions malloc, realloc, and 1520c43e99fdSEd Maste free to allocate memory. Passing replacements for those functions to 1521c43e99fdSEd Maste event_set_mem_functions() overrides this behavior. 1522c43e99fdSEd Maste 1523c43e99fdSEd Maste Note that all memory returned from Libevent will be allocated by the 1524c43e99fdSEd Maste replacement functions rather than by malloc() and realloc(). Thus, if you 1525c43e99fdSEd Maste have replaced those functions, it will not be appropriate to free() memory 1526c43e99fdSEd Maste that you get from Libevent. Instead, you must use the free_fn replacement 1527c43e99fdSEd Maste that you provided. 1528c43e99fdSEd Maste 1529c43e99fdSEd Maste Note also that if you are going to call this function, you should do so 1530c43e99fdSEd Maste before any call to any Libevent function that does allocation. 1531*b50261e2SCy Schubert Otherwise, those functions will allocate their memory using malloc(), but 1532c43e99fdSEd Maste then later free it using your provided free_fn. 1533c43e99fdSEd Maste 1534c43e99fdSEd Maste @param malloc_fn A replacement for malloc. 1535c43e99fdSEd Maste @param realloc_fn A replacement for realloc 1536c43e99fdSEd Maste @param free_fn A replacement for free. 1537c43e99fdSEd Maste **/ 1538c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1539c43e99fdSEd Maste void event_set_mem_functions( 1540c43e99fdSEd Maste void *(*malloc_fn)(size_t sz), 1541c43e99fdSEd Maste void *(*realloc_fn)(void *ptr, size_t sz), 1542c43e99fdSEd Maste void (*free_fn)(void *ptr)); 1543c43e99fdSEd Maste /** This definition is present if Libevent was built with support for 1544c43e99fdSEd Maste event_set_mem_functions() */ 1545c43e99fdSEd Maste #define EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED 1546c43e99fdSEd Maste #endif 1547c43e99fdSEd Maste 1548c43e99fdSEd Maste /** 1549c43e99fdSEd Maste Writes a human-readable description of all inserted and/or active 1550c43e99fdSEd Maste events to a provided stdio stream. 1551c43e99fdSEd Maste 1552c43e99fdSEd Maste This is intended for debugging; its format is not guaranteed to be the same 1553c43e99fdSEd Maste between libevent versions. 1554c43e99fdSEd Maste 1555c43e99fdSEd Maste @param base An event_base on which to scan the events. 1556c43e99fdSEd Maste @param output A stdio file to write on. 1557c43e99fdSEd Maste */ 1558c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1559c43e99fdSEd Maste void event_base_dump_events(struct event_base *, FILE *); 1560c43e99fdSEd Maste 1561c43e99fdSEd Maste 1562c43e99fdSEd Maste /** 1563c43e99fdSEd Maste Activates all pending events for the given fd and event mask. 1564c43e99fdSEd Maste 1565c43e99fdSEd Maste This function activates pending events only. Events which have not been 1566c43e99fdSEd Maste added will not become active. 1567c43e99fdSEd Maste 1568c43e99fdSEd Maste @param base the event_base on which to activate the events. 1569c43e99fdSEd Maste @param fd An fd to active events on. 1570*b50261e2SCy Schubert @param events One or more of EV_{READ,WRITE,TIMEOUT}. 1571c43e99fdSEd Maste */ 1572c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1573c43e99fdSEd Maste void event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events); 1574c43e99fdSEd Maste 1575c43e99fdSEd Maste /** 1576c43e99fdSEd Maste Activates all pending signals with a given signal number 1577c43e99fdSEd Maste 1578c43e99fdSEd Maste This function activates pending events only. Events which have not been 1579c43e99fdSEd Maste added will not become active. 1580c43e99fdSEd Maste 1581c43e99fdSEd Maste @param base the event_base on which to activate the events. 1582c43e99fdSEd Maste @param fd The signal to active events on. 1583c43e99fdSEd Maste */ 1584c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1585c43e99fdSEd Maste void event_base_active_by_signal(struct event_base *base, int sig); 1586c43e99fdSEd Maste 1587c43e99fdSEd Maste /** 1588c43e99fdSEd Maste * Callback for iterating events in an event base via event_base_foreach_event 1589c43e99fdSEd Maste */ 1590c43e99fdSEd Maste typedef int (*event_base_foreach_event_cb)(const struct event_base *, const struct event *, void *); 1591c43e99fdSEd Maste 1592c43e99fdSEd Maste /** 1593c43e99fdSEd Maste Iterate over all added or active events events in an event loop, and invoke 1594c43e99fdSEd Maste a given callback on each one. 1595c43e99fdSEd Maste 1596c43e99fdSEd Maste The callback must not call any function that modifies the event base, that 1597c43e99fdSEd Maste modifies any event in the event base, or that adds or removes any event to 1598c43e99fdSEd Maste the event base. Doing so is unsupported and will lead to undefined 1599c43e99fdSEd Maste behavior -- likely, to crashes. 1600c43e99fdSEd Maste 1601c43e99fdSEd Maste event_base_foreach_event() holds a lock on the event_base() for the whole 1602c43e99fdSEd Maste time it's running: slow callbacks are not advisable. 1603c43e99fdSEd Maste 1604c43e99fdSEd Maste Note that Libevent adds some events of its own to make pieces of its 1605c43e99fdSEd Maste functionality work. You must not assume that the only events you'll 1606c43e99fdSEd Maste encounter will be the ones you added yourself. 1607c43e99fdSEd Maste 1608c43e99fdSEd Maste The callback function must return 0 to continue iteration, or some other 1609c43e99fdSEd Maste integer to stop iterating. 1610c43e99fdSEd Maste 1611c43e99fdSEd Maste @param base An event_base on which to scan the events. 1612c43e99fdSEd Maste @param fn A callback function to receive the events. 1613c43e99fdSEd Maste @param arg An argument passed to the callback function. 1614c43e99fdSEd Maste @return 0 if we iterated over every event, or the value returned by the 1615c43e99fdSEd Maste callback function if the loop exited early. 1616c43e99fdSEd Maste */ 1617c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1618c43e99fdSEd Maste int event_base_foreach_event(struct event_base *base, event_base_foreach_event_cb fn, void *arg); 1619c43e99fdSEd Maste 1620c43e99fdSEd Maste 1621c43e99fdSEd Maste /** Sets 'tv' to the current time (as returned by gettimeofday()), 1622c43e99fdSEd Maste looking at the cached value in 'base' if possible, and calling 1623c43e99fdSEd Maste gettimeofday() or clock_gettime() as appropriate if there is no 1624c43e99fdSEd Maste cached time. 1625c43e99fdSEd Maste 1626c43e99fdSEd Maste Generally, this value will only be cached while actually 1627*b50261e2SCy Schubert processing event callbacks, and may be very inaccurate if your 1628c43e99fdSEd Maste callbacks take a long time to execute. 1629c43e99fdSEd Maste 1630c43e99fdSEd Maste Returns 0 on success, negative on failure. 1631c43e99fdSEd Maste */ 1632c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1633c43e99fdSEd Maste int event_base_gettimeofday_cached(struct event_base *base, 1634c43e99fdSEd Maste struct timeval *tv); 1635c43e99fdSEd Maste 1636c43e99fdSEd Maste /** Update cached_tv in the 'base' to the current time 1637c43e99fdSEd Maste * 1638c43e99fdSEd Maste * You can use this function is useful for selectively increasing 1639c43e99fdSEd Maste * the accuracy of the cached time value in 'base' during callbacks 1640c43e99fdSEd Maste * that take a long time to execute. 1641c43e99fdSEd Maste * 1642c43e99fdSEd Maste * This function has no effect if the base is currently not in its 1643c43e99fdSEd Maste * event loop, or if timeval caching is disabled via 1644c43e99fdSEd Maste * EVENT_BASE_FLAG_NO_CACHE_TIME. 1645c43e99fdSEd Maste * 1646c43e99fdSEd Maste * @return 0 on success, -1 on failure 1647c43e99fdSEd Maste */ 1648c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1649c43e99fdSEd Maste int event_base_update_cache_time(struct event_base *base); 1650c43e99fdSEd Maste 1651c43e99fdSEd Maste /** Release up all globally-allocated resources allocated by Libevent. 1652c43e99fdSEd Maste 1653c43e99fdSEd Maste This function does not free developer-controlled resources like 1654c43e99fdSEd Maste event_bases, events, bufferevents, listeners, and so on. It only releases 1655c43e99fdSEd Maste resources like global locks that there is no other way to free. 1656c43e99fdSEd Maste 1657c43e99fdSEd Maste It is not actually necessary to call this function before exit: every 1658c43e99fdSEd Maste resource that it frees would be released anyway on exit. It mainly exists 1659c43e99fdSEd Maste so that resource-leak debugging tools don't see Libevent as holding 1660c43e99fdSEd Maste resources at exit. 1661c43e99fdSEd Maste 1662c43e99fdSEd Maste You should only call this function when no other Libevent functions will 1663c43e99fdSEd Maste be invoked -- e.g., when cleanly exiting a program. 1664c43e99fdSEd Maste */ 1665c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1666c43e99fdSEd Maste void libevent_global_shutdown(void); 1667c43e99fdSEd Maste 1668c43e99fdSEd Maste #ifdef __cplusplus 1669c43e99fdSEd Maste } 1670c43e99fdSEd Maste #endif 1671c43e99fdSEd Maste 1672c43e99fdSEd Maste #endif /* EVENT2_EVENT_H_INCLUDED_ */ 1673