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