compat_winthreads.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /** Minimalist interface to run a void function in the background. On
  12. * Unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  13. * func should not return, but rather should call spawn_exit.
  14. *
  15. * NOTE: if <b>data</b> is used, it should not be allocated on the stack,
  16. * since in a multithreaded environment, there is no way to be sure that
  17. * the caller's stack will still be around when the called function is
  18. * running.
  19. */
  20. int
  21. spawn_func(void (*func)(void *), void *data)
  22. {
  23. int rv;
  24. rv = (int)_beginthread(func, 0, data);
  25. if (rv == (int)-1)
  26. return -1;
  27. return 0;
  28. }
  29. /** End the current thread/process.
  30. */
  31. void
  32. spawn_exit(void)
  33. {
  34. _endthread();
  35. //we should never get here. my compiler thinks that _endthread returns, this
  36. //is an attempt to fool it.
  37. tor_assert(0);
  38. _exit(0);
  39. }
  40. void
  41. tor_mutex_init(tor_mutex_t *m)
  42. {
  43. InitializeCriticalSection(&m->mutex);
  44. }
  45. void
  46. tor_mutex_init_nonreentrant(tor_mutex_t *m)
  47. {
  48. InitializeCriticalSection(&m->mutex);
  49. }
  50. void
  51. tor_mutex_uninit(tor_mutex_t *m)
  52. {
  53. DeleteCriticalSection(&m->mutex);
  54. }
  55. void
  56. tor_mutex_acquire(tor_mutex_t *m)
  57. {
  58. tor_assert(m);
  59. EnterCriticalSection(&m->mutex);
  60. }
  61. void
  62. tor_mutex_release(tor_mutex_t *m)
  63. {
  64. LeaveCriticalSection(&m->mutex);
  65. }
  66. unsigned long
  67. tor_get_thread_id(void)
  68. {
  69. return (unsigned long)GetCurrentThreadId();
  70. }
  71. int
  72. tor_cond_init(tor_cond_t *cond)
  73. {
  74. memset(cond, 0, sizeof(tor_cond_t));
  75. if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
  76. return -1;
  77. }
  78. if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
  79. DeleteCriticalSection(&cond->lock);
  80. return -1;
  81. }
  82. cond->n_waiting = cond->n_to_wake = cond->generation = 0;
  83. return 0;
  84. }
  85. void
  86. tor_cond_uninit(tor_cond_t *cond)
  87. {
  88. DeleteCriticalSection(&cond->lock);
  89. CloseHandle(cond->event);
  90. }
  91. static void
  92. tor_cond_signal_impl(tor_cond_t *cond, int broadcast)
  93. {
  94. EnterCriticalSection(&cond->lock);
  95. if (broadcast)
  96. cond->n_to_wake = cond->n_waiting;
  97. else
  98. ++cond->n_to_wake;
  99. cond->generation++;
  100. SetEvent(cond->event);
  101. LeaveCriticalSection(&cond->lock);
  102. return 0;
  103. }
  104. void
  105. tor_cond_signal_one(tor_cond_t *cond)
  106. {
  107. tor_cond_signal_impl(cond, 0);
  108. }
  109. void
  110. tor_cond_signal_all(tor_cond_t *cond)
  111. {
  112. tor_cond_signal_impl(cond, 1);
  113. }
  114. int
  115. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *lock, const struct timeval *tv)
  116. {
  117. CRITICAL_SECTION *lock = &lock->mutex;
  118. int generation_at_start;
  119. int waiting = 1;
  120. int result = -1;
  121. DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
  122. if (tv)
  123. ms_orig = ms = evutil_tv_to_msec_(tv);
  124. EnterCriticalSection(&cond->lock);
  125. ++cond->n_waiting;
  126. generation_at_start = cond->generation;
  127. LeaveCriticalSection(&cond->lock);
  128. LeaveCriticalSection(lock);
  129. startTime = GetTickCount();
  130. do {
  131. DWORD res;
  132. res = WaitForSingleObject(cond->event, ms);
  133. EnterCriticalSection(&cond->lock);
  134. if (cond->n_to_wake &&
  135. cond->generation != generation_at_start) {
  136. --cond->n_to_wake;
  137. --cond->n_waiting;
  138. result = 0;
  139. waiting = 0;
  140. goto out;
  141. } else if (res != WAIT_OBJECT_0) {
  142. result = (res==WAIT_TIMEOUT) ? 1 : -1;
  143. --cond->n_waiting;
  144. waiting = 0;
  145. goto out;
  146. } else if (ms != INFINITE) {
  147. endTime = GetTickCount();
  148. if (startTime + ms_orig <= endTime) {
  149. result = 1; /* Timeout */
  150. --cond->n_waiting;
  151. waiting = 0;
  152. goto out;
  153. } else {
  154. ms = startTime + ms_orig - endTime;
  155. }
  156. }
  157. /* If we make it here, we are still waiting. */
  158. if (cond->n_to_wake == 0) {
  159. /* There is nobody else who should wake up; reset
  160. * the event. */
  161. ResetEvent(cond->event);
  162. }
  163. out:
  164. LeaveCriticalSection(&cond->lock);
  165. } while (waiting);
  166. EnterCriticalSection(lock);
  167. EnterCriticalSection(&cond->lock);
  168. if (!cond->n_waiting)
  169. ResetEvent(cond->event);
  170. LeaveCriticalSection(&cond->lock);
  171. return result;
  172. }
  173. void
  174. tor_threads_init(void)
  175. {
  176. set_main_thread();
  177. }