1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 10:47:35 +00:00

Kernel/Devices: Use try_create_device helper for ConsoleDevice

This commit is contained in:
Liav A 2021-09-16 22:23:25 +03:00 committed by Idan Horowitz
parent 5e8dcb9ca7
commit fd4397a430
8 changed files with 43 additions and 26 deletions

View file

@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/Devices/ConsoleDevice.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/IO.h>
#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Sections.h>
@ -14,23 +14,13 @@
// Output bytes to kernel debug port 0xE9 (Bochs console). It's very handy.
#define CONSOLE_OUT_TO_BOCHS_DEBUG_PORT
static Singleton<ConsoleDevice> s_the;
static Kernel::Spinlock g_console_lock;
UNMAP_AFTER_INIT void ConsoleDevice::initialize()
UNMAP_AFTER_INIT NonnullRefPtr<ConsoleDevice> ConsoleDevice::must_create()
{
s_the.ensure_instance();
s_the->after_inserting();
}
ConsoleDevice& ConsoleDevice::the()
{
return *s_the;
}
bool ConsoleDevice::is_initialized()
{
return s_the.is_initialized();
auto device_or_error = DeviceManagement::try_create_device<ConsoleDevice>();
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();
}
UNMAP_AFTER_INIT ConsoleDevice::ConsoleDevice()

View file

@ -14,12 +14,11 @@ namespace Kernel {
class ConsoleDevice final : public CharacterDevice {
AK_MAKE_ETERNAL
public:
static ConsoleDevice& the();
static void initialize();
static bool is_initialized();
friend class DeviceManagement;
public:
static NonnullRefPtr<ConsoleDevice> must_create();
ConsoleDevice();
virtual ~ConsoleDevice() override;
// ^CharacterDevice
@ -34,6 +33,7 @@ public:
const CircularQueue<char, 16384>& logbuffer() const { return m_logbuffer; }
private:
ConsoleDevice();
CircularQueue<char, 16384> m_logbuffer;
};

View file

@ -22,6 +22,12 @@ UNMAP_AFTER_INIT void DeviceManagement::initialize()
s_the.ensure_instance();
}
UNMAP_AFTER_INIT void DeviceManagement::attach_console_device(ConsoleDevice const& device)
{
m_console_device = device;
}
UNMAP_AFTER_INIT void DeviceManagement::attach_null_device(NullDevice const& device)
{
m_null_device = device;
@ -85,4 +91,13 @@ NullDevice const& DeviceManagement::null_device() const
return *m_null_device;
}
ConsoleDevice const& DeviceManagement::console_device() const
{
return *m_console_device;
}
ConsoleDevice& DeviceManagement::console_device()
{
return *m_console_device;
}
}

View file

@ -15,6 +15,7 @@
#include <Kernel/API/KResult.h>
#include <Kernel/API/TimePage.h>
#include <Kernel/Arch/x86/RegisterState.h>
#include <Kernel/Devices/ConsoleDevice.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/UnixTypes.h>
@ -30,14 +31,21 @@ public:
static DeviceManagement& the();
void attach_null_device(NullDevice const&);
bool is_console_device_attached() const { return !m_console_device.is_null(); }
void attach_console_device(ConsoleDevice const&);
void after_inserting_device(Badge<Device>, Device&);
void before_device_removal(Badge<Device>, Device&);
void for_each(Function<void(Device&)>);
Device* get_device(unsigned major, unsigned minor);
NullDevice const& null_device() const;
NullDevice& null_device();
ConsoleDevice const& console_device() const;
ConsoleDevice& console_device();
template<typename DeviceType, typename... Args>
static inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args)
{
@ -48,6 +56,7 @@ public:
private:
RefPtr<NullDevice> m_null_device;
RefPtr<ConsoleDevice> m_console_device;
MutexProtected<HashMap<u32, Device*>> m_devices;
};