rusticl/context: implement clSetContextDestructorCallback

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Acked-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15439>
This commit is contained in:
Karol Herbst 2022-03-18 00:41:38 +01:00 committed by Marge Bot
parent 069c122b48
commit e1fefd5372
4 changed files with 53 additions and 1 deletions

View file

@ -6,6 +6,7 @@ use crate::api::icd::*;
use crate::api::platform::*;
use crate::api::types::*;
use crate::api::util::*;
use crate::cl_closure;
use crate::core::context::*;
use self::mesa_rust_util::properties::Properties;
@ -115,3 +116,22 @@ pub fn create_context_from_type(
user_data,
)
}
pub fn set_context_destructor_callback(
context: cl_context,
pfn_notify: ::std::option::Option<DeleteContextCB>,
user_data: *mut ::std::os::raw::c_void,
) -> CLResult<()> {
let c = context.get_ref()?;
// CL_INVALID_VALUE if pfn_notify is NULL.
if pfn_notify.is_none() {
return Err(CL_INVALID_VALUE);
}
c.dtors
.lock()
.unwrap()
.push(cl_closure!(|c| pfn_notify(c, user_data)));
Ok(())
}

View file

@ -170,7 +170,7 @@ pub static DISPATCH: cl_icd_dispatch = cl_icd_dispatch {
clSetProgramSpecializationConstant: None,
clCreateBufferWithProperties: None,
clCreateImageWithProperties: None,
clSetContextDestructorCallback: None,
clSetContextDestructorCallback: Some(cl_set_context_destructor_callback),
};
pub type CLError = cl_int;
@ -1503,6 +1503,16 @@ extern "C" fn cl_create_command_queue_with_properties(
match_obj!(create_command_queue(context, device, 0), errcode_ret)
}
extern "C" fn cl_set_context_destructor_callback(
context: cl_context,
pfn_notify: ::std::option::Option<DeleteContextCB>,
user_data: *mut ::std::os::raw::c_void,
) -> cl_int {
match_err!(set_context_destructor_callback(
context, pfn_notify, user_data
))
}
// cl_khr_icd
extern "C" fn cl_icd_get_platform_ids_khr(
num_entries: cl_uint,

View file

@ -33,6 +33,13 @@ cl_callback!(
}
);
cl_callback!(
DeleteContextCB {
context: cl_context,
user_data: *mut ::std::os::raw::c_void,
}
);
cl_callback!(
EventCB {
event: cl_event,

View file

@ -12,11 +12,13 @@ use std::collections::HashMap;
use std::convert::TryInto;
use std::os::raw::c_void;
use std::sync::Arc;
use std::sync::Mutex;
pub struct Context {
pub base: CLObjectBase<CL_INVALID_CONTEXT>,
pub devs: Vec<Arc<Device>>,
pub properties: Vec<cl_context_properties>,
pub dtors: Mutex<Vec<Box<dyn Fn(cl_context)>>>,
}
impl_cl_type_trait!(cl_context, Context, CL_INVALID_CONTEXT);
@ -27,6 +29,7 @@ impl Context {
base: CLObjectBase::new(),
devs: devs,
properties: properties,
dtors: Mutex::new(Vec::new()),
})
}
@ -60,3 +63,15 @@ impl Context {
Ok(res)
}
}
impl Drop for Context {
fn drop(&mut self) {
let cl = cl_context::from_ptr(self);
self.dtors
.lock()
.unwrap()
.iter()
.rev()
.for_each(|cb| cb(cl));
}
}