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