sem_timedwait.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_timedwait.c
  5. *
  6. * Purpose:
  7. * Semaphores aren't actually part of the PThreads standard.
  8. * They are defined by the POSIX Standard:
  9. *
  10. * POSIX 1003.1b-1993 (POSIX.1b)
  11. *
  12. * -------------------------------------------------------------
  13. *
  14. * --------------------------------------------------------------------------
  15. *
  16. * Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The current list of contributors is contained
  23. * in the file CONTRIBUTORS included with the source
  24. * code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include "pthread.h"
  44. #include "semaphore.h"
  45. #include "implement.h"
  46. typedef struct {
  47. sem_t sem;
  48. int * resultPtr;
  49. } sem_timedwait_cleanup_args_t;
  50. static void PTW32_CDECL
  51. ptw32_sem_timedwait_cleanup (void * args)
  52. {
  53. sem_timedwait_cleanup_args_t * a = (sem_timedwait_cleanup_args_t *)args;
  54. sem_t s = a->sem;
  55. if (pthread_mutex_lock (&s->lock) == 0)
  56. {
  57. /*
  58. * We either timed out or were cancelled.
  59. * If someone has posted between then and now we try to take the semaphore.
  60. * Otherwise the semaphore count may be wrong after we
  61. * return. In the case of a cancellation, it is as if we
  62. * were cancelled just before we return (after taking the semaphore)
  63. * which is ok.
  64. */
  65. if (WaitForSingleObject(s->sem, 0) == WAIT_OBJECT_0)
  66. {
  67. /* We got the semaphore on the second attempt */
  68. *(a->resultPtr) = 0;
  69. }
  70. else
  71. {
  72. /* Indicate we're no longer waiting */
  73. s->value++;
  74. #if defined(NEED_SEM)
  75. if (s->value > 0)
  76. {
  77. s->leftToUnblock = 0;
  78. }
  79. #else
  80. /*
  81. * Don't release the W32 sema, it doesn't need adjustment
  82. * because it doesn't record the number of waiters.
  83. */
  84. #endif
  85. }
  86. (void) pthread_mutex_unlock (&s->lock);
  87. }
  88. }
  89. int
  90. sem_timedwait (sem_t * sem, const struct timespec *abstime)
  91. /*
  92. * ------------------------------------------------------
  93. * DOCPUBLIC
  94. * This function waits on a semaphore possibly until
  95. * 'abstime' time.
  96. *
  97. * PARAMETERS
  98. * sem
  99. * pointer to an instance of sem_t
  100. *
  101. * abstime
  102. * pointer to an instance of struct timespec
  103. *
  104. * DESCRIPTION
  105. * This function waits on a semaphore. If the
  106. * semaphore value is greater than zero, it decreases
  107. * its value by one. If the semaphore value is zero, then
  108. * the calling thread (or process) is blocked until it can
  109. * successfully decrease the value or until interrupted by
  110. * a signal.
  111. *
  112. * If 'abstime' is a NULL pointer then this function will
  113. * block until it can successfully decrease the value or
  114. * until interrupted by a signal.
  115. *
  116. * RESULTS
  117. * 0 successfully decreased semaphore,
  118. * -1 failed, error in errno
  119. * ERRNO
  120. * EINVAL 'sem' is not a valid semaphore,
  121. * ENOSYS semaphores are not supported,
  122. * EINTR the function was interrupted by a signal,
  123. * EDEADLK a deadlock condition was detected.
  124. * ETIMEDOUT abstime elapsed before success.
  125. *
  126. * ------------------------------------------------------
  127. */
  128. {
  129. int result = 0;
  130. sem_t s = *sem;
  131. pthread_testcancel();
  132. if (sem == NULL)
  133. {
  134. result = EINVAL;
  135. }
  136. else
  137. {
  138. DWORD milliseconds;
  139. if (abstime == NULL)
  140. {
  141. milliseconds = INFINITE;
  142. }
  143. else
  144. {
  145. /*
  146. * Calculate timeout as milliseconds from current system time.
  147. */
  148. milliseconds = ptw32_relmillisecs (abstime);
  149. }
  150. if ((result = pthread_mutex_lock (&s->lock)) == 0)
  151. {
  152. int v;
  153. /* See sem_destroy.c
  154. */
  155. if (*sem == NULL)
  156. {
  157. (void) pthread_mutex_unlock (&s->lock);
  158. errno = EINVAL;
  159. return -1;
  160. }
  161. v = --s->value;
  162. (void) pthread_mutex_unlock (&s->lock);
  163. if (v < 0)
  164. {
  165. #if defined(NEED_SEM)
  166. int timedout;
  167. #endif
  168. sem_timedwait_cleanup_args_t cleanup_args;
  169. cleanup_args.sem = s;
  170. cleanup_args.resultPtr = &result;
  171. #if defined(_MSC_VER) && _MSC_VER < 1400
  172. #pragma inline_depth(0)
  173. #endif
  174. /* Must wait */
  175. pthread_cleanup_push(ptw32_sem_timedwait_cleanup, (void *) &cleanup_args);
  176. #if defined(NEED_SEM)
  177. timedout =
  178. #endif
  179. result = pthreadCancelableTimedWait (s->sem, milliseconds);
  180. pthread_cleanup_pop(result);
  181. #if defined(_MSC_VER) && _MSC_VER < 1400
  182. #pragma inline_depth()
  183. #endif
  184. #if defined(NEED_SEM)
  185. if (!timedout && pthread_mutex_lock (&s->lock) == 0)
  186. {
  187. if (*sem == NULL)
  188. {
  189. (void) pthread_mutex_unlock (&s->lock);
  190. errno = EINVAL;
  191. return -1;
  192. }
  193. if (s->leftToUnblock > 0)
  194. {
  195. --s->leftToUnblock;
  196. SetEvent(s->sem);
  197. }
  198. (void) pthread_mutex_unlock (&s->lock);
  199. }
  200. #endif /* NEED_SEM */
  201. }
  202. }
  203. }
  204. if (result != 0)
  205. {
  206. errno = result;
  207. return -1;
  208. }
  209. return 0;
  210. } /* sem_timedwait */