threads.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file threads.h
  7. * \brief Header for threads.c
  8. **/
  9. #ifndef TOR_COMPAT_THREADS_H
  10. #define TOR_COMPAT_THREADS_H
  11. #include "orconfig.h"
  12. #include "lib/cc/torint.h"
  13. #include "lib/testsupport/testsupport.h"
  14. #include "lib/lock/compat_mutex.h"
  15. #if defined(HAVE_STDATOMIC_H) && defined(STDATOMIC_WORKS)
  16. #define HAVE_WORKING_STDATOMIC
  17. #endif
  18. #ifdef HAVE_WORKING_STDATOMIC
  19. #include <stdatomic.h>
  20. #endif
  21. struct timeval;
  22. typedef struct tor_thread_s {
  23. #ifdef _WIN32
  24. uintptr_t winthread;
  25. #else
  26. pthread_t pthread;
  27. #endif
  28. } tor_thread_t;
  29. tor_thread_t* spawn_func(void (*func)(void *), void *data);
  30. void spawn_exit(void) ATTR_NORETURN;
  31. int join_thread(tor_thread_t* thread);
  32. void free_thread(tor_thread_t* thread);
  33. unsigned long tor_get_thread_id(void);
  34. void tor_threads_init(void);
  35. /** Conditions need nonrecursive mutexes with pthreads. */
  36. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  37. void set_main_thread(void);
  38. int in_main_thread(void);
  39. typedef struct tor_cond_t {
  40. #ifdef USE_PTHREADS
  41. pthread_cond_t cond;
  42. #elif defined(USE_WIN32_THREADS)
  43. HANDLE event;
  44. CRITICAL_SECTION lock;
  45. int n_waiting;
  46. int n_to_wake;
  47. int generation;
  48. #else
  49. #error no known condition implementation.
  50. #endif /* defined(USE_PTHREADS) || ... */
  51. } tor_cond_t;
  52. tor_cond_t *tor_cond_new(void);
  53. void tor_cond_free_(tor_cond_t *cond);
  54. #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
  55. int tor_cond_init(tor_cond_t *cond);
  56. void tor_cond_uninit(tor_cond_t *cond);
  57. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  58. const struct timeval *tv);
  59. void tor_cond_signal_one(tor_cond_t *cond);
  60. void tor_cond_signal_all(tor_cond_t *cond);
  61. typedef struct tor_threadlocal_s {
  62. #ifdef _WIN32
  63. DWORD index;
  64. #else
  65. pthread_key_t key;
  66. #endif
  67. } tor_threadlocal_t;
  68. /** Initialize a thread-local variable.
  69. *
  70. * After you call this function on a tor_threadlocal_t, you can call
  71. * tor_threadlocal_set to change the current value of this variable for the
  72. * current thread, and tor_threadlocal_get to retrieve the current value for
  73. * the current thread. Each thread has its own value.
  74. **/
  75. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  76. /**
  77. * Release all resource associated with a thread-local variable.
  78. */
  79. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  80. /**
  81. * Return the current value of a thread-local variable for this thread.
  82. *
  83. * It's undefined behavior to use this function if the threadlocal hasn't
  84. * been initialized, or has been destroyed.
  85. */
  86. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  87. /**
  88. * Change the current value of a thread-local variable for this thread to
  89. * <b>value</b>.
  90. *
  91. * It's undefined behavior to use this function if the threadlocal hasn't
  92. * been initialized, or has been destroyed.
  93. */
  94. void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
  95. /**
  96. * Atomic counter type; holds a size_t value.
  97. */
  98. #ifdef HAVE_WORKING_STDATOMIC
  99. typedef struct atomic_counter_t {
  100. atomic_size_t val;
  101. } atomic_counter_t;
  102. #define ATOMIC_LINKAGE static
  103. #else /* !defined(HAVE_WORKING_STDATOMIC) */
  104. typedef struct atomic_counter_t {
  105. tor_mutex_t mutex;
  106. size_t val;
  107. } atomic_counter_t;
  108. #define ATOMIC_LINKAGE
  109. #endif /* defined(HAVE_WORKING_STDATOMIC) */
  110. ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
  111. ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
  112. ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
  113. ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  114. ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
  115. ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
  116. size_t newval);
  117. #undef ATOMIC_LINKAGE
  118. #ifdef HAVE_WORKING_STDATOMIC
  119. /** Initialize a new atomic counter with the value 0 */
  120. static inline void
  121. atomic_counter_init(atomic_counter_t *counter)
  122. {
  123. atomic_init(&counter->val, 0);
  124. }
  125. /** Clean up all resources held by an atomic counter.
  126. *
  127. * This usage note applies to the compat_threads implementation of
  128. * atomic_counter_destroy():
  129. * Destroying a locked mutex is undefined behaviour. Global mutexes may be
  130. * locked when they are passed to this function, because multiple threads can
  131. * still access them. So we can either:
  132. * - destroy on shutdown, and re-initialise when tor re-initialises, or
  133. * - skip destroying and re-initialisation, using a sentinel variable.
  134. * See #31735 for details.
  135. */
  136. static inline void
  137. atomic_counter_destroy(atomic_counter_t *counter)
  138. {
  139. (void)counter;
  140. }
  141. /** Add a value to an atomic counter. */
  142. static inline void
  143. atomic_counter_add(atomic_counter_t *counter, size_t add)
  144. {
  145. (void) atomic_fetch_add(&counter->val, add);
  146. }
  147. /** Subtract a value from an atomic counter. */
  148. static inline void
  149. atomic_counter_sub(atomic_counter_t *counter, size_t sub)
  150. {
  151. (void) atomic_fetch_sub(&counter->val, sub);
  152. }
  153. /** Return the current value of an atomic counter */
  154. static inline size_t
  155. atomic_counter_get(atomic_counter_t *counter)
  156. {
  157. return atomic_load(&counter->val);
  158. }
  159. /** Replace the value of an atomic counter; return the old one. */
  160. static inline size_t
  161. atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
  162. {
  163. return atomic_exchange(&counter->val, newval);
  164. }
  165. #else /* !defined(HAVE_WORKING_STDATOMIC) */
  166. #endif /* defined(HAVE_WORKING_STDATOMIC) */
  167. #endif /* !defined(TOR_COMPAT_THREADS_H) */