compat_threads.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. typedef struct tor_threadlocal_s {
  45. #ifdef _WIN32
  46. DWORD index;
  47. #else
  48. pthread_key_t key;
  49. #endif
  50. } tor_threadlocal_t;
  51. /** Initialize a thread-local variable.
  52. *
  53. * After you call this function on a tor_threadlocal_t, you can call
  54. * tor_threadlocal_set to change the current value of this variable for the
  55. * current thread, and tor_threadlocal_get to retrieve the current value for
  56. * the current thread. Each thread has its own value.
  57. **/
  58. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  59. /**
  60. * Release all resource associated with a thread-local variable.
  61. */
  62. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  63. /**
  64. * Return the current value of a thread-local variable for this thread.
  65. *
  66. * It's undefined behavior to use this function if the threadlocal hasn't
  67. * been initialized, or has been destroyed.
  68. */
  69. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  70. /**
  71. * Change the current value of a thread-local variable for this thread to
  72. * <b>value</b>.
  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_set(tor_threadlocal_t *threadlocal, void *value);
  78. /**
  79. * Atomic counter type; holds a size_t value.
  80. */
  81. #ifdef HAVE_STDATOMIC_H
  82. typedef struct atomic_counter_t {
  83. atomic_size_t val;
  84. } atomic_counter_t;
  85. #define ATOMIC_LINKAGE static
  86. #else /* !(defined(HAVE_STDATOMIC_H)) */
  87. typedef struct atomic_counter_t {
  88. tor_mutex_t mutex;
  89. size_t val;
  90. } atomic_counter_t;
  91. #define ATOMIC_LINKAGE
  92. #endif /* defined(HAVE_STDATOMIC_H) */
  93. ATOMIC_LINKAGE void atomic_counter_init(atomic_counter_t *counter);
  94. ATOMIC_LINKAGE void atomic_counter_destroy(atomic_counter_t *counter);
  95. ATOMIC_LINKAGE void atomic_counter_add(atomic_counter_t *counter, size_t add);
  96. ATOMIC_LINKAGE void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  97. ATOMIC_LINKAGE size_t atomic_counter_get(atomic_counter_t *counter);
  98. ATOMIC_LINKAGE size_t atomic_counter_exchange(atomic_counter_t *counter,
  99. size_t newval);
  100. #undef ATOMIC_LINKAGE
  101. #ifdef HAVE_STDATOMIC_H
  102. /** Initialize a new atomic counter with the value 0 */
  103. static inline void
  104. atomic_counter_init(atomic_counter_t *counter)
  105. {
  106. atomic_init(&counter->val, 0);
  107. }
  108. /** Clean up all resources held by an atomic counter. */
  109. static inline void
  110. atomic_counter_destroy(atomic_counter_t *counter)
  111. {
  112. (void)counter;
  113. }
  114. /** Add a value to an atomic counter. */
  115. static inline void
  116. atomic_counter_add(atomic_counter_t *counter, size_t add)
  117. {
  118. (void) atomic_fetch_add(&counter->val, add);
  119. }
  120. /** Subtract a value from an atomic counter. */
  121. static inline void
  122. atomic_counter_sub(atomic_counter_t *counter, size_t sub)
  123. {
  124. (void) atomic_fetch_sub(&counter->val, sub);
  125. }
  126. /** Return the current value of an atomic counter */
  127. static inline size_t
  128. atomic_counter_get(atomic_counter_t *counter)
  129. {
  130. return atomic_load(&counter->val);
  131. }
  132. /** Replace the value of an atomic counter; return the old one. */
  133. static inline size_t
  134. atomic_counter_exchange(atomic_counter_t *counter, size_t newval)
  135. {
  136. return atomic_exchange(&counter->val, newval);
  137. }
  138. #else /* !(defined(HAVE_STDATOMIC_H)) */
  139. #endif /* defined(HAVE_STDATOMIC_H) */
  140. #endif /* !defined(TOR_COMPAT_THREADS_H) */