Skip to content

Commit

Permalink
Merge pull request #42 from Xila-Project/Feature/Graphics
Browse files Browse the repository at this point in the history
Use oncelock instead of option for graphics manager singleton
  • Loading branch information
AlixANNERAUD authored Oct 11, 2024
2 parents ec3d26b + 07b75a5 commit 16bf307
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Modules/Graphics/Tests/Graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fn main() {
.Get_current_task_identifier()
.expect("Failed to get current task identifier");

Graphics::Initialize().expect("Error initializing manager");
Graphics::Initialize();

let Graphics_manager = Graphics::Get_instance().expect("Error getting manager");
let Graphics_manager = Graphics::Get_instance();

let Display = Graphics_manager
.Create_display::<Buffer_size>(Screen_device, Pointer_device, false)
Expand Down
27 changes: 10 additions & 17 deletions Modules/Graphics/src/Manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Mutex;
use std::sync::OnceLock;
use std::time::Duration;

use File_system::Device_type;
Expand All @@ -10,26 +11,18 @@ use super::Point_type;
use crate::Display_type;
use crate::{Error_type, Input_type, Result_type, Screen_read_data_type};

/// Avoid using Arc, because the manager is a singleton.
static mut Manager_instance: Option<Manager_type> = None;
static Manager_instance: OnceLock<Manager_type> = OnceLock::new();

pub fn Initialize() -> Result_type<&'static Manager_type> {
unsafe {
if Is_initialized() {
return Err(Error_type::Already_initialized);
}

Manager_instance.replace(Manager_type::New(Time::Get_instance())?);
}
Get_instance()
}

pub fn Is_initialized() -> bool {
unsafe { Manager_instance.is_some() }
pub fn Initialize() -> &'static Manager_type {
Manager_instance.get_or_init(|| {
Manager_type::New(Time::Get_instance()).expect("Failed to create manager instance")
})
}

pub fn Get_instance() -> Result_type<&'static Manager_type> {
unsafe { Manager_instance.as_ref().ok_or(Error_type::Not_initialized) }
pub fn Get_instance() -> &'static Manager_type {
Manager_instance
.get()
.expect("Failed to get manager instance")
}

struct Inner(Option<Input_type>);
Expand Down

0 comments on commit 16bf307

Please sign in to comment.