pthread_cond_signal.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * pthread_cond_signal.c
  3. *
  4. * Description:
  5. * This translation unit implements condition variables and their primitives.
  6. *
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  11. * Copyright(C) 2008 Jason Schmidlapp
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. *
  16. * Based upon 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 original list of contributors to the Pthreads-win32 project
  23. * is contained in the file CONTRIBUTORS.ptw32 included with the
  24. * source 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. * -------------------------------------------------------------
  44. * Algorithm:
  45. * See the comments at the top of pthread_cond_wait.c.
  46. */
  47. #include "pthread.h"
  48. #include "implement.h"
  49. static int
  50. pte_cond_unblock (pthread_cond_t * cond, int unblockAll)
  51. /*
  52. * Notes.
  53. *
  54. * Does not use the external mutex for synchronisation,
  55. * therefore semBlockLock is needed.
  56. * mtxUnblockLock is for LEVEL-2 synch. LEVEL-2 is the
  57. * state where the external mutex is not necessarily locked by
  58. * any thread, ie. between cond_wait unlocking and re-acquiring
  59. * the lock after having been signaled or a timeout or
  60. * cancellation.
  61. *
  62. * Uses the following CV elements:
  63. * nWaitersBlocked
  64. * nWaitersToUnblock
  65. * nWaitersGone
  66. * mtxUnblockLock
  67. * semBlockLock
  68. * semBlockQueue
  69. */
  70. {
  71. int result;
  72. pthread_cond_t cv;
  73. int nSignalsToIssue;
  74. if (cond == NULL || *cond == NULL)
  75. {
  76. return EINVAL;
  77. }
  78. cv = *cond;
  79. /*
  80. * No-op if the CV is static and hasn't been initialised yet.
  81. * Assuming that any race condition is harmless.
  82. */
  83. if (cv == PTHREAD_COND_INITIALIZER)
  84. {
  85. return 0;
  86. }
  87. if ((result = pthread_mutex_lock (&(cv->mtxUnblockLock))) != 0)
  88. {
  89. return result;
  90. }
  91. if (0 != cv->nWaitersToUnblock)
  92. {
  93. if (0 == cv->nWaitersBlocked)
  94. {
  95. return pthread_mutex_unlock (&(cv->mtxUnblockLock));
  96. }
  97. if (unblockAll)
  98. {
  99. cv->nWaitersToUnblock += (nSignalsToIssue = cv->nWaitersBlocked);
  100. cv->nWaitersBlocked = 0;
  101. }
  102. else
  103. {
  104. nSignalsToIssue = 1;
  105. cv->nWaitersToUnblock++;
  106. cv->nWaitersBlocked--;
  107. }
  108. }
  109. else if (cv->nWaitersBlocked > cv->nWaitersGone)
  110. {
  111. /* Use the non-cancellable version of sem_wait() */
  112. // if (sem_wait_nocancel (&(cv->semBlockLock)) != 0)
  113. if (sem_wait (&(cv->semBlockLock)) != 0)
  114. {
  115. result = errno;
  116. (void) pthread_mutex_unlock (&(cv->mtxUnblockLock));
  117. return result;
  118. }
  119. if (0 != cv->nWaitersGone)
  120. {
  121. cv->nWaitersBlocked -= cv->nWaitersGone;
  122. cv->nWaitersGone = 0;
  123. }
  124. if (unblockAll)
  125. {
  126. nSignalsToIssue = cv->nWaitersToUnblock = cv->nWaitersBlocked;
  127. cv->nWaitersBlocked = 0;
  128. }
  129. else
  130. {
  131. nSignalsToIssue = cv->nWaitersToUnblock = 1;
  132. cv->nWaitersBlocked--;
  133. }
  134. }
  135. else
  136. {
  137. return pthread_mutex_unlock (&(cv->mtxUnblockLock));
  138. }
  139. if ((result = pthread_mutex_unlock (&(cv->mtxUnblockLock))) == 0)
  140. {
  141. if (sem_post_multiple (&(cv->semBlockQueue), nSignalsToIssue) != 0)
  142. {
  143. result = errno;
  144. }
  145. }
  146. return result;
  147. } /* pte_cond_unblock */
  148. int
  149. pthread_cond_signal (pthread_cond_t * cond)
  150. /*
  151. * ------------------------------------------------------
  152. * DOCPUBLIC
  153. * This function signals a condition variable, waking
  154. * one waiting thread.
  155. * If SCHED_FIFO or SCHED_RR policy threads are waiting
  156. * the highest priority waiter is awakened; otherwise,
  157. * an unspecified waiter is awakened.
  158. *
  159. * PARAMETERS
  160. * cond
  161. * pointer to an instance of pthread_cond_t
  162. *
  163. *
  164. * DESCRIPTION
  165. * This function signals a condition variable, waking
  166. * one waiting thread.
  167. * If SCHED_FIFO or SCHED_RR policy threads are waiting
  168. * the highest priority waiter is awakened; otherwise,
  169. * an unspecified waiter is awakened.
  170. *
  171. * NOTES:
  172. *
  173. * 1) Use when any waiter can respond and only one need
  174. * respond (all waiters being equal).
  175. *
  176. * RESULTS
  177. * 0 successfully signaled condition,
  178. * EINVAL 'cond' is invalid,
  179. *
  180. * ------------------------------------------------------
  181. */
  182. {
  183. /*
  184. * The '0'(FALSE) unblockAll arg means unblock ONE waiter.
  185. */
  186. return (pte_cond_unblock (cond, 0));
  187. } /* pthread_cond_signal */
  188. int
  189. pthread_cond_broadcast (pthread_cond_t * cond)
  190. /*
  191. * ------------------------------------------------------
  192. * DOCPUBLIC
  193. * This function broadcasts the condition variable,
  194. * waking all current waiters.
  195. *
  196. * PARAMETERS
  197. * cond
  198. * pointer to an instance of pthread_cond_t
  199. *
  200. *
  201. * DESCRIPTION
  202. * This function signals a condition variable, waking
  203. * all waiting threads.
  204. *
  205. * NOTES:
  206. *
  207. * 1) Use when more than one waiter may respond to
  208. * predicate change or if any waiting thread may
  209. * not be able to respond
  210. *
  211. * RESULTS
  212. * 0 successfully signalled condition to all
  213. * waiting threads,
  214. * EINVAL 'cond' is invalid
  215. * ENOSPC a required resource has been exhausted,
  216. *
  217. * ------------------------------------------------------
  218. */
  219. {
  220. /*
  221. * The TRUE unblockAll arg means unblock ALL waiters.
  222. */
  223. return (pte_cond_unblock (cond, PTE_TRUE));
  224. } /* pthread_cond_broadcast */