From 22808d542bf1fb019c13344b89603afd5ca3865e Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Tue, 4 Apr 2023 02:40:36 +0200 Subject: [PATCH] rusticl/platform: move device initialization to the platform Signed-off-by: Karol Herbst Part-of: --- src/gallium/frontends/rusticl/api/device.rs | 17 +------------- src/gallium/frontends/rusticl/core/platform.rs | 31 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/gallium/frontends/rusticl/api/device.rs b/src/gallium/frontends/rusticl/api/device.rs index 6eaa1b8..4e080d7 100644 --- a/src/gallium/frontends/rusticl/api/device.rs +++ b/src/gallium/frontends/rusticl/api/device.rs @@ -13,7 +13,6 @@ use std::ffi::CStr; use std::mem::size_of; use std::ptr; use std::sync::Arc; -use std::sync::Once; const SPIRV_SUPPORT_STRING: &str = "SPIR-V_1.0 SPIR-V_1.1 SPIR-V_1.2 SPIR-V_1.3 SPIR-V_1.4"; const SPIRV_SUPPORT: [cl_name_version; 5] = [ @@ -196,22 +195,8 @@ impl CLInfo for cl_device_id { } } -// TODO replace with const new container -static mut DEVICES: Vec> = Vec::new(); -static INIT: Once = Once::new(); - -fn load_devices() { - unsafe { - glsl_type_singleton_init_or_ref(); - } - Device::all() - .into_iter() - .for_each(|d| unsafe { DEVICES.push(d) }); -} - fn devs() -> &'static Vec> { - INIT.call_once(load_devices); - unsafe { &DEVICES } + &Platform::get().devs } pub fn get_devs_for_type(device_type: cl_device_type) -> Vec<&'static Arc> { diff --git a/src/gallium/frontends/rusticl/core/platform.rs b/src/gallium/frontends/rusticl/core/platform.rs index 4dc2c64..661d39d 100644 --- a/src/gallium/frontends/rusticl/core/platform.rs +++ b/src/gallium/frontends/rusticl/core/platform.rs @@ -1,21 +1,29 @@ use crate::api::icd::CLResult; use crate::api::icd::DISPATCH; +use crate::core::device::*; use crate::core::version::*; +use mesa_rust_gen::*; use rusticl_opencl_gen::*; +use std::sync::Arc; +use std::sync::Once; + #[repr(C)] pub struct Platform { dispatch: &'static cl_icd_dispatch, pub extensions: [cl_name_version; 2], + pub devs: Vec>, } -static PLATFORM: Platform = Platform { +static PLATFORM_ONCE: Once = Once::new(); +static mut PLATFORM: Platform = Platform { dispatch: &DISPATCH, extensions: [ mk_cl_version_ext(1, 0, 0, "cl_khr_icd"), mk_cl_version_ext(1, 0, 0, "cl_khr_il_program"), ], + devs: Vec::new(), }; impl Platform { @@ -24,7 +32,26 @@ impl Platform { } pub fn get() -> &'static Self { - &PLATFORM + // SAFETY: no concurrent static mut access due to std::Once + PLATFORM_ONCE.call_once(|| unsafe { PLATFORM.init() }); + // SAFETY: no mut references exist at this point + unsafe { &PLATFORM } + } + + fn init(&mut self) { + unsafe { + glsl_type_singleton_init_or_ref(); + } + + self.devs.extend(Device::all()); + } +} + +impl Drop for Platform { + fn drop(&mut self) { + unsafe { + glsl_type_singleton_decref(); + } } } -- 2.7.4