compat_winthreads.c 5.6 KB

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