compat_pthreads.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include <pthread.h>
  7. #include "compat.h"
  8. #include "torlog.h"
  9. #include "util.h"
  10. /** Wraps a void (*)(void*) function and its argument so we can
  11. * invoke them in a way pthreads would expect.
  12. */
  13. typedef struct tor_pthread_data_t {
  14. void (*func)(void *);
  15. void *data;
  16. } tor_pthread_data_t;
  17. /** Given a tor_pthread_data_t <b>_data</b>, call _data-&gt;func(d-&gt;data)
  18. * and free _data. Used to make sure we can call functions the way pthread
  19. * expects. */
  20. static void *
  21. tor_pthread_helper_fn(void *_data)
  22. {
  23. tor_pthread_data_t *data = _data;
  24. void (*func)(void*);
  25. void *arg;
  26. /* mask signals to worker threads to avoid SIGPIPE, etc */
  27. sigset_t sigs;
  28. /* We're in a subthread; don't handle any signals here. */
  29. sigfillset(&sigs);
  30. pthread_sigmask(SIG_SETMASK, &sigs, NULL);
  31. func = data->func;
  32. arg = data->data;
  33. tor_free(_data);
  34. func(arg);
  35. return NULL;
  36. }
  37. /**
  38. * A pthread attribute to make threads start detached.
  39. */
  40. static pthread_attr_t attr_detached;
  41. /** True iff we've called tor_threads_init() */
  42. static int threads_initialized = 0;
  43. /** Minimalist interface to run a void function in the background. On
  44. * Unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  45. * func should not return, but rather should call spawn_exit.
  46. *
  47. * NOTE: if <b>data</b> is used, it should not be allocated on the stack,
  48. * since in a multithreaded environment, there is no way to be sure that
  49. * the caller's stack will still be around when the called function is
  50. * running.
  51. */
  52. int
  53. spawn_func(void (*func)(void *), void *data)
  54. {
  55. pthread_t thread;
  56. tor_pthread_data_t *d;
  57. if (PREDICT_UNLIKELY(!threads_initialized))
  58. tor_threads_init();
  59. d = tor_malloc(sizeof(tor_pthread_data_t));
  60. d->data = data;
  61. d->func = func;
  62. if (pthread_create(&thread,&attr_detached,tor_pthread_helper_fn,d))
  63. return -1;
  64. return 0;
  65. }
  66. /** End the current thread/process.
  67. */
  68. void
  69. spawn_exit(void)
  70. {
  71. pthread_exit(NULL);
  72. }
  73. /** A mutex attribute that we're going to use to tell pthreads that we want
  74. * "recursive" mutexes (i.e., once we can re-lock if we're already holding
  75. * them.) */
  76. static pthread_mutexattr_t attr_recursive;
  77. /** Initialize <b>mutex</b> so it can be locked. Every mutex must be set
  78. * up with tor_mutex_init() or tor_mutex_new(); not both. */
  79. void
  80. tor_mutex_init(tor_mutex_t *mutex)
  81. {
  82. int err;
  83. if (PREDICT_UNLIKELY(!threads_initialized))
  84. tor_threads_init();
  85. err = pthread_mutex_init(&mutex->mutex, &attr_recursive);
  86. if (PREDICT_UNLIKELY(err)) {
  87. log_err(LD_GENERAL, "Error %d creating a mutex.", err);
  88. tor_fragile_assert();
  89. }
  90. }
  91. /** As tor_mutex_init, but initialize a mutex suitable that may be
  92. * non-recursive, if the OS supports that. */
  93. void
  94. tor_mutex_init_nonrecursive(tor_mutex_t *mutex)
  95. {
  96. int err;
  97. if (PREDICT_UNLIKELY(!threads_initialized))
  98. tor_threads_init();
  99. err = pthread_mutex_init(&mutex->mutex, NULL);
  100. if (PREDICT_UNLIKELY(err)) {
  101. log_err(LD_GENERAL, "Error %d creating a mutex.", err);
  102. tor_fragile_assert();
  103. }
  104. }
  105. /** Wait until <b>m</b> is free, then acquire it. */
  106. void
  107. tor_mutex_acquire(tor_mutex_t *m)
  108. {
  109. int err;
  110. tor_assert(m);
  111. err = pthread_mutex_lock(&m->mutex);
  112. if (PREDICT_UNLIKELY(err)) {
  113. log_err(LD_GENERAL, "Error %d locking a mutex.", err);
  114. tor_fragile_assert();
  115. }
  116. }
  117. /** Release the lock <b>m</b> so another thread can have it. */
  118. void
  119. tor_mutex_release(tor_mutex_t *m)
  120. {
  121. int err;
  122. tor_assert(m);
  123. err = pthread_mutex_unlock(&m->mutex);
  124. if (PREDICT_UNLIKELY(err)) {
  125. log_err(LD_GENERAL, "Error %d unlocking a mutex.", err);
  126. tor_fragile_assert();
  127. }
  128. }
  129. /** Clean up the mutex <b>m</b> so that it no longer uses any system
  130. * resources. Does not free <b>m</b>. This function must only be called on
  131. * mutexes from tor_mutex_init(). */
  132. void
  133. tor_mutex_uninit(tor_mutex_t *m)
  134. {
  135. int err;
  136. tor_assert(m);
  137. err = pthread_mutex_destroy(&m->mutex);
  138. if (PREDICT_UNLIKELY(err)) {
  139. log_err(LD_GENERAL, "Error %d destroying a mutex.", err);
  140. tor_fragile_assert();
  141. }
  142. }
  143. /** Return an integer representing this thread. */
  144. unsigned long
  145. tor_get_thread_id(void)
  146. {
  147. union {
  148. pthread_t thr;
  149. unsigned long id;
  150. } r;
  151. r.thr = pthread_self();
  152. return r.id;
  153. }
  154. /* Conditions. */
  155. /** Initialize an already-allocated condition variable. */
  156. int
  157. tor_cond_init(tor_cond_t *cond)
  158. {
  159. memset(cond, 0, sizeof(tor_cond_t));
  160. if (pthread_cond_init(&cond->cond, NULL)) {
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. /** Release all resources held by <b>cond</b>, but do not free <b>cond</b>
  166. * itself. */
  167. void
  168. tor_cond_uninit(tor_cond_t *cond)
  169. {
  170. if (pthread_cond_destroy(&cond->cond)) {
  171. log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
  172. return;
  173. }
  174. }
  175. /** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
  176. * (If <b>tv</b> is set, and that amount of time passes with no signal to
  177. * <b>cond</b>, return anyway. All waiters on the condition must wait holding
  178. * the same <b>mutex</b>. All signallers should hold that mutex. The mutex
  179. * needs to have been allocated with tor_mutex_init_for_cond().
  180. *
  181. * Returns 0 on success, -1 on failure, 1 on timeout. */
  182. int
  183. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
  184. {
  185. if (tv == NULL) {
  186. return pthread_cond_wait(&cond->cond, &mutex->mutex) ? -1 : 0;
  187. } else {
  188. struct timespec ts;
  189. struct timeval tvnow, tvsum;
  190. int r;
  191. gettimeofday(&tvnow, NULL);
  192. timeradd(tv, &tvnow, &tvsum);
  193. ts.tv_sec = tvsum.tv_sec;
  194. ts.tv_nsec = tvsum.tv_usec * 1000;
  195. r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts);
  196. if (r == 0)
  197. return 0;
  198. else if (r == ETIMEDOUT)
  199. return 1;
  200. else
  201. return -1;
  202. }
  203. }
  204. /** Wake up one of the waiters on <b>cond</b>. */
  205. void
  206. tor_cond_signal_one(tor_cond_t *cond)
  207. {
  208. pthread_cond_signal(&cond->cond);
  209. }
  210. /** Wake up all of the waiters on <b>cond</b>. */
  211. void
  212. tor_cond_signal_all(tor_cond_t *cond)
  213. {
  214. pthread_cond_broadcast(&cond->cond);
  215. }
  216. /** Set up common structures for use by threading. */
  217. void
  218. tor_threads_init(void)
  219. {
  220. if (!threads_initialized) {
  221. pthread_mutexattr_init(&attr_recursive);
  222. pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE);
  223. tor_assert(0==pthread_attr_init(&attr_detached));
  224. tor_assert(0==pthread_attr_setdetachstate(&attr_detached, 1));
  225. threads_initialized = 1;
  226. set_main_thread();
  227. }
  228. }