2 * pthread_cond_signal.c
5 * This translation unit implements condition variables and their primitives.
8 * --------------------------------------------------------------------------
10 * Pthreads-win32 - POSIX Threads Library for Win32
11 * Copyright(C) 1998 John E. Bossom
12 * Copyright(C) 1999,2005 Pthreads-win32 contributors
14 * Contact Email: rpj@callisto.canberra.edu.au
16 * The current list of contributors is contained
17 * in the file CONTRIBUTORS included with the source
18 * code distribution. The list can also be seen at the
19 * following World Wide Web location:
20 * http://sources.redhat.com/pthreads-win32/contributors.html
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Lesser General Public License for more details.
32 * You should have received a copy of the GNU Lesser General Public
33 * License along with this library in the file COPYING.LIB;
34 * if not, write to the Free Software Foundation, Inc.,
35 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
37 * -------------------------------------------------------------
39 * See the comments at the top of pthread_cond_wait.c.
43 #include "implement.h"
46 ptw32_cond_unblock (pthread_cond_t
* cond
, int unblockAll
)
50 * Does not use the external mutex for synchronisation,
51 * therefore semBlockLock is needed.
52 * mtxUnblockLock is for LEVEL-2 synch. LEVEL-2 is the
53 * state where the external mutex is not necessarily locked by
54 * any thread, ie. between cond_wait unlocking and re-acquiring
55 * the lock after having been signaled or a timeout or
58 * Uses the following CV elements:
71 if (cond
== NULL
|| *cond
== NULL
)
79 * No-op if the CV is static and hasn't been initialised yet.
80 * Assuming that any race condition is harmless.
82 if (cv
== PTHREAD_COND_INITIALIZER
)
87 if ((result
= pthread_mutex_lock (&(cv
->mtxUnblockLock
))) != 0)
92 if (0 != cv
->nWaitersToUnblock
)
94 if (0 == cv
->nWaitersBlocked
)
96 return pthread_mutex_unlock (&(cv
->mtxUnblockLock
));
100 cv
->nWaitersToUnblock
+= (nSignalsToIssue
= cv
->nWaitersBlocked
);
101 cv
->nWaitersBlocked
= 0;
106 cv
->nWaitersToUnblock
++;
107 cv
->nWaitersBlocked
--;
110 else if (cv
->nWaitersBlocked
> cv
->nWaitersGone
)
112 /* Use the non-cancellable version of sem_wait() */
113 if (ptw32_semwait (&(cv
->semBlockLock
)) != 0)
116 (void) pthread_mutex_unlock (&(cv
->mtxUnblockLock
));
119 if (0 != cv
->nWaitersGone
)
121 cv
->nWaitersBlocked
-= cv
->nWaitersGone
;
122 cv
->nWaitersGone
= 0;
126 nSignalsToIssue
= cv
->nWaitersToUnblock
= cv
->nWaitersBlocked
;
127 cv
->nWaitersBlocked
= 0;
131 nSignalsToIssue
= cv
->nWaitersToUnblock
= 1;
132 cv
->nWaitersBlocked
--;
137 return pthread_mutex_unlock (&(cv
->mtxUnblockLock
));
140 if ((result
= pthread_mutex_unlock (&(cv
->mtxUnblockLock
))) == 0)
142 if (sem_post_multiple (&(cv
->semBlockQueue
), nSignalsToIssue
) != 0)
150 } /* ptw32_cond_unblock */
153 pthread_cond_signal (pthread_cond_t
* cond
)
155 * ------------------------------------------------------
157 * This function signals a condition variable, waking
158 * one waiting thread.
159 * If SCHED_FIFO or SCHED_RR policy threads are waiting
160 * the highest priority waiter is awakened; otherwise,
161 * an unspecified waiter is awakened.
165 * pointer to an instance of pthread_cond_t
169 * This function signals a condition variable, waking
170 * one waiting thread.
171 * If SCHED_FIFO or SCHED_RR policy threads are waiting
172 * the highest priority waiter is awakened; otherwise,
173 * an unspecified waiter is awakened.
177 * 1) Use when any waiter can respond and only one need
178 * respond (all waiters being equal).
181 * 0 successfully signaled condition,
182 * EINVAL 'cond' is invalid,
184 * ------------------------------------------------------
188 * The '0'(FALSE) unblockAll arg means unblock ONE waiter.
190 return (ptw32_cond_unblock (cond
, 0));
192 } /* pthread_cond_signal */
195 pthread_cond_broadcast (pthread_cond_t
* cond
)
197 * ------------------------------------------------------
199 * This function broadcasts the condition variable,
200 * waking all current waiters.
204 * pointer to an instance of pthread_cond_t
208 * This function signals a condition variable, waking
209 * all waiting threads.
213 * 1) Use when more than one waiter may respond to
214 * predicate change or if any waiting thread may
215 * not be able to respond
218 * 0 successfully signalled condition to all
220 * EINVAL 'cond' is invalid
221 * ENOSPC a required resource has been exhausted,
223 * ------------------------------------------------------
227 * The TRUE unblockAll arg means unblock ALL waiters.
229 return (ptw32_cond_unblock (cond
, PTW32_TRUE
));
231 } /* pthread_cond_broadcast */