compat_libevent.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (c) 2009-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_COMPAT_LIBEVENT_H
  4. #define TOR_COMPAT_LIBEVENT_H
  5. #include "orconfig.h"
  6. #include "testsupport.h"
  7. struct event;
  8. struct event_base;
  9. #ifdef USE_BUFFEREVENTS
  10. struct bufferevent;
  11. struct ev_token_bucket_cfg;
  12. struct bufferevent_rate_limit_group;
  13. #endif
  14. #ifdef HAVE_EVENT2_EVENT_H
  15. #include <event2/util.h>
  16. #elif !defined(EVUTIL_SOCKET_DEFINED)
  17. #define EVUTIL_SOCKET_DEFINED
  18. #define evutil_socket_t int
  19. #endif
  20. void configure_libevent_logging(void);
  21. void suppress_libevent_log_msg(const char *msg);
  22. #ifdef HAVE_EVENT2_EVENT_H
  23. #define tor_event_new event_new
  24. #define tor_evtimer_new evtimer_new
  25. #define tor_evsignal_new evsignal_new
  26. #define tor_evdns_add_server_port(sock, tcp, cb, data) \
  27. evdns_add_server_port_with_base(tor_libevent_get_base(), \
  28. (sock),(tcp),(cb),(data));
  29. #else
  30. struct event *tor_event_new(struct event_base * base, evutil_socket_t sock,
  31. short what, void (*cb)(evutil_socket_t, short, void *), void *arg);
  32. struct event *tor_evtimer_new(struct event_base * base,
  33. void (*cb)(evutil_socket_t, short, void *), void *arg);
  34. struct event *tor_evsignal_new(struct event_base * base, int sig,
  35. void (*cb)(evutil_socket_t, short, void *), void *arg);
  36. #define tor_evdns_add_server_port evdns_add_server_port
  37. #endif
  38. void tor_event_free(struct event *ev);
  39. typedef struct periodic_timer_t periodic_timer_t;
  40. periodic_timer_t *periodic_timer_new(struct event_base *base,
  41. const struct timeval *tv,
  42. void (*cb)(periodic_timer_t *timer, void *data),
  43. void *data);
  44. void periodic_timer_free(periodic_timer_t *);
  45. #define tor_event_base_loopexit event_base_loopexit
  46. /** Defines a configuration for using libevent with Tor: passed as an argument
  47. * to tor_libevent_initialize() to describe how we want to set up. */
  48. typedef struct tor_libevent_cfg {
  49. /** Flag: if true, disable IOCP (assuming that it could be enabled). */
  50. int disable_iocp;
  51. /** How many CPUs should we use (relevant only with IOCP). */
  52. int num_cpus;
  53. /** How many milliseconds should we allow between updating bandwidth limits?
  54. * (relevant only with bufferevents). */
  55. int msec_per_tick;
  56. } tor_libevent_cfg;
  57. void tor_libevent_initialize(tor_libevent_cfg *cfg);
  58. MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
  59. const char *tor_libevent_get_method(void);
  60. void tor_check_libevent_header_compatibility(void);
  61. const char *tor_libevent_get_version_str(void);
  62. const char *tor_libevent_get_header_version_str(void);
  63. #ifdef USE_BUFFEREVENTS
  64. const struct timeval *tor_libevent_get_one_tick_timeout(void);
  65. int tor_libevent_using_iocp_bufferevents(void);
  66. int tor_set_bufferevent_rate_limit(struct bufferevent *bev,
  67. struct ev_token_bucket_cfg *cfg);
  68. int tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
  69. struct bufferevent_rate_limit_group *g);
  70. #endif
  71. int tor_init_libevent_rng(void);
  72. void tor_gettimeofday_cached(struct timeval *tv);
  73. void tor_gettimeofday_cache_clear(void);
  74. #ifdef TOR_UNIT_TESTS
  75. void tor_gettimeofday_cache_set(const struct timeval *tv);
  76. #endif
  77. void tor_gettimeofday_cached_monotonic(struct timeval *tv);
  78. #endif