compat_pthreads.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * "reentrant" mutexes (i.e., once we can re-lock if we're already holding
  75. * them.) */
  76. static pthread_mutexattr_t attr_reentrant;
  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_reentrant);
  86. if (PREDICT_UNLIKELY(err)) {
  87. log_err(LD_GENERAL, "Error %d creating a mutex.", err);
  88. tor_fragile_assert();
  89. }
  90. }
  91. /** Wait until <b>m</b> is free, then acquire it. */
  92. void
  93. tor_mutex_acquire(tor_mutex_t *m)
  94. {
  95. int err;
  96. tor_assert(m);
  97. err = pthread_mutex_lock(&m->mutex);
  98. if (PREDICT_UNLIKELY(err)) {
  99. log_err(LD_GENERAL, "Error %d locking a mutex.", err);
  100. tor_fragile_assert();
  101. }
  102. }
  103. /** Release the lock <b>m</b> so another thread can have it. */
  104. void
  105. tor_mutex_release(tor_mutex_t *m)
  106. {
  107. int err;
  108. tor_assert(m);
  109. err = pthread_mutex_unlock(&m->mutex);
  110. if (PREDICT_UNLIKELY(err)) {
  111. log_err(LD_GENERAL, "Error %d unlocking a mutex.", err);
  112. tor_fragile_assert();
  113. }
  114. }
  115. /** Clean up the mutex <b>m</b> so that it no longer uses any system
  116. * resources. Does not free <b>m</b>. This function must only be called on
  117. * mutexes from tor_mutex_init(). */
  118. void
  119. tor_mutex_uninit(tor_mutex_t *m)
  120. {
  121. int err;
  122. tor_assert(m);
  123. err = pthread_mutex_destroy(&m->mutex);
  124. if (PREDICT_UNLIKELY(err)) {
  125. log_err(LD_GENERAL, "Error %d destroying a mutex.", err);
  126. tor_fragile_assert();
  127. }
  128. }
  129. /** Return an integer representing this thread. */
  130. unsigned long
  131. tor_get_thread_id(void)
  132. {
  133. union {
  134. pthread_t thr;
  135. unsigned long id;
  136. } r;
  137. r.thr = pthread_self();
  138. return r.id;
  139. }
  140. /* Conditions. */
  141. /** Cross-platform condition implementation. */
  142. struct tor_cond_t {
  143. pthread_cond_t cond;
  144. };
  145. /** Return a newly allocated condition, with nobody waiting on it. */
  146. tor_cond_t *
  147. tor_cond_new(void)
  148. {
  149. tor_cond_t *cond = tor_malloc_zero(sizeof(tor_cond_t));
  150. if (pthread_cond_init(&cond->cond, NULL)) {
  151. tor_free(cond);
  152. return NULL;
  153. }
  154. return cond;
  155. }
  156. /** Release all resources held by <b>cond</b>. */
  157. void
  158. tor_cond_free(tor_cond_t *cond)
  159. {
  160. if (!cond)
  161. return;
  162. if (pthread_cond_destroy(&cond->cond)) {
  163. log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
  164. return;
  165. }
  166. tor_free(cond);
  167. }
  168. /** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
  169. * All waiters on the condition must wait holding the same <b>mutex</b>.
  170. * Returns 0 on success, negative on failure. */
  171. int
  172. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex)
  173. {
  174. return pthread_cond_wait(&cond->cond, &mutex->mutex) ? -1 : 0;
  175. }
  176. /** Wake up one of the waiters on <b>cond</b>. */
  177. void
  178. tor_cond_signal_one(tor_cond_t *cond)
  179. {
  180. pthread_cond_signal(&cond->cond);
  181. }
  182. /** Wake up all of the waiters on <b>cond</b>. */
  183. void
  184. tor_cond_signal_all(tor_cond_t *cond)
  185. {
  186. pthread_cond_broadcast(&cond->cond);
  187. }
  188. /** Set up common structures for use by threading. */
  189. void
  190. tor_threads_init(void)
  191. {
  192. if (!threads_initialized) {
  193. pthread_mutexattr_init(&attr_reentrant);
  194. pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE);
  195. tor_assert(0==pthread_attr_init(&attr_detached));
  196. tor_assert(0==pthread_attr_setdetachstate(&attr_detached, 1));
  197. threads_initialized = 1;
  198. set_main_thread();
  199. }
  200. }