Skip to content
This repository
Browse code

Add a new libevent_global_shutdown() to free all globals before exiting.

Mark Ellzey added a function libevent_shutdown() which calls a set of
private functions:

       * event_free_globals()
       * event_free_debug_globals()
       * event_free_debug_globals_locks()
       * event_free_evsig_globals()
       * evsig_free_globals()
       * evsig_free_globals_locks()
       * evutil_free_globals()
       * evutil_free_secure_rng_globals()
       * evutil_free_secure_rng_globals_lock()

Nick tweaked this libevent global shutdown code:

  - rename the function to emphasize that it's for global resources
  - write more in the doxygen
  - make function brace style consistent
  - add a missing void in a function definition.
  • Loading branch information...
commit 041ca00c754eed316b7e3066613abee620aae9b5 1 parent 24dab0b
Mark Ellzey ellzey authored Nick Mathewson committed
45 event.c
@@ -3124,6 +3124,51 @@ event_base_del_virtual_(struct event_base *base)
3124 3124 EVBASE_RELEASE_LOCK(base, th_base_lock);
3125 3125 }
3126 3126
  3127 +static void
  3128 +event_free_debug_globals_locks(void)
  3129 +{
  3130 +#ifndef EVENT__DISABLE_THREAD_SUPPORT
  3131 +#ifndef EVENT__DISABLE_DEBUG_MODE
  3132 + if (event_debug_map_lock_ != NULL) {
  3133 + EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0);
  3134 + }
  3135 +#endif /* EVENT__DISABLE_DEBUG_MODE */
  3136 +#endif /* EVENT__DISABLE_THREAD_SUPPORT */
  3137 + return;
  3138 +}
  3139 +
  3140 +static void
  3141 +event_free_debug_globals(void)
  3142 +{
  3143 + event_free_debug_globals_locks();
  3144 +}
  3145 +
  3146 +static void
  3147 +event_free_evsig_globals(void)
  3148 +{
  3149 + evsig_free_globals_();
  3150 +}
  3151 +
  3152 +static void
  3153 +event_free_evutil_globals(void)
  3154 +{
  3155 + evutil_free_globals_();
  3156 +}
  3157 +
  3158 +static void
  3159 +event_free_globals(void)
  3160 +{
  3161 + event_free_debug_globals();
  3162 + event_free_evsig_globals();
  3163 + event_free_evutil_globals();
  3164 +}
  3165 +
  3166 +void
  3167 +libevent_global_shutdown(void)
  3168 +{
  3169 + event_free_globals();
  3170 +}
  3171 +
3127 3172 #ifndef EVENT__DISABLE_THREAD_SUPPORT
3128 3173 int
3129 3174 event_global_setup_locks_(const int enable_locks)
1  evsignal-internal.h
@@ -60,5 +60,6 @@ int evsig_init_(struct event_base *);
60 60 void evsig_dealloc_(struct event_base *);
61 61
62 62 void evsig_set_base_(struct event_base *base);
  63 +void evsig_free_globals_(void);
63 64
64 65 #endif /* EVSIGNAL_INTERNAL_H_INCLUDED_ */
5 evutil.c
@@ -2543,3 +2543,8 @@ evutil_eventfd_(unsigned initval, int flags)
2543 2543 #endif
2544 2544 }
2545 2545
  2546 +void
  2547 +evutil_free_globals_(void)
  2548 +{
  2549 + evutil_free_secure_rng_globals_();
  2550 +}
18 evutil_rand.c
@@ -50,11 +50,13 @@ evutil_secure_rng_init(void)
50 50 (void) arc4random();
51 51 return 0;
52 52 }
  53 +#ifndef EVENT__DISABLE_THREAD_SUPPORT
53 54 int
54 55 evutil_secure_rng_global_setup_locks_(const int enable_locks)
55 56 {
56 57 return 0;
57 58 }
  59 +#endif
58 60
59 61 static void
60 62 ev_arc4random_buf(void *buf, size_t n)
@@ -112,6 +114,22 @@ evutil_secure_rng_global_setup_locks_(const int enable_locks)
112 114 }
113 115 #endif
114 116
  117 +static void
  118 +evutil_free_secure_rng_globals_locks(void)
  119 +{
  120 +#ifndef EVENT__DISABLE_THREAD_SUPPORT
  121 + if (arc4rand_lock != NULL) {
  122 + EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
  123 + }
  124 +#endif
  125 + return;
  126 +}
  127 +void
  128 +evutil_free_secure_rng_globals_(void) {
  129 +{
  130 + evutil_free_secure_rng_globals_locks();
  131 +}
  132 +
115 133 int
116 134 evutil_secure_rng_init(void)
117 135 {
16 include/event2/event.h
@@ -1310,6 +1310,22 @@ int event_base_gettimeofday_cached(struct event_base *base,
1310 1310 */
1311 1311 int event_base_update_cache_time(struct event_base *base);
1312 1312
  1313 +/** Release up all globally-allocated resources allocated by Libevent.
  1314 +
  1315 + This function does not free developer-controlled resources like
  1316 + event_bases, events, bufferevents, listeners, and so on. It only releases
  1317 + resources like global locks that there is no other way to free.
  1318 +
  1319 + It is not actually necessary to call this function before exit: every
  1320 + resource that it frees would be released anyway on exit. It mainly exists
  1321 + so that resource-leak debugging tools don't see Libevent as holding
  1322 + resources at exit.
  1323 +
  1324 + You should only call this function when no other Libevent functions will
  1325 + be invoked -- e.g., when cleanly exiting a program.
  1326 + */
  1327 +void libevent_global_shutdown(void);
  1328 +
1313 1329 #ifdef __cplusplus
1314 1330 }
1315 1331 #endif
1  sample/event-read-fifo.c
@@ -156,6 +156,7 @@ main(int argc, char **argv)
156 156 close(socket);
157 157 unlink(fifo);
158 158 #endif
  159 + libevent_global_shutdown();
159 160 return (0);
160 161 }
161 162
18 signal.c
@@ -449,6 +449,23 @@ evsig_dealloc_(struct event_base *base)
449 449 }
450 450 }
451 451
  452 +static void
  453 +evsig_free_globals_locks(void)
  454 +{
  455 +#ifndef EVENT__DISABLE_THREAD_SUPPORT
  456 + if (evsig_base_lock != NULL) {
  457 + EVTHREAD_FREE_LOCK(evsig_base_lock, 0);
  458 + }
  459 +#endif
  460 + return;
  461 +}
  462 +
  463 +void
  464 +evsig_free_globals_(void)
  465 +{
  466 + evsig_free_globals_locks();
  467 +}
  468 +
452 469 #ifndef EVENT__DISABLE_THREAD_SUPPORT
453 470 int
454 471 evsig_global_setup_locks_(const int enable_locks)
@@ -456,4 +473,5 @@ evsig_global_setup_locks_(const int enable_locks)
456 473 EVTHREAD_SETUP_GLOBAL_LOCK(evsig_base_lock, 0);
457 474 return 0;
458 475 }
  476 +
459 477 #endif
3  util-internal.h
@@ -347,6 +347,9 @@ int evutil_hex_char_to_int_(char c);
347 347
348 348 void evutil_usleep_(const struct timeval *tv);
349 349
  350 +void evutil_free_secure_rng_globals_(void);
  351 +void evutil_free_globals_(void);
  352 +
350 353 #ifdef _WIN32
351 354 HANDLE evutil_load_windows_system_library_(const TCHAR *library_name);
352 355 #endif

0 comments on commit 041ca00

Please sign in to comment.
Something went wrong with that request. Please try again.