compat_threads.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. void tor_mutex_uninit(tor_mutex_t *m);
  50. unsigned long tor_get_thread_id(void);
  51. void tor_threads_init(void);
  52. /** Conditions need nonrecursive mutexes with pthreads. */
  53. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  54. void set_main_thread(void);
  55. int in_main_thread(void);
  56. typedef struct tor_cond_t {
  57. #ifdef USE_PTHREADS
  58. pthread_cond_t cond;
  59. #elif defined(USE_WIN32_THREADS)
  60. HANDLE event;
  61. CRITICAL_SECTION lock;
  62. int n_waiting;
  63. int n_to_wake;
  64. int generation;
  65. #else
  66. #error no known condition implementation.
  67. #endif /* defined(USE_PTHREADS) || ... */
  68. } tor_cond_t;
  69. tor_cond_t *tor_cond_new(void);
  70. void tor_cond_free(tor_cond_t *cond);
  71. int tor_cond_init(tor_cond_t *cond);
  72. void tor_cond_uninit(tor_cond_t *cond);
  73. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  74. const struct timeval *tv);
  75. void tor_cond_signal_one(tor_cond_t *cond);
  76. void tor_cond_signal_all(tor_cond_t *cond);
  77. /** Helper type used to manage waking up the main thread while it's in
  78. * the libevent main loop. Used by the work queue code. */
  79. typedef struct alert_sockets_s {
  80. /* XXXX This structure needs a better name. */
  81. /** Socket that the main thread should listen for EV_READ events on.
  82. * Note that this socket may be a regular fd on a non-Windows platform.
  83. */
  84. tor_socket_t read_fd;
  85. /** Socket to use when alerting the main thread. */
  86. tor_socket_t write_fd;
  87. /** Function to alert the main thread */
  88. int (*alert_fn)(tor_socket_t write_fd);
  89. /** Function to make the main thread no longer alerted. */
  90. int (*drain_fn)(tor_socket_t read_fd);
  91. } alert_sockets_t;
  92. /* Flags to disable one or more alert_sockets backends. */
  93. #define ASOCKS_NOEVENTFD2 (1u<<0)
  94. #define ASOCKS_NOEVENTFD (1u<<1)
  95. #define ASOCKS_NOPIPE2 (1u<<2)
  96. #define ASOCKS_NOPIPE (1u<<3)
  97. #define ASOCKS_NOSOCKETPAIR (1u<<4)
  98. int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
  99. void alert_sockets_close(alert_sockets_t *socks);
  100. typedef struct tor_threadlocal_s {
  101. #ifdef _WIN32
  102. DWORD index;
  103. #else
  104. pthread_key_t key;
  105. #endif
  106. } tor_threadlocal_t;
  107. /** Initialize a thread-local variable.
  108. *
  109. * After you call this function on a tor_threadlocal_t, you can call
  110. * tor_threadlocal_set to change the current value of this variable for the
  111. * current thread, and tor_threadlocal_get to retrieve the current value for
  112. * the current thread. Each thread has its own value.
  113. **/
  114. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  115. /**
  116. * Release all resource associated with a thread-local variable.
  117. */
  118. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  119. /**
  120. * Return the current value of a thread-local variable for this thread.
  121. *
  122. * It's undefined behavior to use this function if the threadlocal hasn't
  123. * been initialized, or has been destroyed.
  124. */
  125. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  126. /**
  127. * Change the current value of a thread-local variable for this thread to
  128. * <b>value</b>.
  129. *
  130. * It's undefined behavior to use this function if the threadlocal hasn't
  131. * been initialized, or has been destroyed.
  132. */
  133. void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
  134. /**
  135. * Atomic counter type; holds a size_t value.
  136. */
  137. #ifdef HAVE_STDATOMIC_H
  138. typedef struct atomic_counter_t {
  139. atomic_size_t val;
  140. } atomic_counter_t;
  141. #define ATOMIC_LINKAGE static
  142. #else /* !(defined(HAVE_STDATOMIC_H)) */
  143. typedef struct atomic_counter_t {
  144. tor_mutex_t mutex;
  145. size_t val;
  146. } atomic_counter_t;
  147. #define ATOMIC_LINKAGE
  148. #endif /* defined(HAVE_STDATOMIC_H) */
  149. ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
  150. ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
  151. ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
  152. ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  153. ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
  154. ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
  155. size_t newval);
  156. #undef ATOMIC_LINKAGE
  157. #ifdef HAVE_STDATOMIC_H
  158. /** Initialize a new atomic counter with the value 0 */
  159. static inline void
  160. atomic_counter_init(atomic_counter_t *counter)
  161. {
  162. atomic_init(&counter->val, 0);
  163. }
  164. /** Clean up all resources held by an atomic counter. */
  165. static inline void
  166. atomic_counter_destroy(atomic_counter_t *counter)
  167. {
  168. (void)counter;
  169. }
  170. /** Add a value to an atomic counter. */
  171. static inline void
  172. atomic_counter_add(atomic_counter_t *counter, size_t add)
  173. {
  174. (void) atomic_fetch_add(&counter->val, add);
  175. }
  176. /** Subtract a value from an atomic counter. */
  177. static inline void
  178. atomic_counter_sub(atomic_counter_t *counter, size_t sub)
  179. {
  180. (void) atomic_fetch_sub(&counter->val, sub);
  181. }
  182. /** Return the current value of an atomic counter */
  183. static inline size_t
  184. atomic_counter_get(atomic_counter_t *counter)
  185. {
  186. return atomic_load(&counter->val);
  187. }
  188. /** Replace the value of an atomic counter; return the old one. */
  189. static inline size_t
  190. atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
  191. {
  192. return atomic_exchange(&counter->val, newval);
  193. }
  194. #else /* !(defined(HAVE_STDATOMIC_H)) */
  195. #endif /* defined(HAVE_STDATOMIC_H) */
  196. #endif /* !defined(TOR_COMPAT_THREADS_H) */