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

@ -20,60 +20,44 @@ namespace ams::powctl::impl {
namespace {
os::ThreadType g_interrupt_thread;
constinit os::ThreadType g_interrupt_thread;
constexpr inline size_t InterruptThreadStackSize = os::MemoryPageSize;
alignas(os::MemoryPageSize) u8 g_interrupt_thread_stack[InterruptThreadStackSize];
constinit u8 g_unit_heap_memory[2_KB];
constinit lmem::HeapHandle g_unit_heap_handle;
constinit sf::UnitHeapMemoryResource g_unit_heap_memory_resource;
IPowerControlDriver::List &GetDriverList() {
static constinit IPowerControlDriver::List s_driver_list;
AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(IPowerControlDriver::List, s_driver_list);
return s_driver_list;
}
ddsf::EventHandlerManager &GetInterruptHandlerManager() {
static constinit util::TypedStorage<ddsf::EventHandlerManager> s_interrupt_handler_manager;
static constinit bool s_initialized = false;
static constinit os::SdkMutex s_mutex;
AMS_FUNCTION_LOCAL_STATIC(ddsf::EventHandlerManager, s_interrupt_handler_manager);
if (AMS_UNLIKELY(!s_initialized)) {
std::scoped_lock lk(s_mutex);
if (AMS_LIKELY(!s_initialized)) {
util::ConstructAt(s_interrupt_handler_manager);
s_initialized = true;
}
}
return util::GetReference(s_interrupt_handler_manager);
return s_interrupt_handler_manager;
}
ddsf::DeviceCodeEntryManager &GetDeviceCodeEntryManager() {
static constinit util::TypedStorage<ddsf::DeviceCodeEntryManager> s_device_code_entry_manager;
static constinit bool s_initialized = false;
static constinit os::SdkMutex s_mutex;
class DeviceCodeEntryManagerWithUnitHeap {
private:
u8 m_heap_memory[2_KB];
sf::UnitHeapMemoryResource m_memory_resource;
util::TypedStorage<ddsf::DeviceCodeEntryManager> m_manager;
public:
DeviceCodeEntryManagerWithUnitHeap() {
/* Initialize the memory resource. */
m_memory_resource.Attach(lmem::CreateUnitHeap(m_heap_memory, sizeof(m_heap_memory), sizeof(ddsf::DeviceCodeEntryHolder), lmem::CreateOption_ThreadSafe));
if (AMS_UNLIKELY(!s_initialized)) {
std::scoped_lock lk(s_mutex);
/* Construct the entry manager. */
util::ConstructAt(m_manager, std::addressof(m_memory_resource));
}
if (AMS_LIKELY(!s_initialized)) {
/* Initialize the entry code heap. */
g_unit_heap_handle = lmem::CreateUnitHeap(g_unit_heap_memory, sizeof(g_unit_heap_memory), sizeof(ddsf::DeviceCodeEntryHolder), lmem::CreateOption_ThreadSafe);
ALWAYS_INLINE operator ddsf::DeviceCodeEntryManager &() {
return util::GetReference(m_manager);
}
};
AMS_FUNCTION_LOCAL_STATIC(DeviceCodeEntryManagerWithUnitHeap, s_device_code_entry_manager_holder);
/* Initialize the entry code memory resource. */
g_unit_heap_memory_resource.Attach(g_unit_heap_handle);
/* Make the entry manager using the newly initialized memory resource. */
util::ConstructAt(s_device_code_entry_manager, std::addressof(g_unit_heap_memory_resource));
s_initialized = true;
}
}
return util::GetReference(s_device_code_entry_manager);
return s_device_code_entry_manager_holder;
}
void InterruptThreadFunction(void *arg) {