compat_pthreads.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. int
  142. tor_cond_init(tor_cond_t *cond)
  143. {
  144. memset(cond, 0, sizeof(tor_cond_t));
  145. if (pthread_cond_init(&cond->cond, NULL)) {
  146. return -1;
  147. }
  148. return 0;
  149. }
  150. /** Release all resources held by <b>cond</b>. */
  151. void
  152. tor_cond_uninit(tor_cond_t *cond)
  153. {
  154. if (pthread_cond_destroy(&cond->cond)) {
  155. log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
  156. return;
  157. }
  158. }
  159. /** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
  160. * All waiters on the condition must wait holding the same <b>mutex</b>.
  161. * Returns 0 on success, -1 on failure, 1 on timeout. */
  162. int
  163. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv)
  164. {
  165. if (tv == NULL) {
  166. return pthread_cond_wait(&cond->cond, &mutex->mutex) ? -1 : 0;
  167. } else {
  168. struct timespec ts;
  169. int r;
  170. ts.tv_sec = tv->tv_sec;
  171. ts.tv_nsec = tv->tv_usec * 1000;
  172. r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts);
  173. if (r == 0)
  174. return 0;
  175. else if (r == ETIMEDOUT)
  176. return 1;
  177. else
  178. return -1;
  179. }
  180. }
  181. /** Wake up one of the waiters on <b>cond</b>. */
  182. void
  183. tor_cond_signal_one(tor_cond_t *cond)
  184. {
  185. pthread_cond_signal(&cond->cond);
  186. }
  187. /** Wake up all of the waiters on <b>cond</b>. */
  188. void
  189. tor_cond_signal_all(tor_cond_t *cond)
  190. {
  191. pthread_cond_broadcast(&cond->cond);
  192. }
  193. /** Set up common structures for use by threading. */
  194. void
  195. tor_threads_init(void)
  196. {
  197. if (!threads_initialized) {
  198. pthread_mutexattr_init(&attr_reentrant);
  199. pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE);
  200. tor_assert(0==pthread_attr_init(&attr_detached));
  201. tor_assert(0==pthread_attr_setdetachstate(&attr_detached, 1));
  202. threads_initialized = 1;
  203. set_main_thread();
  204. }
  205. }