Skip to content

Commit

Permalink
AK+Kernel: Add AK::AtomicRefCounted and use everywhere in the kernel
Browse files Browse the repository at this point in the history
Instead of having two separate implementations of AK::RefCounted, one
for userspace and one for kernelspace, there is now RefCounted and
AtomicRefCounted.
  • Loading branch information
awesomekling committed Aug 20, 2022
1 parent 4889eb0 commit e475263
Show file tree
Hide file tree
Showing 45 changed files with 81 additions and 94 deletions.
18 changes: 9 additions & 9 deletions Kernel/Library/ThreadSafeRefCounted.h → AK/AtomicRefCounted.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2018-2022, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -15,9 +15,9 @@

namespace AK {

class RefCountedBase {
AK_MAKE_NONCOPYABLE(RefCountedBase);
AK_MAKE_NONMOVABLE(RefCountedBase);
class AtomicRefCountedBase {
AK_MAKE_NONCOPYABLE(AtomicRefCountedBase);
AK_MAKE_NONMOVABLE(AtomicRefCountedBase);

public:
using RefCountType = unsigned int;
Expand Down Expand Up @@ -48,8 +48,8 @@ class RefCountedBase {
}

protected:
RefCountedBase() = default;
~RefCountedBase()
AtomicRefCountedBase() = default;
~AtomicRefCountedBase()
{
VERIFY(m_ref_count.load(AK::MemoryOrder::memory_order_relaxed) == 0);
}
Expand All @@ -65,7 +65,7 @@ class RefCountedBase {
};

template<typename T>
class RefCounted : public RefCountedBase {
class AtomicRefCounted : public AtomicRefCountedBase {
public:
bool unref() const
{
Expand All @@ -83,5 +83,5 @@ class RefCounted : public RefCountedBase {

}

using AK::RefCounted;
using AK::RefCountedBase;
using AK::AtomicRefCounted;
using AK::AtomicRefCountedBase;
18 changes: 6 additions & 12 deletions AK/RefCounted.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@

#pragma once

#ifdef KERNEL
# include <Kernel/Library/ThreadSafeRefCounted.h>
#else

# include <AK/Assertions.h>
# include <AK/Checked.h>
# include <AK/Noncopyable.h>
# include <AK/Platform.h>
# include <AK/StdLibExtras.h>
#include <AK/Assertions.h>
#include <AK/Checked.h>
#include <AK/Noncopyable.h>
#include <AK/Platform.h>
#include <AK/StdLibExtras.h>

namespace AK {

Expand Down Expand Up @@ -67,7 +63,7 @@ class RefCounted : public RefCountedBase {
if (new_ref_count == 0) {
if constexpr (requires { that->will_be_destroyed(); })
that->will_be_destroyed();
delete static_cast<const T*>(this);
delete static_cast<T const*>(this);
return true;
}
return false;
Expand All @@ -78,5 +74,3 @@ class RefCounted : public RefCountedBase {

using AK::RefCounted;
using AK::RefCountedBase;

#endif
4 changes: 2 additions & 2 deletions Kernel/Arch/aarch64/IRQController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/AtomicRefCounted.h>
#include <AK/Types.h>

namespace Kernel {

class GenericInterruptHandler;

class IRQController : public RefCounted<IRQController> {
class IRQController : public AtomicRefCounted<IRQController> {
public:
virtual ~IRQController() = default;

Expand Down
4 changes: 2 additions & 2 deletions Kernel/Arch/x86/IRQController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/AtomicRefCounted.h>
#include <AK/Types.h>
#include <Kernel/Interrupts/GenericInterruptHandler.h>

Expand All @@ -19,7 +19,7 @@ enum class IRQControllerType {
i82093AA = 2 /* Intel 82093AA I/O ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER (IOAPIC) */
};

class IRQController : public RefCounted<IRQController> {
class IRQController : public AtomicRefCounted<IRQController> {
public:
virtual ~IRQController() = default;

Expand Down
1 change: 0 additions & 1 deletion Kernel/Arch/x86/InterruptManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <AK/Function.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Types.h>
#include <Kernel/Arch/x86/IRQController.h>
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Bus/USB/USBController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/Error.h>
#include <AK/RefCounted.h>
#include <Kernel/Bus/USB/USBDevice.h>
#include <Kernel/Bus/USB/USBTransfer.h>

namespace Kernel::USB {

class USBController : public RefCounted<USBController> {
class USBController : public AtomicRefCounted<USBController> {
public:
virtual ~USBController() = default;

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Bus/USB/USBDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class USBConfiguration;
//
// https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf
class Hub;
class Device : public RefCounted<Device> {
class Device : public AtomicRefCounted<Device> {
public:
enum class DeviceSpeed : u8 {
FullSpeed = 0,
Expand Down
1 change: 0 additions & 1 deletion Kernel/Bus/USB/USBHub.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/Types.h>
#include <Kernel/Bus/USB/USBDevice.h>

Expand Down
3 changes: 2 additions & 1 deletion Kernel/Bus/USB/USBTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <Kernel/Bus/USB/PacketTypes.h>
Expand All @@ -17,7 +18,7 @@
// TODO: Callback stuff in this class please!
namespace Kernel::USB {

class Transfer : public RefCounted<Transfer> {
class Transfer final : public AtomicRefCounted<Transfer> {
public:
static ErrorOr<NonnullRefPtr<Transfer>> try_create(Pipe&, u16 length, Memory::Region& dma_buffer);

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Bus/VirtIO/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Kernel::VirtIO {
class Console
: public VirtIO::Device
, public RefCounted<Console> {
, public AtomicRefCounted<Console> {
friend VirtIO::ConsolePort;

public:
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Bus/VirtIO/RNG.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/AtomicRefCounted.h>
#include <Kernel/Bus/VirtIO/Device.h>
#include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/Random.h>
Expand All @@ -16,7 +16,7 @@ namespace Kernel::VirtIO {
#define REQUESTQ 0

class RNG final
: public RefCounted<RNG>
: public AtomicRefCounted<RNG>
, public VirtIO::Device {
public:
static NonnullRefPtr<RNG> must_create(PCI::DeviceIdentifier const&);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/AsyncDeviceRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Device;

extern WorkQueue* g_io_work;

class AsyncDeviceRequest : public RefCounted<AsyncDeviceRequest> {
class AsyncDeviceRequest : public AtomicRefCounted<AsyncDeviceRequest> {
AK_MAKE_NONCOPYABLE(AsyncDeviceRequest);
AK_MAKE_NONMOVABLE(AsyncDeviceRequest);

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/Audio/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Kernel {

class AudioManagement;
class AudioController
: public RefCounted<AudioController>
: public AtomicRefCounted<AudioController>
, public Weakable<AudioController> {
friend class AudioManagement;

Expand Down
4 changes: 2 additions & 2 deletions Kernel/Devices/HID/I8042Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/AtomicRefCounted.h>
#include <Kernel/Devices/HID/KeyboardDevice.h>
#include <Kernel/Devices/HID/MouseDevice.h>
#include <Kernel/Locking/Spinlock.h>
Expand Down Expand Up @@ -83,7 +83,7 @@ class I8042Device {
class PS2KeyboardDevice;
class PS2MouseDevice;
class HIDManagement;
class I8042Controller : public RefCounted<I8042Controller> {
class I8042Controller final : public AtomicRefCounted<I8042Controller> {
friend class PS2KeyboardDevice;
friend class PS2MouseDevice;

Expand Down
6 changes: 3 additions & 3 deletions Kernel/FileSystem/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/Error.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <AK/Weakable.h>
Expand Down Expand Up @@ -71,10 +71,10 @@ class FileBlockerSet final : public Thread::BlockerSet {
// - Should create a Region in the Process and return it if successful.

class File
: public RefCounted<File>
: public AtomicRefCounted<File>
, public Weakable<File> {
public:
virtual bool unref() const { return RefCounted<File>::unref(); }
virtual bool unref() const { return AtomicRefCounted<File>::unref(); }
virtual void will_be_destroyed() { }
virtual ~File();

Expand Down
4 changes: 2 additions & 2 deletions Kernel/FileSystem/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/Error.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <Kernel/FileSystem/InodeIdentifier.h>
Expand All @@ -18,7 +18,7 @@

namespace Kernel {

class FileSystem : public RefCounted<FileSystem> {
class FileSystem : public AtomicRefCounted<FileSystem> {
friend class Inode;

public:
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/ISO9660FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class ISO9660FS final : public BlockBasedFileSystem {
friend ISO9660DirectoryIterator;

public:
struct DirectoryEntry : public RefCounted<DirectoryEntry> {
struct DirectoryEntry final : public AtomicRefCounted<DirectoryEntry> {
u32 extent { 0 };
u32 length { 0 };

Expand Down
4 changes: 2 additions & 2 deletions Kernel/FileSystem/OpenFileDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/Badge.h>
#include <AK/RefCounted.h>
#include <Kernel/FileSystem/FIFO.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/FileSystem/InodeMetadata.h>
Expand All @@ -22,7 +22,7 @@ class OpenFileDescriptionData {
virtual ~OpenFileDescriptionData() = default;
};

class OpenFileDescription : public RefCounted<OpenFileDescription> {
class OpenFileDescription final : public AtomicRefCounted<OpenFileDescription> {
public:
static ErrorOr<NonnullRefPtr<OpenFileDescription>> try_create(Custody&);
static ErrorOr<NonnullRefPtr<OpenFileDescription>> try_create(File&);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/Plan9FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Plan9FS final : public FileBackedFileSystem {
mutable Spinlock m_lock { LockRank::None };
};

struct ReceiveCompletion : public RefCounted<ReceiveCompletion> {
struct ReceiveCompletion final : public AtomicRefCounted<ReceiveCompletion> {
mutable Spinlock lock { LockRank::None };
bool completed { false };
const u16 tag;
Expand Down
4 changes: 2 additions & 2 deletions Kernel/FileSystem/SysFS/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/Error.h>
#include <AK/Function.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <AK/Types.h>
Expand All @@ -24,7 +24,7 @@ struct SysFSInodeData : public OpenFileDescriptionData {
};

class SysFSDirectory;
class SysFSComponent : public RefCounted<SysFSComponent> {
class SysFSComponent : public AtomicRefCounted<SysFSComponent> {
friend class SysFSDirectory;

public:
Expand Down
2 changes: 0 additions & 2 deletions Kernel/Firmware/ACPI/Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <Kernel/PhysicalAddress.h>

namespace Kernel::ACPI {
Expand Down
6 changes: 2 additions & 4 deletions Kernel/FutexQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

#pragma once

#include <AK/Atomic.h>
#include <AK/RefCounted.h>
#include <AK/AtomicRefCounted.h>
#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Memory/VMObject.h>
#include <Kernel/Thread.h>

namespace Kernel {

class FutexQueue final
: public RefCounted<FutexQueue>
: public AtomicRefCounted<FutexQueue>
, public Thread::BlockerSet {
public:
FutexQueue();
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Graphics/Console/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/AtomicRefCounted.h>
#include <AK/Types.h>
#include <Kernel/Graphics/GenericGraphicsAdapter.h>

namespace Kernel::Graphics {

class Console : public RefCounted<Console> {
class Console : public AtomicRefCounted<Console> {
public:
// Stanadard VGA text mode colors
enum Color : u8 {
Expand Down
1 change: 0 additions & 1 deletion Kernel/Graphics/Console/GenericFramebufferConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#pragma once

#include <AK/RefCounted.h>
#include <AK/Types.h>
#include <Kernel/Graphics/Console/Console.h>
#include <Kernel/PhysicalAddress.h>
Expand Down
Loading

0 comments on commit e475263

Please sign in to comment.