threads.h 4.7 KB

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