compat_winthreads.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <windows.h>
  13. #include <process.h>
  14. #include "lib/thread/threads.h"
  15. #include "lib/log/torlog.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. 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. unsigned long
  52. tor_get_thread_id(void)
  53. {
  54. return (unsigned long)GetCurrentThreadId();
  55. }
  56. int
  57. tor_cond_init(tor_cond_t *cond)
  58. {
  59. memset(cond, 0, sizeof(tor_cond_t));
  60. if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
  61. return -1;
  62. }
  63. if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
  64. DeleteCriticalSection(&cond->lock);
  65. return -1;
  66. }
  67. cond->n_waiting = cond->n_to_wake = cond->generation = 0;
  68. return 0;
  69. }
  70. void
  71. tor_cond_uninit(tor_cond_t *cond)
  72. {
  73. DeleteCriticalSection(&cond->lock);
  74. CloseHandle(cond->event);
  75. }
  76. static void
  77. tor_cond_signal_impl(tor_cond_t *cond, int broadcast)
  78. {
  79. EnterCriticalSection(&cond->lock);
  80. if (broadcast)
  81. cond->n_to_wake = cond->n_waiting;
  82. else
  83. ++cond->n_to_wake;
  84. cond->generation++;
  85. SetEvent(cond->event);
  86. LeaveCriticalSection(&cond->lock);
  87. }
  88. void
  89. tor_cond_signal_one(tor_cond_t *cond)
  90. {
  91. tor_cond_signal_impl(cond, 0);
  92. }
  93. void
  94. tor_cond_signal_all(tor_cond_t *cond)
  95. {
  96. tor_cond_signal_impl(cond, 1);
  97. }
  98. int
  99. tor_threadlocal_init(tor_threadlocal_t *threadlocal)
  100. {
  101. threadlocal->index = TlsAlloc();
  102. return (threadlocal->index == TLS_OUT_OF_INDEXES) ? -1 : 0;
  103. }
  104. void
  105. tor_threadlocal_destroy(tor_threadlocal_t *threadlocal)
  106. {
  107. TlsFree(threadlocal->index);
  108. memset(threadlocal, 0, sizeof(tor_threadlocal_t));
  109. }
  110. void *
  111. tor_threadlocal_get(tor_threadlocal_t *threadlocal)
  112. {
  113. void *value = TlsGetValue(threadlocal->index);
  114. if (value == NULL) {
  115. DWORD err = GetLastError();
  116. if (err != ERROR_SUCCESS) {
  117. char *msg = format_win32_error(err);
  118. log_err(LD_GENERAL, "Error retrieving thread-local value: %s", msg);
  119. tor_free(msg);
  120. tor_assert(err == ERROR_SUCCESS);
  121. }
  122. }
  123. return value;
  124. }
  125. void
  126. tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
  127. {
  128. BOOL ok = TlsSetValue(threadlocal->index, value);
  129. if (!ok) {
  130. DWORD err = GetLastError();
  131. char *msg = format_win32_error(err);
  132. log_err(LD_GENERAL, "Error adjusting thread-local value: %s", msg);
  133. tor_free(msg);
  134. tor_assert(ok);
  135. }
  136. }
  137. int
  138. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *lock_, const struct timeval *tv)
  139. {
  140. CRITICAL_SECTION *lock = &lock_->mutex;
  141. int generation_at_start;
  142. int waiting = 1;
  143. int result = -1;
  144. DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
  145. if (tv)
  146. ms_orig = ms = tv->tv_sec*1000 + (tv->tv_usec+999)/1000;
  147. EnterCriticalSection(&cond->lock);
  148. ++cond->n_waiting;
  149. generation_at_start = cond->generation;
  150. LeaveCriticalSection(&cond->lock);
  151. LeaveCriticalSection(lock);
  152. startTime = GetTickCount();
  153. do {
  154. DWORD res;
  155. res = WaitForSingleObject(cond->event, ms);
  156. EnterCriticalSection(&cond->lock);
  157. if (cond->n_to_wake &&
  158. cond->generation != generation_at_start) {
  159. --cond->n_to_wake;
  160. --cond->n_waiting;
  161. result = 0;
  162. waiting = 0;
  163. goto out;
  164. } else if (res != WAIT_OBJECT_0) {
  165. result = (res==WAIT_TIMEOUT) ? 1 : -1;
  166. --cond->n_waiting;
  167. waiting = 0;
  168. goto out;
  169. } else if (ms != INFINITE) {
  170. endTime = GetTickCount();
  171. if (startTime + ms_orig <= endTime) {
  172. result = 1; /* Timeout */
  173. --cond->n_waiting;
  174. waiting = 0;
  175. goto out;
  176. } else {
  177. ms = startTime + ms_orig - endTime;
  178. }
  179. }
  180. /* If we make it here, we are still waiting. */
  181. if (cond->n_to_wake == 0) {
  182. /* There is nobody else who should wake up; reset
  183. * the event. */
  184. ResetEvent(cond->event);
  185. }
  186. out:
  187. LeaveCriticalSection(&cond->lock);
  188. } while (waiting);
  189. EnterCriticalSection(lock);
  190. EnterCriticalSection(&cond->lock);
  191. if (!cond->n_waiting)
  192. ResetEvent(cond->event);
  193. LeaveCriticalSection(&cond->lock);
  194. return result;
  195. }
  196. void
  197. tor_threads_init(void)
  198. {
  199. set_main_thread();
  200. }
  201. #endif /* defined(_WIN32) */