Skip to content

Commit e3a258d

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 a274a5c commit e3a258d

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
@@ -277,6 +277,20 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
277277
// returned by `platform_get_resource`.
278278
Some(unsafe { Resource::from_raw(resource) })
279279
}
280+
281+
/// excute closure while the device is bound
282+
pub fn while_bound_with<F, U>(&self, f: F) -> Result<U>
283+
where
284+
F: FnOnce(&Device<device::Bound>) -> Result<U>,
285+
{
286+
let _guard = self.as_ref().lock();
287+
if unsafe { !bindings::device_is_bound(self.as_ref().as_raw()) } {
288+
return Err(ENODEV);
289+
}
290+
let ptr: *const Self = self;
291+
let ptr = ptr.cast::<Device<device::Bound>>();
292+
f(unsafe { &*ptr })
293+
}
280294
}
281295

282296
impl Device<Bound> {

0 commit comments

Comments
 (0)