1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 15:24:57 +00:00

LibCore+AK: Use proper atomics in Singleton

This commit is contained in:
Daniel Bertalan 2021-06-24 10:28:36 +02:00 committed by Ali Mohammad Pur
parent e364845456
commit 2d2747cb15
2 changed files with 9 additions and 10 deletions

View file

@ -37,19 +37,19 @@ public:
Singleton() = default; Singleton() = default;
template<bool allow_create = true> template<bool allow_create = true>
static T* get(T*& obj_var) static T* get(Atomic<T*>& obj_var)
{ {
T* obj = AK::atomic_load(&obj_var, AK::memory_order_acquire); T* obj = obj_var.load(AK::memory_order_acquire);
if (FlatPtr(obj) <= 0x1) { if (FlatPtr(obj) <= 0x1) {
// If this is the first time, see if we get to initialize it // If this is the first time, see if we get to initialize it
#ifdef KERNEL #ifdef KERNEL
Kernel::ScopedCritical critical; Kernel::ScopedCritical critical;
#endif #endif
if constexpr (allow_create) { if constexpr (allow_create) {
if (obj == nullptr && AK::atomic_compare_exchange_strong(&obj_var, obj, (T*)0x1, AK::memory_order_acq_rel)) { if (obj == nullptr && obj_var.compare_exchange_strong(obj, (T*)0x1, AK::memory_order_acq_rel)) {
// We're the first one // We're the first one
obj = InitFunction(); obj = InitFunction();
AK::atomic_store(&obj_var, obj, AK::memory_order_release); obj_var.store(obj, AK::memory_order_release);
return obj; return obj;
} }
} }
@ -60,7 +60,7 @@ public:
#else #else
// TODO: yield // TODO: yield
#endif #endif
obj = AK::atomic_load(&obj_var, AK::memory_order_acquire); obj = obj_var.load(AK::memory_order_acquire);
} }
if constexpr (allow_create) { if constexpr (allow_create) {
// We should always return an instance if we allow creating one // We should always return an instance if we allow creating one
@ -98,7 +98,7 @@ public:
bool is_initialized() const bool is_initialized() const
{ {
T* obj = AK::atomic_load(&m_obj, AK::memory_order_consume); T* obj = m_obj.load(AK::MemoryOrder::memory_order_consume);
return FlatPtr(obj) > 0x1; return FlatPtr(obj) > 0x1;
} }
@ -108,7 +108,6 @@ public:
} }
private: private:
mutable T* m_obj { nullptr }; // atomic mutable Atomic<T*> m_obj { nullptr };
}; };
} }

View file

@ -112,11 +112,11 @@ struct SignalHandlersInfo {
int next_signal_id { 0 }; int next_signal_id { 0 };
}; };
static AK::Singleton<SignalHandlersInfo> s_signals;
template<bool create_if_null = true> template<bool create_if_null = true>
inline SignalHandlersInfo* signals_info() inline SignalHandlersInfo* signals_info()
{ {
static SignalHandlersInfo* s_signals; return s_signals.ptr();
return AK::Singleton<SignalHandlersInfo>::get(s_signals);
} }
pid_t EventLoop::s_pid; pid_t EventLoop::s_pid;