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