rusticl/platform: move device initialization to the platform
authorKarol Herbst <kherbst@redhat.com>
Tue, 4 Apr 2023 00:40:36 +0000 (02:40 +0200)
committerMarge Bot <emma+marge@anholt.net>
Thu, 13 Apr 2023 02:54:21 +0000 (02:54 +0000)
Signed-off-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22280>

src/gallium/frontends/rusticl/api/device.rs
src/gallium/frontends/rusticl/core/platform.rs

index 6eaa1b8..4e080d7 100644 (file)
@@ -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<cl_device_info> for cl_device_id {
     }
 }
 
-// TODO replace with const new container
-static mut DEVICES: Vec<Arc<Device>> = 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<Arc<Device>> {
-    INIT.call_once(load_devices);
-    unsafe { &DEVICES }
+    &Platform::get().devs
 }
 
 pub fn get_devs_for_type(device_type: cl_device_type) -> Vec<&'static Arc<Device>> {
index 4dc2c64..661d39d 100644 (file)
@@ -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<Arc<Device>>,
 }
 
-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();
+        }
     }
 }