ams: deduplicate static initialization logic

This commit is contained in:
Michael Scire
2021-12-13 13:07:03 -08:00
parent 78f7218c4f
commit 30fac905af
24 changed files with 202 additions and 262 deletions

View File

@@ -289,14 +289,8 @@ namespace ams::settings::impl {
}
lmem::HeapHandle &GetHeapHandle() {
static constinit bool s_is_initialized = false;
static constinit lmem::HeapHandle s_heap_handle;
static constinit u8 s_heap_memory[HeapMemorySize];
if (!s_is_initialized) {
s_heap_handle = lmem::CreateExpHeap(s_heap_memory, sizeof(s_heap_memory), lmem::CreateOption_None);
s_is_initialized = true;
}
AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(u8, s_heap_memory[HeapMemorySize]);
AMS_FUNCTION_LOCAL_STATIC(lmem::HeapHandle, s_heap_handle, lmem::CreateExpHeap(s_heap_memory, sizeof(s_heap_memory), lmem::CreateOption_ThreadSafe));
return s_heap_handle;
}
@@ -314,24 +308,15 @@ namespace ams::settings::impl {
AMS_ASSERT(out != nullptr);
/* Declare static instance variables. */
static constinit util::TypedStorage<Map> s_storage = {};
static constinit bool s_is_initialized = false;
static constinit bool s_is_loaded = false;
AMS_FUNCTION_LOCAL_STATIC(Map, s_map);
AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(bool, s_is_map_loaded, false);
/* Get pointer to the map. */
Map *map = util::GetPointer(s_storage);
/* Construct the map, if we haven't already. */
if (AMS_UNLIKELY(!s_is_initialized)) {
/* Construct the instance. */
util::ConstructAt(s_storage);
/* Note that we constructed. */
s_is_initialized = true;
}
Map * const map = std::addressof(s_map);
/* TODO: Mutex? */
/* Load the map, if we haven't already. */
if (AMS_UNLIKELY(!s_is_loaded)) {
if (AMS_UNLIKELY(!s_is_map_loaded)) {
/* Attempt to load the map, allowing for failure if acceptable. */
const auto result = LoadKeyValueStoreMap(map);
@@ -340,7 +325,7 @@ namespace ams::settings::impl {
}
/* Note that the map is loaded. */
s_is_loaded = true;
s_is_map_loaded = true;
}
/* Set the output pointer. */
@@ -425,26 +410,13 @@ namespace ams::settings::impl {
AMS_ASSERT(out_data != nullptr);
/* Declare static instance variables. */
static constinit util::TypedStorage<SystemData> s_storage = {};
static constinit bool s_initialized = false;
static constinit bool s_mounted = false;
AMS_FUNCTION_LOCAL_STATIC(SystemData, s_data, id, GetSystemDataMountName<T>());
AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(bool, s_mounted, false);
/* Get pointer to the system data. */
SystemData *data = util::GetPointer(s_storage);
/* Construct the system data, if we haven't already. */
if (AMS_UNLIKELY(!s_initialized)) {
/* Construct the instance. */
util::ConstructAt(s_storage);
/* Setup system data. */
data->SetSystemDataId(id);
data->SetMountName(GetSystemDataMountName<T>());
/* Note that we constructed. */
s_initialized = true;
}
SystemData *data = std::addressof(s_data);
/* TODO: Mutex? */
/* Mount the system data, if we haven't already. */
if (AMS_UNLIKELY(!s_mounted)) {
/* Mount the system data. */
@@ -464,28 +436,11 @@ namespace ams::settings::impl {
AMS_ASSERT(out_data != nullptr);
/* Declare static instance variables. */
static constinit util::TypedStorage<SystemSaveData> s_storage = {};
static constinit bool s_initialized = false;
static constinit bool s_mounted = false;
AMS_FUNCTION_LOCAL_STATIC(SystemSaveData, s_data, SystemSaveDataId, SystemSaveDataSize, SystemSaveDataJournalSize, SystemSaveDataFlags, SystemSaveDataMountName);
AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(bool, s_mounted, false);
/* Get pointer to the system data. */
SystemSaveData *data = util::GetPointer(s_storage);
/* Construct the system data, if we haven't already. */
if (AMS_UNLIKELY(!s_initialized)) {
/* Construct the instance. */
util::ConstructAt(s_storage);
/* Setup system data. */
data->SetSystemSaveDataId(SystemSaveDataId);
data->SetTotalSize(SystemSaveDataSize);
data->SetJournalSize(SystemSaveDataJournalSize);
data->SetFlags(SystemSaveDataFlags);
data->SetMountName(SystemSaveDataMountName);
/* Note that we constructed. */
s_initialized = true;
}
/* Get pointer to the system save data. */
SystemSaveData *data = std::addressof(s_data);
/* Mount the system data, if we haven't already. */
if (AMS_UNLIKELY(!s_mounted)) {

View File

@@ -56,27 +56,33 @@ namespace ams::settings::impl {
}
SplConfig GetSplConfig() {
static constinit bool s_is_initialized = false;
static constinit SplConfig s_config;
class SplConfigHolder {
NON_COPYABLE(SplConfigHolder);
NON_MOVEABLE(SplConfigHolder);
private:
SplConfig m_config;
public:
SplConfigHolder() {
/* Initialize spl. */
spl::Initialize();
ON_SCOPE_EXIT { spl::Finalize(); };
if (!s_is_initialized) {
/* Initialize spl. */
spl::Initialize();
ON_SCOPE_EXIT { spl::Finalize(); };
/* Create the config. */
m_config = {
.is_development = spl::IsDevelopment(),
.hardware_type = ConvertToSplHardwareType(spl::GetHardwareType()),
.is_quest = IsSplRetailInteractiveDisplayStateEnabled(spl::GetRetailInteractiveDisplayState()),
.device_id_low = spl::GetDeviceIdLow(),
};
}
/* Create the config. */
s_config = {
.is_development = spl::IsDevelopment(),
.hardware_type = ConvertToSplHardwareType(spl::GetHardwareType()),
.is_quest = IsSplRetailInteractiveDisplayStateEnabled(spl::GetRetailInteractiveDisplayState()),
.device_id_low = spl::GetDeviceIdLow(),
};
ALWAYS_INLINE operator SplConfig() {
return m_config;
}
};
/* Mark as initialized. */
s_is_initialized = true;
}
return s_config;
AMS_FUNCTION_LOCAL_STATIC(SplConfigHolder, s_config_holder);
return s_config_holder;
}
}

View File

@@ -25,27 +25,9 @@ namespace ams::settings::impl {
StaticObject();
public:
static T &Get() {
/* Declare static instance variables. */
static constinit util::TypedStorage<T> s_storage = {};
static constinit bool s_initialized = false;
static constinit os::SdkMutex s_mutex;
AMS_FUNCTION_LOCAL_STATIC(T, s_object);
/* If we haven't already done so, construct the instance. */
if (AMS_UNLIKELY(!s_initialized)) {
std::scoped_lock lk(s_mutex);
/* Check that we didn't concurrently construct the instance. */
if (AMS_LIKELY(!s_initialized)) {
/* Construct the instance. */
util::ConstructAt(s_storage);
/* Note that we constructed. */
s_initialized = true;
}
}
/* Return the constructed instance. */
return util::GetReference(s_storage);
return s_object;
}
};

View File

@@ -30,6 +30,11 @@ namespace ams::settings::impl {
public:
SystemData() : m_system_data_id(), m_mount_name(), m_file_path() { /* ... */ }
SystemData(ncm::SystemDataId id, const char *mn) : SystemData() {
this->SetSystemDataId(id);
this->SetMountName(mn);
}
void SetSystemDataId(ncm::SystemDataId id);
void SetMountName(const char *name);
Result Mount();

View File

@@ -30,6 +30,14 @@ namespace ams::settings::impl {
public:
SystemSaveData() : m_system_save_data_id(0), m_save_data_space_id(fs::SaveDataSpaceId::System), m_total_size(0), m_journal_size(0), m_flags(0) { /* ... */ }
SystemSaveData(u64 id, s64 total_size, s64 journal_size, u32 flags, const char *mn) : SystemSaveData() {
this->SetSystemSaveDataId(id);
this->SetTotalSize(total_size);
this->SetJournalSize(journal_size);
this->SetFlags(flags);
this->SetMountName(mn);
}
void SetSystemSaveDataId(u64 id);
void SetTotalSize(s64 size);
void SetJournalSize(s64 size);