compat_libevent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (c) 2009-2018, 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. void configure_libevent_logging(void);
  13. void suppress_libevent_log_msg(const char *msg);
  14. #define tor_event_new event_new
  15. #define tor_evtimer_new evtimer_new
  16. #define tor_evsignal_new evsignal_new
  17. #define tor_evdns_add_server_port(sock, tcp, cb, data) \
  18. evdns_add_server_port_with_base(tor_libevent_get_base(), \
  19. (sock),(tcp),(cb),(data));
  20. struct event;
  21. struct event_base;
  22. struct timeval;
  23. void tor_event_free_(struct event *ev);
  24. #define tor_event_free(ev) \
  25. FREE_AND_NULL(struct event, tor_event_free_, (ev))
  26. typedef struct periodic_timer_t periodic_timer_t;
  27. periodic_timer_t *periodic_timer_new(struct event_base *base,
  28. const struct timeval *tv,
  29. void (*cb)(periodic_timer_t *timer, void *data),
  30. void *data);
  31. void periodic_timer_free_(periodic_timer_t *);
  32. void periodic_timer_launch(periodic_timer_t *, const struct timeval *tv);
  33. void periodic_timer_disable(periodic_timer_t *);
  34. #define periodic_timer_free(t) \
  35. FREE_AND_NULL(periodic_timer_t, periodic_timer_free_, (t))
  36. typedef struct mainloop_event_t mainloop_event_t;
  37. mainloop_event_t *mainloop_event_new(void (*cb)(mainloop_event_t *, void *),
  38. void *userdata);
  39. mainloop_event_t * mainloop_event_postloop_new(
  40. void (*cb)(mainloop_event_t *, void *),
  41. void *userdata);
  42. void mainloop_event_activate(mainloop_event_t *event);
  43. int mainloop_event_schedule(mainloop_event_t *event,
  44. const struct timeval *delay);
  45. void mainloop_event_cancel(mainloop_event_t *event);
  46. void mainloop_event_free_(mainloop_event_t *event);
  47. #define mainloop_event_free(event) \
  48. FREE_AND_NULL(mainloop_event_t, mainloop_event_free_, (event))
  49. /** Defines a configuration for using libevent with Tor: passed as an argument
  50. * to tor_libevent_initialize() to describe how we want to set up. */
  51. typedef struct tor_libevent_cfg {
  52. /** How many CPUs should we use (not currently useful). */
  53. int num_cpus;
  54. /** How many milliseconds should we allow between updating bandwidth limits?
  55. * (Not currently useful). */
  56. int msec_per_tick;
  57. } tor_libevent_cfg;
  58. void tor_libevent_initialize(tor_libevent_cfg *cfg);
  59. MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
  60. const char *tor_libevent_get_method(void);
  61. void tor_check_libevent_header_compatibility(void);
  62. const char *tor_libevent_get_version_str(void);
  63. const char *tor_libevent_get_header_version_str(void);
  64. void tor_libevent_free_all(void);
  65. int tor_init_libevent_rng(void);
  66. #ifdef TOR_UNIT_TESTS
  67. void tor_libevent_postfork(void);
  68. #endif
  69. int tor_libevent_run_event_loop(struct event_base *base, int once);
  70. void tor_libevent_exit_loop_after_delay(struct event_base *base,
  71. const struct timeval *delay);
  72. void tor_libevent_exit_loop_after_callback(struct event_base *base);
  73. #ifdef COMPAT_LIBEVENT_PRIVATE
  74. /** Macro: returns the number of a Libevent version as a 4-byte number,
  75. with the first three bytes representing the major, minor, and patchlevel
  76. respectively of the library. The fourth byte is unused.
  77. This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
  78. 2.0.1 or later. */
  79. #define V(major, minor, patch) \
  80. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  81. STATIC void
  82. libevent_logging_callback(int severity, const char *msg);
  83. #endif /* defined(COMPAT_LIBEVENT_PRIVATE) */
  84. #endif /* !defined(TOR_COMPAT_LIBEVENT_H) */