KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
cond.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/cond.h
4 Copyright (C) 2001, 2003 Megan Potter
5
6*/
7
8/** \file kos/cond.h
9 \brief Condition variables.
10 \ingroup kthreads
11
12 This file contains the definition of a Condition Variable. Condition
13 Variables (or condvars for short) are used with a mutex to act as a lock and
14 checkpoint pair for threads.
15
16 Basically, things work as follows (for the thread doing work):
17 \li The associated mutex is locked.
18 \li A predicate is checked to see if it is safe to do something.
19 \li If it is not safe, you call cond_wait(), which releases the mutex.
20 \li When cond_wait() returns, the mutex is reacquired, and work can go on.
21 \li Update any predicates so that we know that the work is done, and unlock
22 the mutex.
23
24 Meanwhile, the thread updating the condition works as follows:
25 \li Lock the mutex associated with the condvar.
26 \li Produce work to be done.
27 \li Call cond_signal() (with the associated mutex still locked), so that any
28 threads waiting on the condvar will know they can continue on when the
29 mutex is released, also update any predicates that say whether work can
30 be done.
31 \li Unlock the mutex so that worker threads can acquire the mutex and do
32 whatever work needs to be done.
33
34 Condition variables can be quite useful when used properly, and provide a
35 fairly easy way to wait for work to be ready to be done.
36
37 Condition variables should never be used with mutexes that are of the type
38 MUTEX_TYPE_RECURSIVE. The lock will only be released once by the wait
39 function, and thus you will end up deadlocking if you use a recursive mutex
40 that has been locked more than once.
41
42 \author Megan Potter
43*/
44
45#ifndef __KOS_COND_H
46#define __KOS_COND_H
47
48#include <kos/cdefs.h>
49__BEGIN_DECLS
50
51#include <kos/thread.h>
52#include <kos/mutex.h>
53
54/** \brief Condition variable.
55
56 There are no public members of this structure for you to actually do
57 anything with in your code, so don't try.
58
59 \headerfile kos/cond.h
60*/
61typedef struct condvar {
62 int dummy;
64} condvar_t;
65
66/** \brief Initializer for a transient condvar. */
67#define COND_INITIALIZER { 0, 0 }
68
69/** \brief Allocate a new condition variable.
70
71 This function allocates and initializes a new condition variable for use.
72
73 \deprecated
74 This function is formally deprecated and should not be used in new code.
75 Instead you should use either the static initializer or the cond_init()
76 function.
77
78 \return The created condvar on success. NULL is returned on
79 failure and errno is set as appropriate.
80
81 \par Error Conditions:
82 \em ENOMEM - out of memory
83*/
84condvar_t *cond_create(void) __depr("Use cond_init or COND_INITIALIZER.");
85
86/** \brief Initialize a condition variable.
87
88 This function initializes a new condition variable for use.
89
90 \param cv The condition variable to initialize
91 \retval 0 On success
92 \retval -1 On error, sets errno as appropriate
93*/
95
96/** \brief Free a condition variable.
97
98 This function frees a condition variable, releasing all memory associated
99 with it (but not with the mutex that is associated with it). This will also
100 wake all threads waiting on the condition.
101
102 \retval 0 On success (no error conditions currently defined)
103*/
105
106/** \brief Wait on a condition variable.
107
108 This function will wait on the condition variable, unlocking the mutex and
109 putting the calling thread to sleep as one atomic operation. The wait in
110 this function has no timeout, and will sleep forever if the condition is not
111 signalled.
112
113 The mutex will be locked and owned by the calling thread on return,
114 regardless of whether it is a successful or error return.
115
116 \param cv The condition to wait on
117 \param m The associated mutex
118 \retval 0 On success
119 \retval -1 On error, sets errno as appropriate
120
121 \par Error Conditions:
122 \em EPERM - called inside an interrupt \n
123 \em EINVAL - the condvar was not initialized \n
124 \em EINVAL - the mutex is not initialized or not locked \n
125 \em ENOTRECOVERABLE - the condvar was destroyed while waiting
126*/
128
129/** \brief Wait on a condition variable with a timeout.
130
131 This function will wait on the condition variable, unlocking the mutex and
132 putting the calling thread to sleep as one atomic operation. If the timeout
133 elapses before the condition is signalled, this function will return error.
134 If a timeout of 0 is given, the call is equivalent to cond_wait() (there is
135 no timeout).
136
137 The mutex will be locked and owned by the calling thread on return,
138 regardless of whether it is a successful or error return.
139
140 \param cv The condition to wait on
141 \param m The associated mutex
142 \param timeout The number of milliseconds before timeout
143 \retval 0 On success
144 \retval -1 On error, sets errno as appropriate
145
146 \par Error Conditions:
147 \em EPERM - called inside an interrupt \n
148 \em ETIMEDOUT - timed out \n
149 \em EINVAL - the condvar was not initialized \n
150 \em EINVAL - the mutex is not initialized or not locked \n
151 \em ENOTRECOVERABLE - the condvar was destroyed while waiting
152*/
153int cond_wait_timed(condvar_t *cv, mutex_t * m, int timeout);
154
155/** \brief Signal a single thread waiting on the condition variable.
156
157 This function will wake up a single thread that is waiting on the condition.
158 The calling thread should be holding the associated mutex or recursive lock
159 before calling this to guarantee sane behavior.
160
161 \param cv The condition to signal
162 \retval 0 On success
163 \retval -1 On error, errno will be set as appropriate
164
165 \par Error Conditions:
166 \em EINVAL - the condvar was not initialized
167*/
169
170/** \brief Signal all threads waiting on the condition variable.
171
172 This function will wake up all threads that are waiting on the condition.
173 The calling thread should be holding the associated mutex or recursive lock
174 before calling this to guarantee sane behavior.
175
176 \param cv The condition to signal
177 \retval 0 On success
178 \retval -1 On error, errno will be set as appropriate
179
180 \par Error Conditions:
181 \em EINVAL - the condvar was not initialized
182*/
184
185__END_DECLS
186
187#endif /* __KOS_COND_H */
Various common macros used throughout the codebase.
int cond_signal(condvar_t *cv)
Signal a single thread waiting on the condition variable.
int cond_destroy(condvar_t *cv)
Free a condition variable.
int cond_wait_timed(condvar_t *cv, mutex_t *m, int timeout)
Wait on a condition variable with a timeout.
condvar_t * cond_create(void) 1("Use cond_init or COND_INITIALIZER.")
Allocate a new condition variable.
int cond_wait(condvar_t *cv, mutex_t *m)
Wait on a condition variable.
int cond_broadcast(condvar_t *cv)
Signal all threads waiting on the condition variable.
int cond_init(condvar_t *cv)
Initialize a condition variable.
Mutual exclusion locks.
Condition variable.
Definition cond.h:61
int dynamic
Definition cond.h:63
int dummy
Definition cond.h:62
Mutual exclusion lock type.
Definition mutex.h:68
Threading support.