threads.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. int spawn_func(void (*func)(void *), void *data);
  23. void spawn_exit(void) ATTR_NORETURN;
  24. unsigned long tor_get_thread_id(void);
  25. void tor_threads_init(void);
  26. /** Conditions need nonrecursive mutexes with pthreads. */
  27. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  28. void set_main_thread(void);
  29. int in_main_thread(void);
  30. typedef struct tor_cond_t {
  31. #ifdef USE_PTHREADS
  32. pthread_cond_t cond;
  33. #elif defined(USE_WIN32_THREADS)
  34. HANDLE event;
  35. CRITICAL_SECTION lock;
  36. int n_waiting;
  37. int n_to_wake;
  38. int generation;
  39. #else
  40. #error no known condition implementation.
  41. #endif /* defined(USE_PTHREADS) || ... */
  42. } tor_cond_t;
  43. tor_cond_t *tor_cond_new(void);
  44. void tor_cond_free_(tor_cond_t *cond);
  45. #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
  46. int tor_cond_init(tor_cond_t *cond);
  47. void tor_cond_uninit(tor_cond_t *cond);
  48. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  49. const struct timeval *tv);
  50. void tor_cond_signal_one(tor_cond_t *cond);
  51. void tor_cond_signal_all(tor_cond_t *cond);
  52. typedef struct tor_threadlocal_s {
  53. #ifdef _WIN32
  54. DWORD index;
  55. #else
  56. pthread_key_t key;
  57. #endif
  58. } tor_threadlocal_t;
  59. /** Initialize a thread-local variable.
  60. *
  61. * After you call this function on a tor_threadlocal_t, you can call
  62. * tor_threadlocal_set to change the current value of this variable for the
  63. * current thread, and tor_threadlocal_get to retrieve the current value for
  64. * the current thread. Each thread has its own value.
  65. **/
  66. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  67. /**
  68. * Release all resource associated with a thread-local variable.
  69. */
  70. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  71. /**
  72. * Return the current value of a thread-local variable for this thread.
  73. *
  74. * It's undefined behavior to use this function if the threadlocal hasn't
  75. * been initialized, or has been destroyed.
  76. */
  77. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  78. /**
  79. * Change the current value of a thread-local variable for this thread to
  80. * <b>value</b>.
  81. *
  82. * It's undefined behavior to use this function if the threadlocal hasn't
  83. * been initialized, or has been destroyed.
  84. */
  85. void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
  86. /**
  87. * Atomic counter type; holds a size_t value.
  88. */
  89. #ifdef HAVE_WORKING_STDATOMIC
  90. typedef struct atomic_counter_t {
  91. atomic_size_t val;
  92. } atomic_counter_t;
  93. #define ATOMIC_LINKAGE static
  94. #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
  95. typedef struct atomic_counter_t {
  96. tor_mutex_t mutex;
  97. size_t val;
  98. } atomic_counter_t;
  99. #define ATOMIC_LINKAGE
  100. #endif /* defined(HAVE_WORKING_STDATOMIC) */
  101. ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
  102. ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
  103. ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
  104. ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  105. ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
  106. ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
  107. size_t newval);
  108. #undef ATOMIC_LINKAGE
  109. #ifdef HAVE_WORKING_STDATOMIC
  110. /** Initialize a new atomic counter with the value 0 */
  111. static inline void
  112. atomic_counter_init(atomic_counter_t *counter)
  113. {
  114. atomic_init(&counter->val, 0);
  115. }
  116. /** Clean up all resources held by an atomic counter. */
  117. static inline void
  118. atomic_counter_destroy(atomic_counter_t *counter)
  119. {
  120. (void)counter;
  121. }
  122. /** Add a value to an atomic counter. */
  123. static inline void
  124. atomic_counter_add(atomic_counter_t *counter, size_t add)
  125. {
  126. (void) atomic_fetch_add(&counter->val, add);
  127. }
  128. /** Subtract a value from an atomic counter. */
  129. static inline void
  130. atomic_counter_sub(atomic_counter_t *counter, size_t sub)
  131. {
  132. (void) atomic_fetch_sub(&counter->val, sub);
  133. }
  134. /** Return the current value of an atomic counter */
  135. static inline size_t
  136. atomic_counter_get(atomic_counter_t *counter)
  137. {
  138. return atomic_load(&counter->val);
  139. }
  140. /** Replace the value of an atomic counter; return the old one. */
  141. static inline size_t
  142. atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
  143. {
  144. return atomic_exchange(&counter->val, newval);
  145. }
  146. #else /* !(defined(HAVE_WORKING_STDATOMIC)) */
  147. #endif /* defined(HAVE_WORKING_STDATOMIC) */
  148. #endif /* !defined(TOR_COMPAT_THREADS_H) */