compat_threads.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_COMPAT_THREADS_H
  6. #define TOR_COMPAT_THREADS_H
  7. #include "orconfig.h"
  8. #include "torint.h"
  9. #include "testsupport.h"
  10. #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
  11. #include <pthread.h>
  12. #endif
  13. #if defined(_WIN32)
  14. #define USE_WIN32_THREADS
  15. #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  16. #define USE_PTHREADS
  17. #else
  18. #error "No threading system was found"
  19. #endif
  20. int spawn_func(void (*func)(void *), void *data);
  21. void spawn_exit(void) ATTR_NORETURN;
  22. /* Because we use threads instead of processes on most platforms (Windows,
  23. * Linux, etc), we need locking for them. On platforms with poor thread
  24. * support or broken gethostbyname_r, these functions are no-ops. */
  25. /** A generic lock structure for multithreaded builds. */
  26. typedef struct tor_mutex_t {
  27. #if defined(USE_WIN32_THREADS)
  28. /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
  29. CRITICAL_SECTION mutex;
  30. #elif defined(USE_PTHREADS)
  31. /** Pthreads-only: with pthreads, we implement locks with
  32. * pthread_mutex_t. */
  33. pthread_mutex_t mutex;
  34. #else
  35. /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
  36. int _unused;
  37. #endif
  38. } tor_mutex_t;
  39. tor_mutex_t *tor_mutex_new(void);
  40. tor_mutex_t *tor_mutex_new_nonrecursive(void);
  41. void tor_mutex_init(tor_mutex_t *m);
  42. void tor_mutex_init_nonrecursive(tor_mutex_t *m);
  43. void tor_mutex_acquire(tor_mutex_t *m);
  44. void tor_mutex_release(tor_mutex_t *m);
  45. void tor_mutex_free(tor_mutex_t *m);
  46. void tor_mutex_uninit(tor_mutex_t *m);
  47. unsigned long tor_get_thread_id(void);
  48. void tor_threads_init(void);
  49. /** Conditions need nonrecursive mutexes with pthreads. */
  50. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  51. void set_main_thread(void);
  52. int in_main_thread(void);
  53. typedef struct tor_cond_t {
  54. #ifdef USE_PTHREADS
  55. pthread_cond_t cond;
  56. #elif defined(USE_WIN32_THREADS)
  57. HANDLE event;
  58. CRITICAL_SECTION lock;
  59. int n_waiting;
  60. int n_to_wake;
  61. int generation;
  62. #else
  63. #error no known condition implementation.
  64. #endif
  65. } tor_cond_t;
  66. tor_cond_t *tor_cond_new(void);
  67. void tor_cond_free(tor_cond_t *cond);
  68. int tor_cond_init(tor_cond_t *cond);
  69. void tor_cond_uninit(tor_cond_t *cond);
  70. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  71. const struct timeval *tv);
  72. void tor_cond_signal_one(tor_cond_t *cond);
  73. void tor_cond_signal_all(tor_cond_t *cond);
  74. /** Helper type used to manage waking up the main thread while it's in
  75. * the libevent main loop. Used by the work queue code. */
  76. typedef struct alert_sockets_s {
  77. /* XXXX This structure needs a better name. */
  78. /** Socket that the main thread should listen for EV_READ events on.
  79. * Note that this socket may be a regular fd on a non-Windows platform.
  80. */
  81. tor_socket_t read_fd;
  82. /** Socket to use when alerting the main thread. */
  83. tor_socket_t write_fd;
  84. /** Function to alert the main thread */
  85. int (*alert_fn)(tor_socket_t write_fd);
  86. /** Function to make the main thread no longer alerted. */
  87. int (*drain_fn)(tor_socket_t read_fd);
  88. } alert_sockets_t;
  89. /* Flags to disable one or more alert_sockets backends. */
  90. #define ASOCKS_NOEVENTFD2 (1u<<0)
  91. #define ASOCKS_NOEVENTFD (1u<<1)
  92. #define ASOCKS_NOPIPE2 (1u<<2)
  93. #define ASOCKS_NOPIPE (1u<<3)
  94. #define ASOCKS_NOSOCKETPAIR (1u<<4)
  95. int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
  96. void alert_sockets_close(alert_sockets_t *socks);
  97. #endif