compat_winthreads.c 5.4 KB

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