compat_threads.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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(HAVE_STDATOMIC_H) && defined(STDATOMIC_WORKS)
  14. #define HAVE_WORKING_STDATOMIC
  15. #endif
  16. #ifdef HAVE_WORKING_STDATOMIC
  17. #include <stdatomic.h>
  18. #endif
  19. #if defined(_WIN32)
  20. #define USE_WIN32_THREADS
  21. #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  22. #define USE_PTHREADS
  23. #else
  24. #error "No threading system was found"
  25. #endif /* defined(_WIN32) || ... */
  26. int spawn_func(void (*func)(void *), void *data);
  27. void spawn_exit(void) ATTR_NORETURN;
  28. /* Because we use threads instead of processes on most platforms (Windows,
  29. * Linux, etc), we need locking for them. On platforms with poor thread
  30. * support or broken gethostbyname_r, these functions are no-ops. */
  31. /** A generic lock structure for multithreaded builds. */
  32. typedef struct tor_mutex_t {
  33. #if defined(USE_WIN32_THREADS)
  34. /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
  35. CRITICAL_SECTION mutex;
  36. #elif defined(USE_PTHREADS)
  37. /** Pthreads-only: with pthreads, we implement locks with
  38. * pthread_mutex_t. */
  39. pthread_mutex_t mutex;
  40. #else
  41. /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
  42. int _unused;
  43. #endif /* defined(USE_WIN32_THREADS) || ... */
  44. } tor_mutex_t;
  45. tor_mutex_t *tor_mutex_new(void);
  46. tor_mutex_t *tor_mutex_new_nonrecursive(void);
  47. void tor_mutex_init(tor_mutex_t *m);
  48. void tor_mutex_init_nonrecursive(tor_mutex_t *m);
  49. void tor_mutex_acquire(tor_mutex_t *m);
  50. void tor_mutex_release(tor_mutex_t *m);
  51. void tor_mutex_free_(tor_mutex_t *m);
  52. #define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
  53. void tor_mutex_uninit(tor_mutex_t *m);
  54. unsigned long tor_get_thread_id(void);
  55. void tor_threads_init(void);
  56. /** Conditions need nonrecursive mutexes with pthreads. */
  57. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  58. void set_main_thread(void);
  59. int in_main_thread(void);
  60. typedef struct tor_cond_t {
  61. #ifdef USE_PTHREADS
  62. pthread_cond_t cond;
  63. #elif defined(USE_WIN32_THREADS)
  64. HANDLE event;
  65. CRITICAL_SECTION lock;
  66. int n_waiting;
  67. int n_to_wake;
  68. int generation;
  69. #else
  70. #error no known condition implementation.
  71. #endif /* defined(USE_PTHREADS) || ... */
  72. } tor_cond_t;
  73. tor_cond_t *tor_cond_new(void);
  74. void tor_cond_free_(tor_cond_t *cond);
  75. #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
  76. int tor_cond_init(tor_cond_t *cond);
  77. void tor_cond_uninit(tor_cond_t *cond);
  78. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  79. const struct timeval *tv);
  80. void tor_cond_signal_one(tor_cond_t *cond);
  81. void tor_cond_signal_all(tor_cond_t *cond);
  82. /** Helper type used to manage waking up the main thread while it's in
  83. * the libevent main loop. Used by the work queue code. */
  84. typedef struct alert_sockets_s {
  85. /* XXXX This structure needs a better name. */
  86. /** Socket that the main thread should listen for EV_READ events on.
  87. * Note that this socket may be a regular fd on a non-Windows platform.
  88. */
  89. tor_socket_t read_fd;
  90. /** Socket to use when alerting the main thread. */
  91. tor_socket_t write_fd;
  92. /** Function to alert the main thread */
  93. int (*alert_fn)(tor_socket_t write_fd);
  94. /** Function to make the main thread no longer alerted. */
  95. int (*drain_fn)(tor_socket_t read_fd);
  96. } alert_sockets_t;
  97. /* Flags to disable one or more alert_sockets backends. */
  98. #define ASOCKS_NOEVENTFD2 (1u<<0)
  99. #define ASOCKS_NOEVENTFD (1u<<1)
  100. #define ASOCKS_NOPIPE2 (1u<<2)
  101. #define ASOCKS_NOPIPE (1u<<3)
  102. #define ASOCKS_NOSOCKETPAIR (1u<<4)
  103. int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
  104. void alert_sockets_close(alert_sockets_t *socks);
  105. typedef struct tor_threadlocal_s {
  106. #ifdef _WIN32
  107. DWORD index;
  108. #else
  109. pthread_key_t key;
  110. #endif
  111. } tor_threadlocal_t;
  112. /** Initialize a thread-local variable.
  113. *
  114. * After you call this function on a tor_threadlocal_t, you can call
  115. * tor_threadlocal_set to change the current value of this variable for the
  116. * current thread, and tor_threadlocal_get to retrieve the current value for
  117. * the current thread. Each thread has its own value.
  118. **/
  119. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  120. /**
  121. * Release all resource associated with a thread-local variable.
  122. */
  123. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  124. /**
  125. * Return the current value of a thread-local variable for this thread.
  126. *
  127. * It's undefined behavior to use this function if the threadlocal hasn't
  128. * been initialized, or has been destroyed.
  129. */
  130. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  131. /**
  132. * Change the current value of a thread-local variable for this thread to
  133. * <b>value</b>.
  134. *
  135. * It's undefined behavior to use this function if the threadlocal hasn't
  136. * been initialized, or has been destroyed.
  137. */
  138. void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
  139. /**
  140. * Atomic counter type; holds a size_t value.
  141. */
  142. #ifdef HAVE_WORKING_STDATOMIC
  143. typedef struct atomic_counter_t {
  144. atomic_size_t val;
  145. } atomic_counter_t;
  146. #define ATOMIC_LINKAGE static
  147. #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
  148. typedef struct atomic_counter_t {
  149. tor_mutex_t mutex;
  150. size_t val;
  151. } atomic_counter_t;
  152. #define ATOMIC_LINKAGE
  153. #endif /* defined(HAVE_WORKING_STDATOMIC) */
  154. ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
  155. ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
  156. ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
  157. ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  158. ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
  159. ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
  160. size_t newval);
  161. #undef ATOMIC_LINKAGE
  162. #ifdef HAVE_WORKING_STDATOMIC
  163. /** Initialize a new atomic counter with the value 0 */
  164. static inline void
  165. atomic_counter_init(atomic_counter_t *counter)
  166. {
  167. atomic_init(&counter->val, 0);
  168. }
  169. /** Clean up all resources held by an atomic counter. */
  170. static inline void
  171. atomic_counter_destroy(atomic_counter_t *counter)
  172. {
  173. (void)counter;
  174. }
  175. /** Add a value to an atomic counter. */
  176. static inline void
  177. atomic_counter_add(atomic_counter_t *counter, size_t add)
  178. {
  179. (void) atomic_fetch_add(&counter->val, add);
  180. }
  181. /** Subtract a value from an atomic counter. */
  182. static inline void
  183. atomic_counter_sub(atomic_counter_t *counter, size_t sub)
  184. {
  185. (void) atomic_fetch_sub(&counter->val, sub);
  186. }
  187. /** Return the current value of an atomic counter */
  188. static inline size_t
  189. atomic_counter_get(atomic_counter_t *counter)
  190. {
  191. return atomic_load(&counter->val);
  192. }
  193. /** Replace the value of an atomic counter; return the old one. */
  194. static inline size_t
  195. atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
  196. {
  197. return atomic_exchange(&counter->val, newval);
  198. }
  199. #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
  200. #endif /* defined(HAVE_WORKING_STDATOMIC) */
  201. #endif /* !defined(TOR_COMPAT_THREADS_H) */