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 <arch/types.h>
52#include <kos/thread.h>
53#include <kos/mutex.h>
54
55/** \brief Condition variable.
56
57 There are no public members of this structure for you to actually do
58 anything with in your code, so don't try.
59
60 \headerfile kos/cond.h
61*/
62typedef struct condvar {
63 int dummy;
65} condvar_t;
66
67/** \brief Initializer for a transient condvar. */
68#define COND_INITIALIZER { 0, 0 }
69
70/** \brief Allocate a new condition variable.
71
72 This function allocates and initializes a new condition variable for use.
73
74 \deprecated
75 This function is formally deprecated and should not be used in new code.
76 Instead you should use either the static initializer or the cond_init()
77 function.
78
79 \return The created condvar on success. NULL is returned on
80 failure and errno is set as appropriate.
81
82 \par Error Conditions:
83 \em ENOMEM - out of memory
84*/
86
87/** \brief Initialize a condition variable.
88
89 This function initializes a new condition variable for use.
90
91 \param cv The condition variable to initialize
92 \retval 0 On success
93 \retval -1 On error, sets errno as appropriate
94*/
96
97/** \brief Free a condition variable.
98
99 This function frees a condition variable, releasing all memory associated
100 with it (but not with the mutex that is associated with it). This will also
101 wake all threads waiting on the condition.
102
103 \retval 0 On success (no error conditions currently defined)
104*/
106
107/** \brief Wait on a condition variable.
108
109 This function will wait on the condition variable, unlocking the mutex and
110 putting the calling thread to sleep as one atomic operation. The wait in
111 this function has no timeout, and will sleep forever if the condition is not
112 signalled.
113
114 The mutex will be locked and owned by the calling thread on return,
115 regardless of whether it is a successful or error return.
116
117 \param cv The condition to wait on
118 \param m The associated mutex
119 \retval 0 On success
120 \retval -1 On error, sets errno as appropriate
121
122 \par Error Conditions:
123 \em EPERM - called inside an interrupt \n
124 \em EINVAL - the condvar was not initialized \n
125 \em EINVAL - the mutex is not initialized or not locked \n
126 \em ENOTRECOVERABLE - the condvar was destroyed while waiting
127*/
129
130/** \brief Wait on a condition variable with a timeout.
131
132 This function will wait on the condition variable, unlocking the mutex and
133 putting the calling thread to sleep as one atomic operation. If the timeout
134 elapses before the condition is signalled, this function will return error.
135 If a timeout of 0 is given, the call is equivalent to cond_wait() (there is
136 no timeout).
137
138 The mutex will be locked and owned by the calling thread on return,
139 regardless of whether it is a successful or error return.
140
141 \param cv The condition to wait on
142 \param m The associated mutex
143 \param timeout The number of milliseconds before timeout
144 \retval 0 On success
145 \retval -1 On error, sets errno as appropriate
146
147 \par Error Conditions:
148 \em EPERM - called inside an interrupt \n
149 \em ETIMEDOUT - timed out \n
150 \em EINVAL - the condvar was not initialized \n
151 \em EINVAL - the mutex is not initialized or not locked \n
152 \em ENOTRECOVERABLE - the condvar was destroyed while waiting
153*/
154int cond_wait_timed(condvar_t *cv, mutex_t * m, int timeout);
155
156/** \brief Signal a single thread waiting on the condition variable.
157
158 This function will wake up a single thread that is waiting on the condition.
159 The calling thread should be holding the associated mutex or recursive lock
160 before calling this to guarantee sane behavior.
161
162 \param cv The condition to signal
163 \retval 0 On success
164 \retval -1 On error, errno will be set as appropriate
165
166 \par Error Conditions:
167 \em EINVAL - the condvar was not initialized
168*/
170
171/** \brief Signal all threads waiting on the condition variable.
172
173 This function will wake up all threads that are waiting on the condition.
174 The calling thread should be holding the associated mutex or recursive lock
175 before calling this to guarantee sane behavior.
176
177 \param cv The condition to signal
178 \retval 0 On success
179 \retval -1 On error, errno will be set as appropriate
180
181 \par Error Conditions:
182 \em EINVAL - the condvar was not initialized
183*/
185
186__END_DECLS
187
188#endif /* __KOS_COND_H */
Definitions for builtin attributes and compiler directives.
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.
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.
condvar_t * cond_create() __depr("Use cond_init or COND_INITIALIZER.")
Allocate a new condition variable.
int cond_init(condvar_t *cv)
Initialize a condition variable.
#define COND_INITIALIZER
Initializer for a transient condvar.
Definition cond.h:68
#define __depr(m)
Mark something as deprecated, with an informative message.
Definition cdefs.h:119
Mutual exclusion locks.
Condition variable.
Definition cond.h:62
int dynamic
Definition cond.h:64
int dummy
Definition cond.h:63
Mutual exclusion lock type.
Definition mutex.h:68
Threading support.
Common integer types.