rusticl: add a safe abstraction to execute an EventCB

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25669>
This commit is contained in:
LingMan 2023-10-12 19:49:59 +02:00 committed by Marge Bot
parent d9e2463ef3
commit 241d16c9e8
2 changed files with 13 additions and 4 deletions

View file

@ -1,6 +1,7 @@
use crate::api::icd::CLResult;
use crate::api::icd::ReferenceCountedAPIPointer;
use crate::core::context::Context;
use crate::core::event::Event;
use rusticl_opencl_gen::*;
@ -129,6 +130,15 @@ cl_callback!(
}
);
impl EventCB {
pub fn call(self, event: &Event, status: cl_int) {
let cl = cl_event::from_ptr(event);
// SAFETY: `cl` must be a valid pointer to an OpenCL event, which is where we just got it from.
// All other requirements are covered by this callback's type invariants.
unsafe { (self.func)(cl, status, self.data) };
}
}
cl_callback!(
MemCB(FuncMemCB) {
memobj: cl_mem,

View file

@ -119,9 +119,8 @@ impl Event {
}
if [CL_COMPLETE, CL_RUNNING, CL_SUBMITTED].contains(&(new as u32)) {
if let Some(cbs) = lock.cbs.get(new as usize) {
cbs.iter()
.for_each(|cb| unsafe { (cb.func)(cl_event::from_ptr(self), new, cb.data) });
if let Some(cbs) = lock.cbs.get_mut(new as usize) {
cbs.drain(..).for_each(|cb| cb.call(self, new));
}
}
}
@ -167,7 +166,7 @@ impl Event {
// call cb if the status was already reached
if state >= status {
drop(lock);
unsafe { (cb.func)(cl_event::from_ptr(self), status, cb.data) };
cb.call(self, state);
} else {
lock.cbs.get_mut(state as usize).unwrap().push(cb);
}