rusticl: add a safe abstraction to execute an EventCB
authorLingMan <18294-LingMan@users.noreply.gitlab.freedesktop.org>
Thu, 12 Oct 2023 17:49:59 +0000 (19:49 +0200)
committerMarge Bot <emma+marge@anholt.net>
Sun, 15 Oct 2023 00:17:10 +0000 (00:17 +0000)
Reviewed-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25669>

src/gallium/frontends/rusticl/api/types.rs
src/gallium/frontends/rusticl/core/event.rs

index d117402..0b5177a 100644 (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,
index ecea056..daff5c4 100644 (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);
         }