Skip to content

Commit f19037b

Browse files
committed
rust: kernel: platform: Add ::while_bound_with()
Currently unused and unsafe (do not use while the device is already locked). Executes a closure while the devices is guaranteed to be bound. Signed-off-by: Janne Grunau <j@jannau.net>
1 parent d9e5958 commit f19037b

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

rust/kernel/platform.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
231231
// returned by `platform_get_resource`.
232232
Some(unsafe { Resource::as_ref(resource) })
233233
}
234+
235+
/// excute closure while the device is bound
236+
pub fn while_bound_with<F, U>(&self, f: F) -> Result<U>
237+
where
238+
F: FnOnce(&Device<device::Bound>) -> Result<U>,
239+
{
240+
let _guard = self.as_ref().lock();
241+
if unsafe { !bindings::device_is_bound(self.as_ref().as_raw()) } {
242+
return Err(ENODEV);
243+
}
244+
let ptr: *const Self = self;
245+
let ptr = ptr.cast::<Device<device::Bound>>();
246+
f(unsafe { &*ptr })
247+
}
234248
}
235249

236250
impl Device<device::Bound> {

0 commit comments

Comments
 (0)