compat_winthreads.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_cond_wait(tor_cond_t *cond, tor_mutex_t *lock_, const struct timeval *tv)
  119. {
  120. CRITICAL_SECTION *lock = &lock_->mutex;
  121. int generation_at_start;
  122. int waiting = 1;
  123. int result = -1;
  124. DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
  125. if (tv)
  126. ms_orig = ms = tv->tv_sec*1000 + (tv->tv_usec+999)/1000;
  127. EnterCriticalSection(&cond->lock);
  128. ++cond->n_waiting;
  129. generation_at_start = cond->generation;
  130. LeaveCriticalSection(&cond->lock);
  131. LeaveCriticalSection(lock);
  132. startTime = GetTickCount();
  133. do {
  134. DWORD res;
  135. res = WaitForSingleObject(cond->event, ms);
  136. EnterCriticalSection(&cond->lock);
  137. if (cond->n_to_wake &&
  138. cond->generation != generation_at_start) {
  139. --cond->n_to_wake;
  140. --cond->n_waiting;
  141. result = 0;
  142. waiting = 0;
  143. goto out;
  144. } else if (res != WAIT_OBJECT_0) {
  145. result = (res==WAIT_TIMEOUT) ? 1 : -1;
  146. --cond->n_waiting;
  147. waiting = 0;
  148. goto out;
  149. } else if (ms != INFINITE) {
  150. endTime = GetTickCount();
  151. if (startTime + ms_orig <= endTime) {
  152. result = 1; /* Timeout */
  153. --cond->n_waiting;
  154. waiting = 0;
  155. goto out;
  156. } else {
  157. ms = startTime + ms_orig - endTime;
  158. }
  159. }
  160. /* If we make it here, we are still waiting. */
  161. if (cond->n_to_wake == 0) {
  162. /* There is nobody else who should wake up; reset
  163. * the event. */
  164. ResetEvent(cond->event);
  165. }
  166. out:
  167. LeaveCriticalSection(&cond->lock);
  168. } while (waiting);
  169. EnterCriticalSection(lock);
  170. EnterCriticalSection(&cond->lock);
  171. if (!cond->n_waiting)
  172. ResetEvent(cond->event);
  173. LeaveCriticalSection(&cond->lock);
  174. return result;
  175. }
  176. void
  177. tor_threads_init(void)
  178. {
  179. set_main_thread();
  180. }
  181. #endif