compat_winthreads.c 4.3 KB

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