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;
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 this is the first time, see if we get to initialize it
#ifdef KERNEL
Kernel::ScopedCritical critical;
#endif
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
obj = InitFunction();
AK::atomic_store(&obj_var, obj, AK::memory_order_release);
obj_var.store(obj, AK::memory_order_release);
return obj;
}
}
@ -60,7 +60,7 @@ public:
#else
// TODO: yield
#endif
obj = AK::atomic_load(&obj_var, AK::memory_order_acquire);
obj = obj_var.load(AK::memory_order_acquire);
}
if constexpr (allow_create) {
// We should always return an instance if we allow creating one
@ -98,7 +98,7 @@ public:
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;
}
@ -108,7 +108,6 @@ public:
}
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 };
};
static AK::Singleton<SignalHandlersInfo> s_signals;
template<bool create_if_null = true>
inline SignalHandlersInfo* signals_info()
{
static SignalHandlersInfo* s_signals;
return AK::Singleton<SignalHandlersInfo>::get(s_signals);
return s_signals.ptr();
}
pid_t EventLoop::s_pid;