compat_winthreads.c 5.5 KB

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