Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / libs / thread / broadcastcondition.c
blobf93dac48b51d389c5037ddaf950380daa75c7871
1 /*
2 * thread.library - threading and synchronisation primitives
4 * Copyright © 2007 Robert Norris
6 * This program is free software; you can redistribute it and/or modify it
7 * under the same terms as AROS itself.
8 */
10 #include "thread_intern.h"
12 #include <exec/tasks.h>
13 #include <exec/lists.h>
14 #include <proto/exec.h>
15 #include <assert.h>
17 /*****************************************************************************
19 NAME */
20 AROS_LH1(void, BroadcastCondition,
22 /* SYNOPSIS */
23 AROS_LHA(void *, cond, A0),
25 /* LOCATION */
26 struct ThreadBase *, ThreadBase, 19, Thread)
28 /* FUNCTION
29 Signals all threads waiting on a condition variable.
31 INPUTS
32 cond - the condition to signal.
34 RESULT
35 This function always succeeds.
37 NOTES
38 Before calling this function you should lock the mutex that protects
39 the condition. WaitCondition() atomically unlocks the mutex and waits
40 on the condition, so by locking the mutex first before sending the
41 signal, you ensure that the signal cannot be missed. See
42 WaitCondition() for more details.
44 If no threads are waiting on the condition, nothing happens.
46 EXAMPLE
47 LockMutex(mutex);
48 BroadcastCondition(cond);
49 UnlockMutex(mutex);
51 BUGS
53 SEE ALSO
54 CreateCondition(), DestroyCondition(), WaitCondition(),
55 SignalCondition()
57 INTERNALS
58 SIGF_SIGNAL is used to signal the selected waiting thread.
60 *****************************************************************************/
62 AROS_LIBFUNC_INIT
64 struct _Condition *c = (struct _Condition *) cond;
65 struct _CondWaiter *waiter;
67 assert(c != NULL);
69 /* safely operation on the condition */
70 ObtainSemaphore(&c->lock);
72 /* wake up all the waiters */
73 while ((waiter = (struct _CondWaiter *) REMHEAD(&c->waiters)) != NULL) {
74 Signal(waiter->task, SIGF_SINGLE);
75 FreeMem(waiter, sizeof(struct _CondWaiter));
78 /* none left */
79 c->count = 0;
81 ReleaseSemaphore(&c->lock);
83 AROS_LIBFUNC_EXIT
84 } /* BroadcastCondition */