@@ -34,6 +34,11 @@ use core::fmt;
34
34
35
35
use crate :: error:: { Result , to_result_void} ;
36
36
use crate :: raw:: {
37
+ k_condvar,
38
+ k_condvar_init,
39
+ k_condvar_broadcast,
40
+ k_condvar_signal,
41
+ k_condvar_wait,
37
42
k_mutex,
38
43
k_mutex_init,
39
44
k_mutex_lock,
@@ -46,6 +51,7 @@ use crate::object::{
46
51
use crate :: time:: {
47
52
Timeout ,
48
53
} ;
54
+ use super :: K_FOREVER ;
49
55
50
56
/// A Zephyr `k_mutux` usable from safe Rust code.
51
57
///
@@ -122,3 +128,59 @@ impl StaticMutex {
122
128
} )
123
129
}
124
130
}
131
+
132
+ /// A Condition Variable
133
+ ///
134
+ /// Lightweight wrappers for Zephyr's `k_condvar`.
135
+ #[ derive( Clone ) ]
136
+ pub struct Condvar {
137
+ pub item : * mut k_condvar ,
138
+ }
139
+
140
+ unsafe impl Sync for StaticKernelObject < k_condvar > { }
141
+
142
+ unsafe impl Sync for Condvar { }
143
+ unsafe impl Send for Condvar { }
144
+
145
+ impl KobjInit < k_condvar , Condvar > for StaticKernelObject < k_condvar > {
146
+ fn wrap ( ptr : * mut k_condvar ) -> Condvar {
147
+ Condvar { item : ptr }
148
+ }
149
+ }
150
+
151
+ pub type StaticCondvar = StaticKernelObject < k_condvar > ;
152
+
153
+ impl StaticCondvar {
154
+ pub fn init ( & self ) {
155
+ self . init_help ( |raw| {
156
+ unsafe {
157
+ k_condvar_init ( raw) ;
158
+ }
159
+ } )
160
+ }
161
+ }
162
+
163
+ impl Condvar {
164
+ /// Wait for someone else using this mutex/condvar pair to notify. Note that this requires the
165
+ /// lock to be held by use, but as this is a low-level binding to Zephyr's interfaces, this is
166
+ /// not enforced. See [`sync::Condvar`] for a safer and easier to use interface.
167
+ pub fn wait ( & self , lock : & Mutex ) {
168
+ unsafe { k_condvar_wait ( self . item , lock. item , K_FOREVER ) ; }
169
+ }
170
+
171
+ // TODO: timeout.
172
+
173
+ pub fn notify_one ( & self ) {
174
+ unsafe { k_condvar_signal ( self . item ) ; }
175
+ }
176
+
177
+ pub fn notify_all ( & self ) {
178
+ unsafe { k_condvar_broadcast ( self . item ) ; }
179
+ }
180
+ }
181
+
182
+ impl fmt:: Debug for Condvar {
183
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
184
+ write ! ( f, "sys::Condvar {:?}" , self . item)
185
+ }
186
+ }
0 commit comments