event.h 61 KB

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