compat_libevent.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2009-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file compat_libevent.h
  5. * \brief Header for compat_libevent.c
  6. **/
  7. #ifndef TOR_COMPAT_LIBEVENT_H
  8. #define TOR_COMPAT_LIBEVENT_H
  9. #include "orconfig.h"
  10. #include "lib/testsupport/testsupport.h"
  11. #include "lib/malloc/malloc.h"
  12. #include "lib/thread/threads.h"
  13. #include <stdbool.h>
  14. void configure_libevent_logging(void);
  15. void suppress_libevent_log_msg(const char *msg);
  16. #define tor_event_new event_new
  17. #define tor_evtimer_new evtimer_new
  18. #define tor_evsignal_new evsignal_new
  19. #define tor_evdns_add_server_port(sock, tcp, cb, data) \
  20. evdns_add_server_port_with_base(tor_libevent_get_base(), \
  21. (sock),(tcp),(cb),(data));
  22. struct event;
  23. struct event_base;
  24. struct timeval;
  25. void tor_event_free_(struct event *ev);
  26. #define tor_event_free(ev) \
  27. FREE_AND_NULL(struct event, tor_event_free_, (ev))
  28. typedef struct periodic_timer_t periodic_timer_t;
  29. periodic_timer_t *periodic_timer_new(struct event_base *base,
  30. const struct timeval *tv,
  31. void (*cb)(periodic_timer_t *timer, void *data),
  32. void *data);
  33. void periodic_timer_free_(periodic_timer_t *);
  34. void periodic_timer_launch(periodic_timer_t *, const struct timeval *tv);
  35. void periodic_timer_disable(periodic_timer_t *);
  36. #define periodic_timer_free(t) \
  37. FREE_AND_NULL(periodic_timer_t, periodic_timer_free_, (t))
  38. typedef struct mainloop_event_t mainloop_event_t;
  39. mainloop_event_t *mainloop_event_new(void (*cb)(mainloop_event_t *, void *),
  40. void *userdata);
  41. mainloop_event_t * mainloop_event_postloop_new(
  42. void (*cb)(mainloop_event_t *, void *),
  43. void *userdata);
  44. void mainloop_event_activate(mainloop_event_t *event);
  45. int mainloop_event_schedule(mainloop_event_t *event,
  46. const struct timeval *delay);
  47. void mainloop_event_cancel(mainloop_event_t *event);
  48. void mainloop_event_free_(mainloop_event_t *event);
  49. #define mainloop_event_free(event) \
  50. FREE_AND_NULL(mainloop_event_t, mainloop_event_free_, (event))
  51. /** Defines a configuration for using libevent with Tor: passed as an argument
  52. * to tor_libevent_initialize() to describe how we want to set up. */
  53. typedef struct tor_libevent_cfg {
  54. /** How many CPUs should we use (not currently useful). */
  55. int num_cpus;
  56. /** How many milliseconds should we allow between updating bandwidth limits?
  57. * (Not currently useful). */
  58. int msec_per_tick;
  59. } tor_libevent_cfg;
  60. struct event_base *get_eventloop(int index);
  61. int get_local_eventloop_index(void);
  62. int get_num_eventloops(void);
  63. void rescan_eventloops(void);
  64. void tor_evloop_init_threadlocals(void);
  65. void tor_evloop_destroy_threadlocals(void);
  66. void start_eventloop_threads(void (*func)(void),
  67. tor_thread_t *(*spawn_fn)(void (*func)(void *),
  68. void *data));
  69. void join_eventloop_threads(void);
  70. void tor_libevent_initialize(tor_libevent_cfg *cfg,
  71. int num_additional_eventloops);
  72. bool tor_libevent_is_initialized(void);
  73. MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
  74. const char *tor_libevent_get_method(void);
  75. void tor_check_libevent_header_compatibility(void);
  76. const char *tor_libevent_get_version_str(void);
  77. const char *tor_libevent_get_header_version_str(void);
  78. void tor_libevent_free_all(void);
  79. int tor_init_libevent_rng(void);
  80. #ifdef TOR_UNIT_TESTS
  81. void tor_libevent_postfork(void);
  82. #endif
  83. int tor_libevent_run_event_loop(struct event_base *base, int once);
  84. void tor_libevent_exit_loop_after_delay(struct event_base *base,
  85. const struct timeval *delay);
  86. void tor_libevent_exit_loop_after_callback(struct event_base *base);
  87. #ifdef COMPAT_LIBEVENT_PRIVATE
  88. /** Macro: returns the number of a Libevent version as a 4-byte number,
  89. with the first three bytes representing the major, minor, and patchlevel
  90. respectively of the library. The fourth byte is unused.
  91. This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
  92. 2.0.1 or later. */
  93. #define V(major, minor, patch) \
  94. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  95. STATIC void
  96. libevent_logging_callback(int severity, const char *msg);
  97. #endif /* defined(COMPAT_LIBEVENT_PRIVATE) */
  98. #endif /* !defined(TOR_COMPAT_LIBEVENT_H) */