compat_pthreads.c 6.5 KB

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