compat_threads.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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 "torint.h"
  9. #include "testsupport.h"
  10. #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
  11. #include <pthread.h>
  12. #endif
  13. #if defined(_WIN32)
  14. #define USE_WIN32_THREADS
  15. #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  16. #define USE_PTHREADS
  17. #else
  18. #error "No threading system was found"
  19. #endif /* defined(_WIN32) || ... */
  20. int spawn_func(void (*func)(void *), void *data);
  21. void spawn_exit(void) ATTR_NORETURN;
  22. /* Because we use threads instead of processes on most platforms (Windows,
  23. * Linux, etc), we need locking for them. On platforms with poor thread
  24. * support or broken gethostbyname_r, these functions are no-ops. */
  25. /** A generic lock structure for multithreaded builds. */
  26. typedef struct tor_mutex_t {
  27. #if defined(USE_WIN32_THREADS)
  28. /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
  29. CRITICAL_SECTION mutex;
  30. #elif defined(USE_PTHREADS)
  31. /** Pthreads-only: with pthreads, we implement locks with
  32. * pthread_mutex_t. */
  33. pthread_mutex_t mutex;
  34. #else
  35. /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
  36. int _unused;
  37. #endif /* defined(USE_WIN32_THREADS) || ... */
  38. } tor_mutex_t;
  39. tor_mutex_t *tor_mutex_new(void);
  40. tor_mutex_t *tor_mutex_new_nonrecursive(void);
  41. void tor_mutex_init(tor_mutex_t *m);
  42. void tor_mutex_init_nonrecursive(tor_mutex_t *m);
  43. void tor_mutex_acquire(tor_mutex_t *m);
  44. void tor_mutex_release(tor_mutex_t *m);
  45. void tor_mutex_free_(tor_mutex_t *m);
  46. #define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
  47. void tor_mutex_uninit(tor_mutex_t *m);
  48. unsigned long tor_get_thread_id(void);
  49. void tor_threads_init(void);
  50. /** Conditions need nonrecursive mutexes with pthreads. */
  51. #define tor_mutex_init_for_cond(m) tor_mutex_init_nonrecursive(m)
  52. void set_main_thread(void);
  53. int in_main_thread(void);
  54. typedef struct tor_cond_t {
  55. #ifdef USE_PTHREADS
  56. pthread_cond_t cond;
  57. #elif defined(USE_WIN32_THREADS)
  58. HANDLE event;
  59. CRITICAL_SECTION lock;
  60. int n_waiting;
  61. int n_to_wake;
  62. int generation;
  63. #else
  64. #error no known condition implementation.
  65. #endif /* defined(USE_PTHREADS) || ... */
  66. } tor_cond_t;
  67. tor_cond_t *tor_cond_new(void);
  68. void tor_cond_free_(tor_cond_t *cond);
  69. #define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
  70. int tor_cond_init(tor_cond_t *cond);
  71. void tor_cond_uninit(tor_cond_t *cond);
  72. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
  73. const struct timeval *tv);
  74. void tor_cond_signal_one(tor_cond_t *cond);
  75. void tor_cond_signal_all(tor_cond_t *cond);
  76. /** Helper type used to manage waking up the main thread while it's in
  77. * the libevent main loop. Used by the work queue code. */
  78. typedef struct alert_sockets_s {
  79. /* XXXX This structure needs a better name. */
  80. /** Socket that the main thread should listen for EV_READ events on.
  81. * Note that this socket may be a regular fd on a non-Windows platform.
  82. */
  83. tor_socket_t read_fd;
  84. /** Socket to use when alerting the main thread. */
  85. tor_socket_t write_fd;
  86. /** Function to alert the main thread */
  87. int (*alert_fn)(tor_socket_t write_fd);
  88. /** Function to make the main thread no longer alerted. */
  89. int (*drain_fn)(tor_socket_t read_fd);
  90. } alert_sockets_t;
  91. /* Flags to disable one or more alert_sockets backends. */
  92. #define ASOCKS_NOEVENTFD2 (1u<<0)
  93. #define ASOCKS_NOEVENTFD (1u<<1)
  94. #define ASOCKS_NOPIPE2 (1u<<2)
  95. #define ASOCKS_NOPIPE (1u<<3)
  96. #define ASOCKS_NOSOCKETPAIR (1u<<4)
  97. int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
  98. void alert_sockets_close(alert_sockets_t *socks);
  99. typedef struct tor_threadlocal_s {
  100. #ifdef _WIN32
  101. DWORD index;
  102. #else
  103. pthread_key_t key;
  104. #endif
  105. } tor_threadlocal_t;
  106. /** Initialize a thread-local variable.
  107. *
  108. * After you call this function on a tor_threadlocal_t, you can call
  109. * tor_threadlocal_set to change the current value of this variable for the
  110. * current thread, and tor_threadlocal_get to retrieve the current value for
  111. * the current thread. Each thread has its own value.
  112. **/
  113. int tor_threadlocal_init(tor_threadlocal_t *threadlocal);
  114. /**
  115. * Release all resource associated with a thread-local variable.
  116. */
  117. void tor_threadlocal_destroy(tor_threadlocal_t *threadlocal);
  118. /**
  119. * Return the current value of a thread-local variable for this thread.
  120. *
  121. * It's undefined behavior to use this function if the threadlocal hasn't
  122. * been initialized, or has been destroyed.
  123. */
  124. void *tor_threadlocal_get(tor_threadlocal_t *threadlocal);
  125. /**
  126. * Change the current value of a thread-local variable for this thread to
  127. * <b>value</b>.
  128. *
  129. * It's undefined behavior to use this function if the threadlocal hasn't
  130. * been initialized, or has been destroyed.
  131. */
  132. void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value);
  133. /**
  134. * Atomic counter type; holds a size_t value.
  135. */
  136. typedef struct atomic_counter_t {
  137. tor_mutex_t mutex;
  138. size_t val;
  139. } atomic_counter_t;
  140. void atomic_counter_init(atomic_counter_t *counter);
  141. void atomic_counter_destroy(atomic_counter_t *counter);
  142. void atomic_counter_add(atomic_counter_t *counter, size_t add);
  143. void atomic_counter_sub(atomic_counter_t *counter, size_t sub);
  144. size_t atomic_counter_get(atomic_counter_t *counter);
  145. #endif /* !defined(TOR_COMPAT_THREADS_H) */