compat_winthreads.c 5.3 KB

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