Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / libs / thread / signalcondition.c
blob39754aef762e04d1b4cdcd898445318858af4e51
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, SignalCondition,
22 /* SYNOPSIS */
23 AROS_LHA(void *, cond, A0),
25 /* LOCATION */
26 struct ThreadBase *, ThreadBase, 18, Thread)
28 /* FUNCTION
29 Signals a thread waiting on 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. If more
45 than one thread is waiting, only one will be signalled. Which one is
46 undefined.
48 EXAMPLE
49 LockMutex(mutex);
50 SignalCondition(cond);
51 UnlockMutex(mutex);
53 BUGS
55 SEE ALSO
56 CreateCondition(), DestroyCondition(), WaitCondition(),
57 BroadcastCondition()
59 INTERNALS
60 SIGF_SIGNAL is used to signal the selected waiting thread.
62 *****************************************************************************/
64 AROS_LIBFUNC_INIT
66 struct _Condition *c = (struct _Condition *) cond;
67 struct _CondWaiter *waiter;
69 assert(c != NULL);
71 /* safely remove a waiter from the list */
72 ObtainSemaphore(&c->lock);
73 waiter = (struct _CondWaiter *) REMHEAD(&c->waiters);
74 if (waiter != NULL)
75 c->count--;
76 ReleaseSemaphore(&c->lock);
78 /* noone waiting */
79 if (waiter == NULL)
80 return;
82 /* signal the task */
83 Signal(waiter->task, SIGF_SINGLE);
85 /* all done */
86 FreeMem(waiter, sizeof(struct _CondWaiter));
88 AROS_LIBFUNC_EXIT
89 } /* SignalCondition */