compat_threads.h 7.0 KB

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