compat_winthreads.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_uninit(tor_mutex_t *m)
  47. {
  48. DeleteCriticalSection(&m->mutex);
  49. }
  50. void
  51. tor_mutex_acquire(tor_mutex_t *m)
  52. {
  53. tor_assert(m);
  54. EnterCriticalSection(&m->mutex);
  55. }
  56. void
  57. tor_mutex_release(tor_mutex_t *m)
  58. {
  59. LeaveCriticalSection(&m->mutex);
  60. }
  61. unsigned long
  62. tor_get_thread_id(void)
  63. {
  64. return (unsigned long)GetCurrentThreadId();
  65. }
  66. int
  67. tor_cond_init(tor_cond_t *cond)
  68. {
  69. memset(cond, 0, sizeof(tor_cond_t));
  70. if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
  71. return -1;
  72. }
  73. if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
  74. DeleteCriticalSection(&cond->lock);
  75. return -1;
  76. }
  77. cond->n_waiting = cond->n_to_wake = cond->generation = 0;
  78. return 0;
  79. }
  80. void
  81. tor_cond_uninit(tor_cond_t *cond)
  82. {
  83. DeleteCriticalSection(&cond->lock);
  84. CloseHandle(cond->event);
  85. }
  86. static void
  87. tor_cond_signal_impl(tor_cond_t *cond, int broadcast)
  88. {
  89. EnterCriticalSection(&cond->lock);
  90. if (broadcast)
  91. cond->n_to_wake = cond->n_waiting;
  92. else
  93. ++cond->n_to_wake;
  94. cond->generation++;
  95. SetEvent(cond->event);
  96. LeaveCriticalSection(&cond->lock);
  97. return 0;
  98. }
  99. void
  100. tor_cond_signal_one(tor_cond_t *cond)
  101. {
  102. tor_cond_signal_impl(cond, 0);
  103. }
  104. void
  105. tor_cond_signal_all(tor_cond_t *cond)
  106. {
  107. tor_cond_signal_impl(cond, 1);
  108. }
  109. int
  110. tor_cond_wait(tor_cond_t *cond, tor_mutex_t *lock, const struct timeval *tv)
  111. {
  112. CRITICAL_SECTION *lock = &lock->mutex;
  113. int generation_at_start;
  114. int waiting = 1;
  115. int result = -1;
  116. DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
  117. if (tv)
  118. ms_orig = ms = evutil_tv_to_msec_(tv);
  119. EnterCriticalSection(&cond->lock);
  120. ++cond->n_waiting;
  121. generation_at_start = cond->generation;
  122. LeaveCriticalSection(&cond->lock);
  123. LeaveCriticalSection(lock);
  124. startTime = GetTickCount();
  125. do {
  126. DWORD res;
  127. res = WaitForSingleObject(cond->event, ms);
  128. EnterCriticalSection(&cond->lock);
  129. if (cond->n_to_wake &&
  130. cond->generation != generation_at_start) {
  131. --cond->n_to_wake;
  132. --cond->n_waiting;
  133. result = 0;
  134. waiting = 0;
  135. goto out;
  136. } else if (res != WAIT_OBJECT_0) {
  137. result = (res==WAIT_TIMEOUT) ? 1 : -1;
  138. --cond->n_waiting;
  139. waiting = 0;
  140. goto out;
  141. } else if (ms != INFINITE) {
  142. endTime = GetTickCount();
  143. if (startTime + ms_orig <= endTime) {
  144. result = 1; /* Timeout */
  145. --cond->n_waiting;
  146. waiting = 0;
  147. goto out;
  148. } else {
  149. ms = startTime + ms_orig - endTime;
  150. }
  151. }
  152. /* If we make it here, we are still waiting. */
  153. if (cond->n_to_wake == 0) {
  154. /* There is nobody else who should wake up; reset
  155. * the event. */
  156. ResetEvent(cond->event);
  157. }
  158. out:
  159. LeaveCriticalSection(&cond->lock);
  160. } while (waiting);
  161. EnterCriticalSection(lock);
  162. EnterCriticalSection(&cond->lock);
  163. if (!cond->n_waiting)
  164. ResetEvent(cond->event);
  165. LeaveCriticalSection(&cond->lock);
  166. return result;
  167. }
  168. void
  169. tor_threads_init(void)
  170. {
  171. set_main_thread();
  172. }