compat_winthreads.c 6.0 KB

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