threads.h 4.7 KB

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