compat_threads.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, 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 "lib/cc/torint.h"
  9. #include "lib/testsupport/testsupport.h"
  10. #include "lib/lock/compat_mutex.h"
  11. #ifdef HAVE_STDATOMIC_H
  12. #include <stdatomic.h>
  13. #endif
  14. int spawn_func(void (*func)(void *), void *data);
  15. void spawn_exit(void) ATTR_NORETURN;
  16. unsigned long tor_get_thread_id(void);
  17. void tor_threads_init(void);
  18. /** Conditions need nonrecursive mutexes with pthreads. */
  19. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  20. void set_main_thread(void);
  21. int in_main_thread(void);
  22. typedef struct tor_cond_t {
  23. #ifdef USE_PTHREADS
  24. pthread_cond_t cond;
  25. #elif defined(USE_WIN32_THREADS)
  26. HANDLE event;
  27. CRITICAL_SECTION lock;
  28. int n_waiting;
  29. int n_to_wake;
  30. int generation;
  31. #else
  32. #error no known condition implementation.
  33. #endif /* defined(USE_PTHREADS) || ... */
  34. } tor_cond_t;
  35. tor_cond_t *tor_cond_new(void);
  36. void tor_cond_free_(tor_cond_t *cond);
  37. #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
  38. int tor_cond_init(tor_cond_t *cond);
  39. void tor_cond_uninit(tor_cond_t *cond);
  40. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  41. const struct timeval *tv);
  42. void tor_cond_signal_one(tor_cond_t *cond);
  43. void tor_cond_signal_all(tor_cond_t *cond);
  44. /** Helper type used to manage waking up the main thread while it's in
  45. * the libevent main loop. Used by the work queue code. */
  46. typedef struct alert_sockets_s {
  47. /* XXXX This structure needs a better name. */
  48. /** Socket that the main thread should listen for EV_READ events on.
  49. * Note that this socket may be a regular fd on a non-Windows platform.
  50. */
  51. tor_socket_t read_fd;
  52. /** Socket to use when alerting the main thread. */
  53. tor_socket_t write_fd;
  54. /** Function to alert the main thread */
  55. int (*alert_fn)(tor_socket_t write_fd);
  56. /** Function to make the main thread no longer alerted. */
  57. int (*drain_fn)(tor_socket_t read_fd);
  58. } alert_sockets_t;
  59. /* Flags to disable one or more alert_sockets backends. */
  60. #define ASOCKS_NOEVENTFD2 (1u<<0)
  61. #define ASOCKS_NOEVENTFD (1u<<1)
  62. #define ASOCKS_NOPIPE2 (1u<<2)
  63. #define ASOCKS_NOPIPE (1u<<3)
  64. #define ASOCKS_NOSOCKETPAIR (1u<<4)
  65. int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
  66. void alert_sockets_close(alert_sockets_t *socks);
  67. typedef struct tor_threadlocal_s {
  68. #ifdef _WIN32
  69. DWORD index;
  70. #else
  71. pthread_key_t key;
  72. #endif
  73. } tor_threadlocal_t;
  74. /** Initialize a thread-local variable.
  75. *
  76. * After you call this function on a tor_threadlocal_t, you can call
  77. * tor_threadlocal_set to change the current value of this variable for the
  78. * current thread, and tor_threadlocal_get to retrieve the current value for
  79. * the current thread. Each thread has its own value.
  80. **/
  81. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  82. /**
  83. * Release all resource associated with a thread-local variable.
  84. */
  85. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  86. /**
  87. * Return the current value of a thread-local variable for this thread.
  88. *
  89. * It's undefined behavior to use this function if the threadlocal hasn't
  90. * been initialized, or has been destroyed.
  91. */
  92. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  93. /**
  94. * Change the current value of a thread-local variable for this thread to
  95. * <b>value</b>.
  96. *
  97. * It's undefined behavior to use this function if the threadlocal hasn't
  98. * been initialized, or has been destroyed.
  99. */
  100. void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
  101. /**
  102. * Atomic counter type; holds a size_t value.
  103. */
  104. #ifdef HAVE_STDATOMIC_H
  105. typedef struct atomic_counter_t {
  106. atomic_size_t val;
  107. } atomic_counter_t;
  108. #define ATOMIC_LINKAGE static
  109. #else /* !(defined(HAVE_STDATOMIC_H)) */
  110. typedef struct atomic_counter_t {
  111. tor_mutex_t mutex;
  112. size_t val;
  113. } atomic_counter_t;
  114. #define ATOMIC_LINKAGE
  115. #endif /* defined(HAVE_STDATOMIC_H) */
  116. ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
  117. ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
  118. ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
  119. ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  120. ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
  121. ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
  122. size_t newval);
  123. #undef ATOMIC_LINKAGE
  124. #ifdef HAVE_STDATOMIC_H
  125. /** Initialize a new atomic counter with the value 0 */
  126. static inline void
  127. atomic_counter_init(atomic_counter_t *counter)
  128. {
  129. atomic_init(&counter->val, 0);
  130. }
  131. /** Clean up all resources held by an atomic counter. */
  132. static inline void
  133. atomic_counter_destroy(atomic_counter_t *counter)
  134. {
  135. (void)counter;
  136. }
  137. /** Add a value to an atomic counter. */
  138. static inline void
  139. atomic_counter_add(atomic_counter_t *counter, size_t add)
  140. {
  141. (void) atomic_fetch_add(&counter->val, add);
  142. }
  143. /** Subtract a value from an atomic counter. */
  144. static inline void
  145. atomic_counter_sub(atomic_counter_t *counter, size_t sub)
  146. {
  147. (void) atomic_fetch_sub(&counter->val, sub);
  148. }
  149. /** Return the current value of an atomic counter */
  150. static inline size_t
  151. atomic_counter_get(atomic_counter_t *counter)
  152. {
  153. return atomic_load(&counter->val);
  154. }
  155. /** Replace the value of an atomic counter; return the old one. */
  156. static inline size_t
  157. atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
  158. {
  159. return atomic_exchange(&counter->val, newval);
  160. }
  161. #else /* !(defined(HAVE_STDATOMIC_H)) */
  162. #endif /* defined(HAVE_STDATOMIC_H) */
  163. #endif /* !defined(TOR_COMPAT_THREADS_H) */