rusticl: use DeleteContextCB
authorLingMan <18294-LingMan@users.noreply.gitlab.freedesktop.org>
Wed, 11 Oct 2023 19:35:23 +0000 (21:35 +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/context.rs
src/gallium/frontends/rusticl/api/types.rs
src/gallium/frontends/rusticl/core/context.rs

index e318644..d04a81f 100644 (file)
@@ -1,7 +1,6 @@
 use crate::api::icd::*;
 use crate::api::types::*;
 use crate::api::util::*;
-use crate::cl_closure;
 use crate::core::context::*;
 use crate::core::device::get_devs_for_type;
 use crate::core::platform::*;
@@ -132,14 +131,10 @@ fn set_context_destructor_callback(
 ) -> CLResult<()> {
     let c = context.get_ref()?;
 
-    // CL_INVALID_VALUE if pfn_notify is NULL.
-    if pfn_notify.is_none() {
-        return Err(CL_INVALID_VALUE);
-    }
+    // SAFETY: The requirements on `DeleteContextCB::new` match the requirements
+    // imposed by the OpenCL specification. It is the caller's duty to uphold them.
+    let cb = unsafe { DeleteContextCB::new(pfn_notify, user_data)? };
 
-    c.dtors
-        .lock()
-        .unwrap()
-        .push(cl_closure!(|c| pfn_notify(c, user_data)));
+    c.dtors.lock().unwrap().push(cb);
     Ok(())
 }
index 3e7c6c8..9f4f0c3 100644 (file)
@@ -45,8 +45,11 @@ macro_rules! cl_callback {
             /// - Passing `data` as the last parameter to `func` must not cause unsoundness.
             /// - CreateContextCB: `func` must be soundly callable as documented on
             ///   [`clCreateContext`] in the OpenCL specification.
+            /// - DeleteContextCB: `func` must be soundly callable as documented on
+            ///   [`clSetContextDestructorCallback`] in the OpenCL specification.
             ///
             /// [`clCreateContext`]: https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#clCreateContext
+            /// [`clSetContextDestructorCallback`]: https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#clSetContextDestructorCallback
             pub unsafe fn new(func: Option<$fn_alias>, data: *mut c_void) -> CLResult<Self> {
                 let Some(func) = func else {
                     return Err(CL_INVALID_VALUE);
index 623ec06..5cc0ec8 100644 (file)
@@ -1,4 +1,5 @@
 use crate::api::icd::*;
+use crate::api::types::DeleteContextCB;
 use crate::core::device::*;
 use crate::core::format::*;
 use crate::core::memory::*;
@@ -22,7 +23,7 @@ pub struct Context {
     pub base: CLObjectBase<CL_INVALID_CONTEXT>,
     pub devs: Vec<&'static Device>,
     pub properties: Properties<cl_context_properties>,
-    pub dtors: Mutex<Vec<Box<dyn Fn(cl_context)>>>,
+    pub dtors: Mutex<Vec<DeleteContextCB>>,
     pub svm_ptrs: Mutex<BTreeMap<*const c_void, Layout>>,
 }
 
@@ -208,6 +209,6 @@ impl Drop for Context {
             .unwrap()
             .iter()
             .rev()
-            .for_each(|cb| cb(cl));
+            .for_each(|cb| unsafe { (cb.func)(cl, cb.data) });
     }
 }