2020-08-20 18:35:13 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-20 18:35:13 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-08-20 18:35:13 +03:00
|
|
|
#include <AK/Atomic.h>
|
2020-11-22 04:02:56 +03:00
|
|
|
#include <AK/Noncopyable.h>
|
2020-08-25 04:35:19 +03:00
|
|
|
#ifdef KERNEL
|
2021-03-07 23:28:28 +03:00
|
|
|
# include <Kernel/Arch/x86/CPU.h>
|
2020-08-25 04:35:19 +03:00
|
|
|
#endif
|
2020-08-20 18:35:13 +03:00
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
#ifndef __serenity__
|
|
|
|
# include <new>
|
|
|
|
#endif
|
2020-08-20 18:35:13 +03:00
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct SingletonInstanceCreator {
|
|
|
|
static T* create()
|
|
|
|
{
|
|
|
|
return new T();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, T* (*InitFunction)() = SingletonInstanceCreator<T>::create>
|
2020-08-20 18:35:13 +03:00
|
|
|
class Singleton {
|
2020-08-25 04:35:19 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(Singleton);
|
|
|
|
AK_MAKE_NONMOVABLE(Singleton);
|
2020-09-18 10:49:51 +03:00
|
|
|
|
2020-08-20 18:35:13 +03:00
|
|
|
public:
|
2020-08-25 04:35:19 +03:00
|
|
|
Singleton() = default;
|
|
|
|
|
2021-01-08 22:01:53 +03:00
|
|
|
template<bool allow_create = true>
|
|
|
|
static T* get(T*& obj_var)
|
2020-08-20 18:35:13 +03:00
|
|
|
{
|
2021-01-08 22:01:53 +03:00
|
|
|
T* obj = AK::atomic_load(&obj_var, AK::memory_order_acquire);
|
2020-08-20 18:35:13 +03:00
|
|
|
if (FlatPtr(obj) <= 0x1) {
|
|
|
|
// If this is the first time, see if we get to initialize it
|
2020-08-25 04:35:19 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
Kernel::ScopedCritical critical;
|
|
|
|
#endif
|
2021-01-08 22:01:53 +03:00
|
|
|
if constexpr (allow_create) {
|
|
|
|
if (obj == nullptr && AK::atomic_compare_exchange_strong(&obj_var, 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);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Someone else was faster, wait until they're done
|
|
|
|
while (obj == (T*)0x1) {
|
2020-08-25 04:35:19 +03:00
|
|
|
#ifdef KERNEL
|
2021-01-08 22:01:53 +03:00
|
|
|
Kernel::Processor::wait_check();
|
2020-08-25 04:35:19 +03:00
|
|
|
#else
|
2021-01-08 22:01:53 +03:00
|
|
|
// TODO: yield
|
2020-08-25 04:35:19 +03:00
|
|
|
#endif
|
2021-01-08 22:01:53 +03:00
|
|
|
obj = AK::atomic_load(&obj_var, AK::memory_order_acquire);
|
|
|
|
}
|
|
|
|
if constexpr (allow_create) {
|
|
|
|
// We should always return an instance if we allow creating one
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(obj != nullptr);
|
2020-08-20 18:35:13 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(obj != (T*)0x1);
|
2020-08-20 18:35:13 +03:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:01:53 +03:00
|
|
|
T* ptr() const
|
|
|
|
{
|
|
|
|
return get(m_obj);
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:35:13 +03:00
|
|
|
T* operator->() const
|
|
|
|
{
|
|
|
|
return ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() const
|
|
|
|
{
|
|
|
|
return *ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() const
|
|
|
|
{
|
|
|
|
return ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T&() const
|
|
|
|
{
|
|
|
|
return *ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_initialized() const
|
|
|
|
{
|
|
|
|
T* obj = AK::atomic_load(&m_obj, AK::memory_order_consume);
|
|
|
|
return FlatPtr(obj) > 0x1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ensure_instance()
|
|
|
|
{
|
2020-12-21 02:09:48 +03:00
|
|
|
ptr();
|
2020-08-20 18:35:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable T* m_obj { nullptr }; // atomic
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|