compat_winthreads.c 5.1 KB

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