Skip to content

Commit a58ceb5

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 2dbf189 commit a58ceb5

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
@@ -264,6 +264,20 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
264264
// returned by `platform_get_resource`.
265265
Some(unsafe { Resource::from_raw(resource) })
266266
}
267+
268+
/// excute closure while the device is bound
269+
pub fn while_bound_with<F, U>(&self, f: F) -> Result<U>
270+
where
271+
F: FnOnce(&Device<device::Bound>) -> Result<U>,
272+
{
273+
let _guard = self.as_ref().lock();
274+
if unsafe { !bindings::device_is_bound(self.as_ref().as_raw()) } {
275+
return Err(ENODEV);
276+
}
277+
let ptr: *const Self = self;
278+
let ptr = ptr.cast::<Device<device::Bound>>();
279+
f(unsafe { &*ptr })
280+
}
267281
}
268282

269283
impl Device<Bound> {

0 commit comments

Comments
 (0)