tor_threads.c 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "app/main/tor_threads.h"
  4. #include "lib/thread/threads.h"
  5. #include "app/main/subsysmgr.h"
  6. /** Wraps a void (*)(void*) function and its argument so we can
  7. * invoke them with proper subsystem thread management.
  8. */
  9. typedef struct tor_thread_helper_info_t {
  10. void (*func)(void *);
  11. void *data;
  12. } tor_thread_helper_info_t;
  13. static void tor_thread_helper(void *_info) ATTR_NORETURN;
  14. static void
  15. tor_thread_helper(void *_info)
  16. {
  17. tor_thread_helper_info_t *info = _info;
  18. void (*func)(void*) = info->func;
  19. void *data = info->data;
  20. tor_free(_info);
  21. subsystems_thread_init();
  22. func(data);
  23. subsystems_thread_cleanup();
  24. spawn_exit();
  25. }
  26. int
  27. start_tor_thread(void (*func)(void *), void *data)
  28. {
  29. tor_thread_helper_info_t *d = tor_malloc(sizeof(tor_thread_helper_info_t));
  30. d->func = func;
  31. d->data = data;
  32. return spawn_func(tor_thread_helper, d);
  33. }