Tor  0.4.5.0-alpha-dev
compat_libevent.h
Go to the documentation of this file.
1 /* Copyright (c) 2009-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file compat_libevent.h
6  * \brief Header for compat_libevent.c
7  **/
8 
9 #ifndef TOR_COMPAT_LIBEVENT_H
10 #define TOR_COMPAT_LIBEVENT_H
11 
12 #include "orconfig.h"
14 #include "lib/malloc/malloc.h"
15 
16 #include <stdbool.h>
17 
19 void suppress_libevent_log_msg(const char *msg);
20 
21 #define tor_event_new event_new
22 #define tor_evtimer_new evtimer_new
23 #define tor_evsignal_new evsignal_new
24 #define tor_evdns_add_server_port(sock, tcp, cb, data) \
25  evdns_add_server_port_with_base(tor_libevent_get_base(), \
26  (sock),(tcp),(cb),(data));
27 
28 struct event;
29 struct event_base;
30 struct timeval;
31 
32 void tor_event_free_(struct event *ev);
33 #define tor_event_free(ev) \
34  FREE_AND_NULL(struct event, tor_event_free_, (ev))
35 
36 typedef struct periodic_timer_t periodic_timer_t;
37 
38 periodic_timer_t *periodic_timer_new(struct event_base *base,
39  const struct timeval *tv,
40  void (*cb)(periodic_timer_t *timer, void *data),
41  void *data);
43 void periodic_timer_launch(periodic_timer_t *, const struct timeval *tv);
45 #define periodic_timer_free(t) \
46  FREE_AND_NULL(periodic_timer_t, periodic_timer_free_, (t))
47 
48 typedef struct mainloop_event_t mainloop_event_t;
50  void *userdata);
52  void (*cb)(mainloop_event_t *, void *),
53  void *userdata);
56  const struct timeval *delay);
59 #define mainloop_event_free(event) \
60  FREE_AND_NULL(mainloop_event_t, mainloop_event_free_, (event))
61 
62 /** Defines a configuration for using libevent with Tor: passed as an argument
63  * to tor_libevent_initialize() to describe how we want to set up. */
64 typedef struct tor_libevent_cfg_t {
65  /** How many CPUs should we use (not currently useful). */
66  int num_cpus;
67  /** How many milliseconds should we allow between updating bandwidth limits?
68  * (Not currently useful). */
71 
74 MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
75 const char *tor_libevent_get_method(void);
76 void tor_check_libevent_header_compatibility(void);
77 const char *tor_libevent_get_version_str(void);
78 const char *tor_libevent_get_header_version_str(void);
79 void tor_libevent_free_all(void);
80 
81 int tor_init_libevent_rng(void);
82 
83 #ifdef TOR_UNIT_TESTS
84 void tor_libevent_postfork(void);
85 #endif
86 
87 int tor_libevent_run_event_loop(struct event_base *base, int once);
88 void tor_libevent_exit_loop_after_delay(struct event_base *base,
89  const struct timeval *delay);
90 void tor_libevent_exit_loop_after_callback(struct event_base *base);
91 
92 #ifdef COMPAT_LIBEVENT_PRIVATE
93 
94 /** Macro: returns the number of a Libevent version as a 4-byte number,
95  with the first three bytes representing the major, minor, and patchlevel
96  respectively of the library. The fourth byte is unused.
97 
98  This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
99  2.0.1 or later. */
100 #define V(major, minor, patch) \
101  (((major) << 24) | ((minor) << 16) | ((patch) << 8))
102 
103 STATIC void
104 libevent_logging_callback(int severity, const char *msg);
105 #endif /* defined(COMPAT_LIBEVENT_PRIVATE) */
106 
107 #endif /* !defined(TOR_COMPAT_LIBEVENT_H) */
mainloop_event_new
mainloop_event_t * mainloop_event_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
Definition: compat_libevent.c:396
suppress_libevent_log_msg
void suppress_libevent_log_msg(const char *msg)
Definition: compat_libevent.c:65
mainloop_event_free_
void mainloop_event_free_(mainloop_event_t *event)
Definition: compat_libevent.c:467
tor_libevent_cfg_t
Definition: compat_libevent.h:64
periodic_timer_t
Definition: compat_libevent.c:226
tor_libevent_run_event_loop
int tor_libevent_run_event_loop(struct event_base *base, int once)
Definition: compat_libevent.c:513
tor_libevent_free_all
void tor_libevent_free_all(void)
Definition: compat_libevent.c:496
libevent_logging_callback
STATIC void libevent_logging_callback(int severity, const char *msg)
Definition: compat_libevent.c:27
tor_libevent_get_version_str
const char * tor_libevent_get_version_str(void)
Definition: compat_libevent.c:212
mainloop_event_postloop_new
mainloop_event_t * mainloop_event_postloop_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
Definition: compat_libevent.c:410
tor_libevent_exit_loop_after_delay
void tor_libevent_exit_loop_after_delay(struct event_base *base, const struct timeval *delay)
Definition: compat_libevent.c:522
periodic_timer_t::data
void * data
Definition: compat_libevent.c:232
tor_libevent_get_header_version_str
const char * tor_libevent_get_header_version_str(void)
Definition: compat_libevent.c:220
periodic_timer_disable
void periodic_timer_disable(periodic_timer_t *)
Definition: compat_libevent.c:293
mainloop_event_schedule
int mainloop_event_schedule(mainloop_event_t *event, const struct timeval *delay)
Definition: compat_libevent.c:443
mainloop_event_cancel
void mainloop_event_cancel(mainloop_event_t *event)
Definition: compat_libevent.c:458
tor_libevent_is_initialized
bool tor_libevent_is_initialized(void)
Definition: compat_libevent.c:189
mainloop_event_t
Definition: compat_libevent.c:320
tor_libevent_cfg_t::msec_per_tick
int msec_per_tick
Definition: compat_libevent.h:69
configure_libevent_logging
void configure_libevent_logging(void)
Definition: compat_libevent.c:58
MOCK_DECL
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
malloc.h
Headers for util_malloc.c.
periodic_timer_t::cb
void(* cb)(struct periodic_timer_t *, void *)
Definition: compat_libevent.c:230
tor_libevent_cfg_t::num_cpus
int num_cpus
Definition: compat_libevent.h:66
timeval
Definition: compat_time.h:151
mainloop_event_activate
void mainloop_event_activate(mainloop_event_t *event)
Definition: compat_libevent.c:425
testsupport.h
Macros to implement mocking and selective exposure for the test code.
periodic_timer_new
periodic_timer_t * periodic_timer_new(struct event_base *base, const struct timeval *tv, void(*cb)(periodic_timer_t *timer, void *data), void *data)
Definition: compat_libevent.c:249
tor_libevent_initialize
void tor_libevent_initialize(tor_libevent_cfg_t *cfg)
Definition: compat_libevent.c:133
periodic_timer_launch
void periodic_timer_launch(periodic_timer_t *, const struct timeval *tv)
Definition: compat_libevent.c:277
tor_libevent_get_method
const char * tor_libevent_get_method(void)
Definition: compat_libevent.c:204
tor_libevent_exit_loop_after_callback
void tor_libevent_exit_loop_after_callback(struct event_base *base)
Definition: compat_libevent.c:531
STATIC
#define STATIC
Definition: testsupport.h:32
periodic_timer_free_
void periodic_timer_free_(periodic_timer_t *)
Definition: compat_libevent.c:301
tor_libevent_get_base
struct event_base * tor_libevent_get_base(void)
Definition: compat_libevent.c:196