blob: bdb6ec28e2f73d5bc7288002f7d4ee5d5d5451ef [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
Travis Geiselbrecht671cb792009-06-28 11:27:48 -07002 * Copyright (c) 2008-2009 Travis Geiselbrecht
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
Travis Geiselbrecht12403e32010-05-06 13:36:57 -070023
24/**
25 * @file
26 * @brief Event wait and signal functions for threads.
27 * @defgroup event Events
28 *
29 * An event is a subclass of a wait queue.
30 *
31 * Threads wait for events, with optional timeouts.
32 *
33 * Events are "signaled", releasing waiting threads to continue.
34 * Signals may be one-shot signals (EVENT_FLAG_AUTOUNSIGNAL), in which
35 * case one signal releases only one thread, at which point it is
36 * automatically cleared. Otherwise, signals release all waiting threads
37 * to continue immediately until the signal is manually cleared with
38 * event_unsignal().
39 *
40 * @{
41 */
42
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070043#include <debug.h>
44#include <err.h>
45#include <kernel/event.h>
46
47#if DEBUGLEVEL > 1
48#define EVENT_CHECK 1
49#endif
50
Travis Geiselbrecht12403e32010-05-06 13:36:57 -070051/**
52 * @brief Initialize an event object
53 *
54 * @param e Event object to initialize
55 * @param initial Initial value for "signaled" state
56 * @param flags 0 or EVENT_FLAG_AUTOUNSIGNAL
57 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070058void event_init(event_t *e, bool initial, uint flags)
59{
60#if EVENT_CHECK
61// ASSERT(e->magic != EVENT_MAGIC);
62#endif
63
64 e->magic = EVENT_MAGIC;
65 e->signalled = initial;
66 e->flags = flags;
67 wait_queue_init(&e->wait);
68}
69
Travis Geiselbrecht12403e32010-05-06 13:36:57 -070070/**
71 * @brief Destroy an event object.
72 *
73 * Event's resources are freed and it may no longer be
74 * used until event_init() is called again. Any threads
75 * still waiting on the event will be resumed.
76 *
77 * @param e Event object to initialize
78 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070079void event_destroy(event_t *e)
80{
81 enter_critical_section();
82
83#if EVENT_CHECK
84 ASSERT(e->magic == EVENT_MAGIC);
85#endif
86
87 e->magic = 0;
88 e->signalled = false;
89 e->flags = 0;
90 wait_queue_destroy(&e->wait, true);
91
92 exit_critical_section();
93}
94
Travis Geiselbrecht12403e32010-05-06 13:36:57 -070095/**
96 * @brief Wait for event to be signaled
97 *
98 * If the event has already been signaled, this function
99 * returns immediately. Otherwise, the current thread
100 * goes to sleep until the event object is signaled,
101 * the timeout is reached, or the event object is destroyed
102 * by another thread.
103 *
104 * @param e Event object
105 * @param timeout Timeout value, in ms
106 *
107 * @return 0 on success, ERR_TIMED_OUT on timeout,
108 * other values on other errors.
109 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700110status_t event_wait_timeout(event_t *e, time_t timeout)
111{
112 status_t ret = NO_ERROR;
113
114 enter_critical_section();
115
116#if EVENT_CHECK
117 ASSERT(e->magic == EVENT_MAGIC);
118#endif
119
120 if (e->signalled) {
121 /* signalled, we're going to fall through */
122 if (e->flags & EVENT_FLAG_AUTOUNSIGNAL) {
123 /* autounsignal flag lets one thread fall through before unsignalling */
124 e->signalled = false;
125 }
126 } else {
127 /* unsignalled, block here */
128 ret = wait_queue_block(&e->wait, timeout);
129 if (ret < 0)
130 goto err;
131 }
132
133err:
134 exit_critical_section();
135
136 return ret;
137}
138
Travis Geiselbrecht12403e32010-05-06 13:36:57 -0700139/**
140 * @brief Same as event_wait_timeout(), but without a timeout.
141 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700142status_t event_wait(event_t *e)
143{
144 return event_wait_timeout(e, INFINITE_TIME);
145}
146
Travis Geiselbrecht12403e32010-05-06 13:36:57 -0700147/**
148 * @brief Signal an event
149 *
150 * Signals an event. If EVENT_FLAG_AUTOUNSIGNAL is set in the event
151 * object's flags, only one waiting thread is allowed to proceed. Otherwise,
152 * all waiting threads are allowed to proceed until such time as
153 * event_unsignal() is called.
154 *
155 * @param e Event object
156 * @param reschedule If true, waiting thread(s) are executed immediately,
157 * and the current thread resumes only after the
158 * waiting threads have been satisfied. If false,
159 * waiting threads are placed at the end of the run
160 * queue.
161 *
162 * @return Returns NO_ERROR on success.
163 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700164status_t event_signal(event_t *e, bool reschedule)
165{
166 enter_critical_section();
167
168#if EVENT_CHECK
169 ASSERT(e->magic == EVENT_MAGIC);
170#endif
171
172 if (!e->signalled) {
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700173 if (e->flags & EVENT_FLAG_AUTOUNSIGNAL) {
Travis Geiselbrechtbb777a12009-01-20 00:24:27 -0800174 /* try to release one thread and leave unsignalled if successful */
175 if (wait_queue_wake_one(&e->wait, reschedule, NO_ERROR) <= 0) {
176 /*
177 * if we didn't actually find a thread to wake up, go to
178 * signalled state and let the next call to event_wait
179 * unsignal the event.
180 */
181 e->signalled = true;
182 }
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700183 } else {
travis geiselbrechtd98cea12008-09-18 16:01:03 +0000184 /* release all threads and remain signalled */
Travis Geiselbrechtbb777a12009-01-20 00:24:27 -0800185 e->signalled = true;
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700186 wait_queue_wake_all(&e->wait, reschedule, NO_ERROR);
187 }
188 }
189
190 exit_critical_section();
191
192 return NO_ERROR;
193}
194
Travis Geiselbrecht12403e32010-05-06 13:36:57 -0700195/**
196 * @brief Clear the "signaled" property of an event
197 *
198 * Used mainly for event objects without the EVENT_FLAG_AUTOUNSIGNAL
199 * flag. Once this function is called, threads that call event_wait()
200 * functions will once again need to wait until the event object
201 * is signaled.
202 *
203 * @param e Event object
204 *
205 * @return Returns NO_ERROR on success.
206 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700207status_t event_unsignal(event_t *e)
208{
209 enter_critical_section();
210
211#if EVENT_CHECK
212 ASSERT(e->magic == EVENT_MAGIC);
213#endif
214
215 e->signalled = false;
216
217 exit_critical_section();
218
219 return NO_ERROR;
220}
221