compat_winthreads.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compat_winthreads.c
  7. *
  8. * \brief Implementation for the windows-based multithreading backend
  9. * functions.
  10. */
  11. #ifdef _WIN32
  12. #include "compat.h"
  13. #include <windows.h>
  14. #include <process.h>
  15. #include "util.h"
  16. #include "container.h"
  17. #include "torlog.h"
  18. /* This value is more or less total cargo-cult */
  19. #define SPIN_COUNT 2000
  20. /** Minimalist interface to run a void function in the background. On
  21. * Unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  22. * func should not return, but rather should call spawn_exit.
  23. *
  24. * NOTE: if <b>data</b> is used, it should not be allocated on the stack,
  25. * since in a multithreaded environment, there is no way to be sure that
  26. * the caller's stack will still be around when the called function is
  27. * running.
  28. */
  29. int
  30. spawn_func(void (*func)(void *), void *data)
  31. {
  32. int rv;
  33. rv = (int)_beginthread(func, 0, data);
  34. if (rv == (int)-1)
  35. return -1;
  36. return 0;
  37. }
  38. /** End the current thread/process.
  39. */
  40. void
  41. spawn_exit(void)
  42. {
  43. _endthread();
  44. // LCOV_EXCL_START
  45. //we should never get here. my compiler thinks that _endthread returns, this
  46. //is an attempt to fool it.
  47. tor_assert(0);
  48. _exit(0); // exit ok: unreachable.
  49. // LCOV_EXCL_STOP
  50. }
  51. void
  52. tor_mutex_init(tor_mutex_t *m)
  53. {
  54. InitializeCriticalSection(&m->mutex);
  55. }
  56. void
  57. tor_mutex_init_nonrecursive(tor_mutex_t *m)
  58. {
  59. InitializeCriticalSection(&m->mutex);
  60. }
  61. void
  62. tor_mutex_uninit(tor_mutex_t *m)
  63. {
  64. DeleteCriticalSection(&m->mutex);
  65. }
  66. void
  67. tor_mutex_acquire(tor_mutex_t *m)
  68. {
  69. tor_assert(m);
  70. EnterCriticalSection(&m->mutex);
  71. }
  72. void
  73. tor_mutex_release(tor_mutex_t *m)
  74. {
  75. LeaveCriticalSection(&m->mutex);
  76. }
  77. unsigned long
  78. tor_get_thread_id(void)
  79. {
  80. return (unsigned long)GetCurrentThreadId();
  81. }
  82. int
  83. tor_cond_init(tor_cond_t *cond)
  84. {
  85. memset(cond, 0, sizeof(tor_cond_t));
  86. if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
  87. return -1;
  88. }
  89. if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
  90. DeleteCriticalSection(&cond->lock);
  91. return -1;
  92. }
  93. cond->n_waiting = cond->n_to_wake = cond->generation = 0;
  94. return 0;
  95. }
  96. void
  97. tor_cond_uninit(tor_cond_t *cond)
  98. {
  99. DeleteCriticalSection(&cond->lock);
  100. CloseHandle(cond->event);
  101. }
  102. static void
  103. tor_cond_signal_impl(tor_cond_t *cond, int broadcast)
  104. {
  105. EnterCriticalSection(&cond->lock);
  106. if (broadcast)
  107. cond->n_to_wake = cond->n_waiting;
  108. else
  109. ++cond->n_to_wake;
  110. cond->generation++;
  111. SetEvent(cond->event);
  112. LeaveCriticalSection(&cond->lock);
  113. }
  114. void
  115. tor_cond_signal_one(tor_cond_t *cond)
  116. {
  117. tor_cond_signal_impl(cond, 0);
  118. }
  119. void
  120. tor_cond_signal_all(tor_cond_t *cond)
  121. {
  122. tor_cond_signal_impl(cond, 1);
  123. }
  124. int
  125. tor_threadlocal_init(tor_threadlocal_t *threadlocal)
  126. {
  127. threadlocal->index = TlsAlloc();
  128. return (threadlocal->index == TLS_OUT_OF_INDEXES) ? -1 : 0;
  129. }
  130. void
  131. tor_threadlocal_destroy(tor_threadlocal_t *threadlocal)
  132. {
  133. TlsFree(threadlocal->index);
  134. memset(threadlocal, 0, sizeof(tor_threadlocal_t));
  135. }
  136. void *
  137. tor_threadlocal_get(tor_threadlocal_t *threadlocal)
  138. {
  139. void *value = TlsGetValue(threadlocal->index);
  140. if (value == NULL) {
  141. DWORD err = GetLastError();
  142. if (err != ERROR_SUCCESS) {
  143. char *msg = format_win32_error(err);
  144. log_err(LD_GENERAL, "Error retrieving thread-local value: %s", msg);
  145. tor_free(msg);
  146. tor_assert(err == ERROR_SUCCESS);
  147. }
  148. }
  149. return value;
  150. }
  151. void
  152. tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
  153. {
  154. BOOL ok = TlsSetValue(threadlocal->index, value);
  155. if (!ok) {
  156. DWORD err = GetLastError();
  157. char *msg = format_win32_error(err);
  158. log_err(LD_GENERAL, "Error adjusting thread-local value: %s", msg);
  159. tor_free(msg);
  160. tor_assert(ok);
  161. }
  162. }
  163. int
  164. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *lock_, const struct timeval *tv)
  165. {
  166. CRITICAL_SECTION *lock = &lock_->mutex;
  167. int generation_at_start;
  168. int waiting = 1;
  169. int result = -1;
  170. DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
  171. if (tv)
  172. ms_orig = ms = tv->tv_sec*1000 + (tv->tv_usec+999)/1000;
  173. EnterCriticalSection(&cond->lock);
  174. ++cond->n_waiting;
  175. generation_at_start = cond->generation;
  176. LeaveCriticalSection(&cond->lock);
  177. LeaveCriticalSection(lock);
  178. startTime = GetTickCount();
  179. do {
  180. DWORD res;
  181. res = WaitForSingleObject(cond->event, ms);
  182. EnterCriticalSection(&cond->lock);
  183. if (cond->n_to_wake &&
  184. cond->generation != generation_at_start) {
  185. --cond->n_to_wake;
  186. --cond->n_waiting;
  187. result = 0;
  188. waiting = 0;
  189. goto out;
  190. } else if (res != WAIT_OBJECT_0) {
  191. result = (res==WAIT_TIMEOUT) ? 1 : -1;
  192. --cond->n_waiting;
  193. waiting = 0;
  194. goto out;
  195. } else if (ms != INFINITE) {
  196. endTime = GetTickCount();
  197. if (startTime + ms_orig <= endTime) {
  198. result = 1; /* Timeout */
  199. --cond->n_waiting;
  200. waiting = 0;
  201. goto out;
  202. } else {
  203. ms = startTime + ms_orig - endTime;
  204. }
  205. }
  206. /* If we make it here, we are still waiting. */
  207. if (cond->n_to_wake == 0) {
  208. /* There is nobody else who should wake up; reset
  209. * the event. */
  210. ResetEvent(cond->event);
  211. }
  212. out:
  213. LeaveCriticalSection(&cond->lock);
  214. } while (waiting);
  215. EnterCriticalSection(lock);
  216. EnterCriticalSection(&cond->lock);
  217. if (!cond->n_waiting)
  218. ResetEvent(cond->event);
  219. LeaveCriticalSection(&cond->lock);
  220. return result;
  221. }
  222. void
  223. tor_threads_init(void)
  224. {
  225. set_main_thread();
  226. }
  227. #endif /* defined(_WIN32) */