pthread.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Code for simulating pthreads API on Windows. This is Git-specific,
  3. * but it is enough for Numexpr needs too.
  4. *
  5. * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * DISCLAIMER: The implementation is Git-specific, it is subset of original
  26. * Pthreads API, without lots of other features that Git doesn't use.
  27. * Git also makes sure that the passed arguments are valid, so there's
  28. * no need for double-checking.
  29. */
  30. #include "pthread.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <process.h>
  34. #include <errno.h>
  35. #include <limits.h>
  36. void die(const char *err, ...)
  37. {
  38. printf("%s", err);
  39. exit(-1);
  40. }
  41. static unsigned __stdcall win32_start_routine(void *arg)
  42. {
  43. pthread_t *thread = (pthread_t*)arg;
  44. thread->arg = thread->start_routine(thread->arg);
  45. return 0;
  46. }
  47. int pthread_create(pthread_t *thread, const void *unused,
  48. void *(*start_routine)(void*), void *arg)
  49. {
  50. thread->arg = arg;
  51. thread->start_routine = start_routine;
  52. thread->handle = (HANDLE)
  53. _beginthreadex(NULL, 0, win32_start_routine, thread, 0, NULL);
  54. if (!thread->handle)
  55. return errno;
  56. else
  57. return 0;
  58. }
  59. int win32_pthread_join(pthread_t *thread, void **value_ptr)
  60. {
  61. DWORD result = WaitForSingleObject(thread->handle, INFINITE);
  62. switch (result) {
  63. case WAIT_OBJECT_0:
  64. if (value_ptr)
  65. *value_ptr = thread->arg;
  66. return 0;
  67. case WAIT_ABANDONED:
  68. return EINVAL;
  69. default:
  70. return GetLastError();
  71. }
  72. }
  73. int pthread_cond_init(pthread_cond_t *cond, const void *unused)
  74. {
  75. cond->waiters = 0;
  76. cond->was_broadcast = 0;
  77. InitializeCriticalSection(&cond->waiters_lock);
  78. cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
  79. if (!cond->sema)
  80. die("CreateSemaphore() failed");
  81. cond->continue_broadcast = CreateEvent(NULL, /* security */
  82. FALSE, /* auto-reset */
  83. FALSE, /* not signaled */
  84. NULL); /* name */
  85. if (!cond->continue_broadcast)
  86. die("CreateEvent() failed");
  87. return 0;
  88. }
  89. int pthread_cond_destroy(pthread_cond_t *cond)
  90. {
  91. CloseHandle(cond->sema);
  92. CloseHandle(cond->continue_broadcast);
  93. DeleteCriticalSection(&cond->waiters_lock);
  94. return 0;
  95. }
  96. int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
  97. {
  98. int last_waiter;
  99. EnterCriticalSection(&cond->waiters_lock);
  100. cond->waiters++;
  101. LeaveCriticalSection(&cond->waiters_lock);
  102. /*
  103. * Unlock external mutex and wait for signal.
  104. * NOTE: we've held mutex locked long enough to increment
  105. * waiters count above, so there's no problem with
  106. * leaving mutex unlocked before we wait on semaphore.
  107. */
  108. LeaveCriticalSection(mutex);
  109. /* let's wait - ignore return value */
  110. WaitForSingleObject(cond->sema, INFINITE);
  111. /*
  112. * Decrease waiters count. If we are the last waiter, then we must
  113. * notify the broadcasting thread that it can continue.
  114. * But if we continued due to cond_signal, we do not have to do that
  115. * because the signaling thread knows that only one waiter continued.
  116. */
  117. EnterCriticalSection(&cond->waiters_lock);
  118. cond->waiters--;
  119. last_waiter = cond->was_broadcast && cond->waiters == 0;
  120. LeaveCriticalSection(&cond->waiters_lock);
  121. if (last_waiter) {
  122. /*
  123. * cond_broadcast was issued while mutex was held. This means
  124. * that all other waiters have continued, but are contending
  125. * for the mutex at the end of this function because the
  126. * broadcasting thread did not leave cond_broadcast, yet.
  127. * (This is so that it can be sure that each waiter has
  128. * consumed exactly one slice of the semaphor.)
  129. * The last waiter must tell the broadcasting thread that it
  130. * can go on.
  131. */
  132. SetEvent(cond->continue_broadcast);
  133. /*
  134. * Now we go on to contend with all other waiters for
  135. * the mutex. Auf in den Kampf!
  136. */
  137. }
  138. /* lock external mutex again */
  139. EnterCriticalSection(mutex);
  140. return 0;
  141. }
  142. /*
  143. * IMPORTANT: This implementation requires that pthread_cond_signal
  144. * is called while the mutex is held that is used in the corresponding
  145. * pthread_cond_wait calls!
  146. */
  147. int pthread_cond_signal(pthread_cond_t *cond)
  148. {
  149. int have_waiters;
  150. EnterCriticalSection(&cond->waiters_lock);
  151. have_waiters = cond->waiters > 0;
  152. LeaveCriticalSection(&cond->waiters_lock);
  153. /*
  154. * Signal only when there are waiters
  155. */
  156. if (have_waiters)
  157. return ReleaseSemaphore(cond->sema, 1, NULL) ?
  158. 0 : GetLastError();
  159. else
  160. return 0;
  161. }
  162. /*
  163. * DOUBLY IMPORTANT: This implementation requires that pthread_cond_broadcast
  164. * is called while the mutex is held that is used in the corresponding
  165. * pthread_cond_wait calls!
  166. */
  167. int pthread_cond_broadcast(pthread_cond_t *cond)
  168. {
  169. EnterCriticalSection(&cond->waiters_lock);
  170. if ((cond->was_broadcast = cond->waiters > 0)) {
  171. /* wake up all waiters */
  172. ReleaseSemaphore(cond->sema, cond->waiters, NULL);
  173. LeaveCriticalSection(&cond->waiters_lock);
  174. /*
  175. * At this point all waiters continue. Each one takes its
  176. * slice of the semaphor. Now it's our turn to wait: Since
  177. * the external mutex is held, no thread can leave cond_wait,
  178. * yet. For this reason, we can be sure that no thread gets
  179. * a chance to eat *more* than one slice. OTOH, it means
  180. * that the last waiter must send us a wake-up.
  181. */
  182. WaitForSingleObject(cond->continue_broadcast, INFINITE);
  183. /*
  184. * Since the external mutex is held, no thread can enter
  185. * cond_wait, and, hence, it is safe to reset this flag
  186. * without cond->waiters_lock held.
  187. */
  188. cond->was_broadcast = 0;
  189. } else {
  190. LeaveCriticalSection(&cond->waiters_lock);
  191. }
  192. return 0;
  193. }