mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
LibCore+AK: Use proper atomics in Singleton
This commit is contained in:
parent
e364845456
commit
2d2747cb15
Notes:
sideshowbarker
2024-07-18 11:34:27 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/2d2747cb15a Pull-request: https://github.com/SerenityOS/serenity/pull/8150 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/alimpfard ✅
2 changed files with 9 additions and 10 deletions
|
@ -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 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue