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;
63} condvar_t;
64
65/** \brief Initializer for a transient condvar. */
66#define COND_INITIALIZER { 0 }
67
68/** \brief Initialize a condition variable.
69
70 This function initializes a new condition variable for use.
71
72 \param cv The condition variable to initialize
73 \retval 0 On success (no error conditions currently defined)
74*/
75int cond_init(condvar_t *cv) __nonnull_all;
76
77/** \brief Free a condition variable.
78
79 This function destroys a condition variable (but not the mutex
80 that is associated with it). This will also wake all threads waiting
81 on the condition.
82
83 \param cv The condition variable to destroy
84 \retval 0 On success (no error conditions currently defined)
85*/
86int cond_destroy(condvar_t *cv) __nonnull_all;
87
88/** \brief Wait on a condition variable.
89
90 This function will wait on the condition variable, unlocking the mutex and
91 putting the calling thread to sleep as one atomic operation. The wait in
92 this function has no timeout, and will sleep forever if the condition is not
93 signalled.
94
95 The mutex will be locked and owned by the calling thread on return,
96 regardless of whether it is a successful or error return.
97
98 \param cv The condition to wait on
99 \param m The associated mutex
100 \retval 0 On success
101 \retval -1 On error, sets errno as appropriate
102
103 \par Error Conditions:
104 \em EPERM - called inside an interrupt \n
105 \em EINVAL - the condvar was not initialized \n
106 \em EINVAL - the mutex is not initialized or not locked \n
107 \em ENOTRECOVERABLE - the condvar was destroyed while waiting
108*/
109int cond_wait(condvar_t *cv, mutex_t * m) __nonnull_all;
110
111/** \brief Wait on a condition variable with a timeout.
112
113 This function will wait on the condition variable, unlocking the mutex and
114 putting the calling thread to sleep as one atomic operation. If the timeout
115 elapses before the condition is signalled, this function will return error.
116 If a timeout of 0 is given, the call is equivalent to cond_wait() (there is
117 no timeout).
118
119 The mutex will be locked and owned by the calling thread on return,
120 regardless of whether it is a successful or error return.
121
122 \param cv The condition to wait on
123 \param m The associated mutex
124 \param timeout The number of milliseconds before timeout
125 \retval 0 On success
126 \retval -1 On error, sets errno as appropriate
127
128 \par Error Conditions:
129 \em EPERM - called inside an interrupt \n
130 \em ETIMEDOUT - timed out \n
131 \em EINVAL - the condvar was not initialized \n
132 \em EINVAL - the mutex is not initialized or not locked \n
133 \em ENOTRECOVERABLE - the condvar was destroyed while waiting
134*/
135int cond_wait_timed(condvar_t *cv, mutex_t * m, int timeout) __nonnull_all;
136
137/** \brief Signal a single thread waiting on the condition variable.
138
139 This function will wake up a single thread that is waiting on the condition.
140 The calling thread should be holding the associated mutex or recursive lock
141 before calling this to guarantee sane behavior.
142
143 \param cv The condition to signal
144 \retval 0 On success
145 \retval -1 On error, errno will be set as appropriate
146
147 \par Error Conditions:
148 \em EINVAL - the condvar was not initialized
149*/
150int cond_signal(condvar_t *cv) __nonnull_all;
151
152/** \brief Signal all threads waiting on the condition variable.
153
154 This function will wake up all threads that are waiting on the condition.
155 The calling thread should be holding the associated mutex or recursive lock
156 before calling this to guarantee sane behavior.
157
158 \param cv The condition to signal
159 \retval 0 On success
160 \retval -1 On error, errno will be set as appropriate
161
162 \par Error Conditions:
163 \em EINVAL - the condvar was not initialized
164*/
165int cond_broadcast(condvar_t *cv) __nonnull_all;
166
167__END_DECLS
168
169#endif /* __KOS_COND_H */
Various common macros used throughout the codebase.
int cond_destroy(condvar_t *cv) __nonnull_all
Free a condition variable.
int cond_wait(condvar_t *cv, mutex_t *m) __nonnull_all
Wait on a condition variable.
int cond_signal(condvar_t *cv) __nonnull_all
Signal a single thread waiting on the condition variable.
int cond_init(condvar_t *cv) __nonnull_all
Initialize a condition variable.
int cond_broadcast(condvar_t *cv) __nonnull_all
Signal all threads waiting on the condition variable.
int cond_wait_timed(condvar_t *cv, mutex_t *m, int timeout) __nonnull_all
Wait on a condition variable with a timeout.
Mutual exclusion locks.
Condition variable.
Definition cond.h:61
int dummy
Definition cond.h:62
Mutual exclusion lock type.
Definition mutex.h:68
Threading support.