From 63c7b44ba8373d54d4214131adf89804c34bd774 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 1 Aug 2018 22:40:00 -0400 Subject: [PATCH 1/2] kernel: Move object class to its own source files General moving to keep kernel object types separate from the direct kernel code. Also essentially a preliminary cleanup before eliminating global kernel state in the kernel code. --- src/citra_qt/debugger/wait_tree.h | 2 +- src/core/CMakeLists.txt | 2 + src/core/hle/kernel/address_arbiter.h | 2 +- src/core/hle/kernel/client_port.cpp | 2 +- src/core/hle/kernel/client_port.h | 2 +- src/core/hle/kernel/client_session.h | 2 +- src/core/hle/kernel/event.cpp | 2 +- src/core/hle/kernel/event.h | 2 +- src/core/hle/kernel/handle_table.cpp | 1 - src/core/hle/kernel/handle_table.h | 2 +- src/core/hle/kernel/hle_ipc.h | 2 +- src/core/hle/kernel/kernel.h | 118 -------------------------- src/core/hle/kernel/mutex.cpp | 1 + src/core/hle/kernel/object.cpp | 37 ++++++++ src/core/hle/kernel/object.h | 106 +++++++++++++++++++++++ src/core/hle/kernel/process.h | 2 +- src/core/hle/kernel/resource_limit.h | 2 +- src/core/hle/kernel/semaphore.h | 2 +- src/core/hle/kernel/server_port.cpp | 2 +- src/core/hle/kernel/server_port.h | 2 +- src/core/hle/kernel/server_session.h | 2 +- src/core/hle/kernel/session.h | 2 +- src/core/hle/kernel/shared_memory.h | 2 +- src/core/hle/kernel/thread.h | 2 +- src/core/hle/kernel/timer.cpp | 2 +- src/core/hle/kernel/timer.h | 2 +- src/core/hle/kernel/wait_object.h | 2 +- src/core/hle/service/service.h | 3 +- src/core/hle/service/sm/sm.h | 4 +- src/core/loader/loader.h | 2 +- 30 files changed, 172 insertions(+), 144 deletions(-) create mode 100644 src/core/hle/kernel/object.cpp create mode 100644 src/core/hle/kernel/object.h diff --git a/src/citra_qt/debugger/wait_tree.h b/src/citra_qt/debugger/wait_tree.h index 24a235a76..47a373b38 100644 --- a/src/citra_qt/debugger/wait_tree.h +++ b/src/citra_qt/debugger/wait_tree.h @@ -9,7 +9,7 @@ #include #include #include "core/core.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" class EmuThread; diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 914ef61d9..e291302dc 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -123,6 +123,8 @@ add_library(core STATIC hle/kernel/memory.h hle/kernel/mutex.cpp hle/kernel/mutex.h + hle/kernel/object.cpp + hle/kernel/object.h hle/kernel/process.cpp hle/kernel/process.h hle/kernel/resource_limit.cpp diff --git a/src/core/hle/kernel/address_arbiter.h b/src/core/hle/kernel/address_arbiter.h index 9b9bdd311..cf2761819 100644 --- a/src/core/hle/kernel/address_arbiter.h +++ b/src/core/hle/kernel/address_arbiter.h @@ -6,7 +6,7 @@ #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/result.h" // Address arbiters are an underlying kernel synchronization object that can be created/used via diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/client_port.cpp index fb2b6f7a3..3fd056e99 100644 --- a/src/core/hle/kernel/client_port.cpp +++ b/src/core/hle/kernel/client_port.cpp @@ -7,7 +7,7 @@ #include "core/hle/kernel/client_session.h" #include "core/hle/kernel/errors.h" #include "core/hle/kernel/hle_ipc.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/server_port.h" #include "core/hle/kernel/server_session.h" diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/client_port.h index a829aeb6d..b42c94bde 100644 --- a/src/core/hle/kernel/client_port.h +++ b/src/core/hle/kernel/client_port.h @@ -6,7 +6,7 @@ #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/result.h" namespace Kernel { diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/client_session.h index f098c4c3a..9c6b47927 100644 --- a/src/core/hle/kernel/client_session.h +++ b/src/core/hle/kernel/client_session.h @@ -7,7 +7,7 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/result.h" namespace Kernel { diff --git a/src/core/hle/kernel/event.cpp b/src/core/hle/kernel/event.cpp index 9cae2369f..6c93719e3 100644 --- a/src/core/hle/kernel/event.cpp +++ b/src/core/hle/kernel/event.cpp @@ -7,7 +7,7 @@ #include #include "common/assert.h" #include "core/hle/kernel/event.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/thread.h" namespace Kernel { diff --git a/src/core/hle/kernel/event.h b/src/core/hle/kernel/event.h index e5c924a75..1c99911b2 100644 --- a/src/core/hle/kernel/event.h +++ b/src/core/hle/kernel/event.h @@ -5,7 +5,7 @@ #pragma once #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/wait_object.h" namespace Kernel { diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 3aec18bb3..8b24b7806 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -7,7 +7,6 @@ #include "common/logging/log.h" #include "core/hle/kernel/errors.h" #include "core/hle/kernel/handle_table.h" -#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/thread.h" diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index ba968c666..22ddda630 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -7,7 +7,7 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/result.h" namespace Kernel { diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 22a96f8fc..5622be96f 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -14,7 +14,7 @@ #include "common/common_types.h" #include "common/swap.h" #include "core/hle/ipc.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/server_session.h" namespace Service { diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 73fab3981..2bc45d7db 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -4,128 +4,10 @@ #pragma once -#include -#include -#include -#include -#include "common/assert.h" #include "common/common_types.h" namespace Kernel { -using Handle = u32; - -enum class HandleType : u32 { - Unknown, - Event, - Mutex, - SharedMemory, - Thread, - Process, - AddressArbiter, - Semaphore, - Timer, - ResourceLimit, - CodeSet, - ClientPort, - ServerPort, - ClientSession, - ServerSession, -}; - -enum { - DEFAULT_STACK_SIZE = 0x4000, -}; - -enum class ResetType { - OneShot, - Sticky, - Pulse, -}; - -class Object : NonCopyable { -public: - virtual ~Object() {} - - /// Returns a unique identifier for the object. For debugging purposes only. - unsigned int GetObjectId() const { - return object_id; - } - - virtual std::string GetTypeName() const { - return "[BAD KERNEL OBJECT TYPE]"; - } - virtual std::string GetName() const { - return "[UNKNOWN KERNEL OBJECT]"; - } - virtual Kernel::HandleType GetHandleType() const = 0; - - /** - * Check if a thread can wait on the object - * @return True if a thread can wait on the object, otherwise false - */ - bool IsWaitable() const { - switch (GetHandleType()) { - case HandleType::Event: - case HandleType::Mutex: - case HandleType::Thread: - case HandleType::Semaphore: - case HandleType::Timer: - case HandleType::ServerPort: - case HandleType::ServerSession: - return true; - - case HandleType::Unknown: - case HandleType::SharedMemory: - case HandleType::Process: - case HandleType::AddressArbiter: - case HandleType::ResourceLimit: - case HandleType::CodeSet: - case HandleType::ClientPort: - case HandleType::ClientSession: - return false; - } - - UNREACHABLE(); - } - -public: - static unsigned int next_object_id; - -private: - friend void intrusive_ptr_add_ref(Object*); - friend void intrusive_ptr_release(Object*); - - unsigned int ref_count = 0; - unsigned int object_id = next_object_id++; -}; - -// Special functions used by boost::instrusive_ptr to do automatic ref-counting -inline void intrusive_ptr_add_ref(Object* object) { - ++object->ref_count; -} - -inline void intrusive_ptr_release(Object* object) { - if (--object->ref_count == 0) { - delete object; - } -} - -template -using SharedPtr = boost::intrusive_ptr; - -/** - * Attempts to downcast the given Object pointer to a pointer to T. - * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. - */ -template -inline SharedPtr DynamicObjectCast(SharedPtr object) { - if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { - return boost::static_pointer_cast(std::move(object)); - } - return nullptr; -} - /// Initialize the kernel with the specified system mode. void Init(u32 system_mode); diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 4f35aa5ef..3593ab2f8 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -10,6 +10,7 @@ #include "core/hle/kernel/errors.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/mutex.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/thread.h" namespace Kernel { diff --git a/src/core/hle/kernel/object.cpp b/src/core/hle/kernel/object.cpp new file mode 100644 index 000000000..48bc80fb2 --- /dev/null +++ b/src/core/hle/kernel/object.cpp @@ -0,0 +1,37 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "core/hle/kernel/object.h" + +namespace Kernel { + +Object::~Object() = default; + +bool Object::IsWaitable() const { + switch (GetHandleType()) { + case HandleType::Event: + case HandleType::Mutex: + case HandleType::Thread: + case HandleType::Semaphore: + case HandleType::Timer: + case HandleType::ServerPort: + case HandleType::ServerSession: + return true; + + case HandleType::Unknown: + case HandleType::SharedMemory: + case HandleType::Process: + case HandleType::AddressArbiter: + case HandleType::ResourceLimit: + case HandleType::CodeSet: + case HandleType::ClientPort: + case HandleType::ClientSession: + return false; + } + + UNREACHABLE(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/object.h b/src/core/hle/kernel/object.h new file mode 100644 index 000000000..9467a2584 --- /dev/null +++ b/src/core/hle/kernel/object.h @@ -0,0 +1,106 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include + +#include "common/common_types.h" + +namespace Kernel { + +using Handle = u32; + +enum class HandleType : u32 { + Unknown, + Event, + Mutex, + SharedMemory, + Thread, + Process, + AddressArbiter, + Semaphore, + Timer, + ResourceLimit, + CodeSet, + ClientPort, + ServerPort, + ClientSession, + ServerSession, +}; + +enum { + DEFAULT_STACK_SIZE = 0x4000, +}; + +enum class ResetType { + OneShot, + Sticky, + Pulse, +}; + +class Object : NonCopyable { +public: + virtual ~Object(); + + /// Returns a unique identifier for the object. For debugging purposes only. + unsigned int GetObjectId() const { + return object_id; + } + + virtual std::string GetTypeName() const { + return "[BAD KERNEL OBJECT TYPE]"; + } + virtual std::string GetName() const { + return "[UNKNOWN KERNEL OBJECT]"; + } + virtual HandleType GetHandleType() const = 0; + + /** + * Check if a thread can wait on the object + * @return True if a thread can wait on the object, otherwise false + */ + bool IsWaitable() const; + +public: + static unsigned int next_object_id; + +private: + friend void intrusive_ptr_add_ref(Object*); + friend void intrusive_ptr_release(Object*); + + unsigned int ref_count = 0; + unsigned int object_id = next_object_id++; +}; + +// Special functions used by boost::instrusive_ptr to do automatic ref-counting +inline void intrusive_ptr_add_ref(Object* object) { + ++object->ref_count; +} + +inline void intrusive_ptr_release(Object* object) { + if (--object->ref_count == 0) { + delete object; + } +} + +template +using SharedPtr = boost::intrusive_ptr; + +/** + * Attempts to downcast the given Object pointer to a pointer to T. + * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. + */ +template +inline SharedPtr DynamicObjectCast(SharedPtr object) { + if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { + return boost::static_pointer_cast(std::move(object)); + } + return nullptr; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index a21b8e704..beb54ed3b 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -12,7 +12,7 @@ #include #include "common/bit_field.h" #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/vm_manager.h" namespace Kernel { diff --git a/src/core/hle/kernel/resource_limit.h b/src/core/hle/kernel/resource_limit.h index 1a0ca11f1..7d6e8611d 100644 --- a/src/core/hle/kernel/resource_limit.h +++ b/src/core/hle/kernel/resource_limit.h @@ -5,7 +5,7 @@ #pragma once #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" namespace Kernel { diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h index ce5690225..6019b09a2 100644 --- a/src/core/hle/kernel/semaphore.h +++ b/src/core/hle/kernel/semaphore.h @@ -7,7 +7,7 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/wait_object.h" #include "core/hle/result.h" diff --git a/src/core/hle/kernel/server_port.cpp b/src/core/hle/kernel/server_port.cpp index 0b7061403..7b6211fd8 100644 --- a/src/core/hle/kernel/server_port.cpp +++ b/src/core/hle/kernel/server_port.cpp @@ -6,7 +6,7 @@ #include "common/assert.h" #include "core/hle/kernel/client_port.h" #include "core/hle/kernel/errors.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/server_port.h" #include "core/hle/kernel/server_session.h" #include "core/hle/kernel/thread.h" diff --git a/src/core/hle/kernel/server_port.h b/src/core/hle/kernel/server_port.h index 9ef4ecc35..9413d8ab7 100644 --- a/src/core/hle/kernel/server_port.h +++ b/src/core/hle/kernel/server_port.h @@ -8,7 +8,7 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/wait_object.h" namespace Kernel { diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h index 477474a25..c739bae5e 100644 --- a/src/core/hle/kernel/server_session.h +++ b/src/core/hle/kernel/server_session.h @@ -8,7 +8,7 @@ #include #include "common/assert.h" #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/wait_object.h" #include "core/hle/result.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/session.h b/src/core/hle/kernel/session.h index 9c38c2fd0..8df6c3e4c 100644 --- a/src/core/hle/kernel/session.h +++ b/src/core/hle/kernel/session.h @@ -4,7 +4,7 @@ #pragma once -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" namespace Kernel { diff --git a/src/core/hle/kernel/shared_memory.h b/src/core/hle/kernel/shared_memory.h index e948819c0..2f9ced2d9 100644 --- a/src/core/hle/kernel/shared_memory.h +++ b/src/core/hle/kernel/shared_memory.h @@ -6,7 +6,7 @@ #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/process.h" #include "core/hle/result.h" diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 2acd8f331..f1d80a1d5 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -11,7 +11,7 @@ #include #include "common/common_types.h" #include "core/arm/arm_interface.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/wait_object.h" #include "core/hle/result.h" diff --git a/src/core/hle/kernel/timer.cpp b/src/core/hle/kernel/timer.cpp index 1c9b0b6fc..f1965365c 100644 --- a/src/core/hle/kernel/timer.cpp +++ b/src/core/hle/kernel/timer.cpp @@ -7,7 +7,7 @@ #include "common/logging/log.h" #include "core/core_timing.h" #include "core/hle/kernel/handle_table.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/thread.h" #include "core/hle/kernel/timer.h" diff --git a/src/core/hle/kernel/timer.h b/src/core/hle/kernel/timer.h index 3999c500e..6607f6058 100644 --- a/src/core/hle/kernel/timer.h +++ b/src/core/hle/kernel/timer.h @@ -5,7 +5,7 @@ #pragma once #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" #include "core/hle/kernel/wait_object.h" namespace Kernel { diff --git a/src/core/hle/kernel/wait_object.h b/src/core/hle/kernel/wait_object.h index 861578186..5af04c6a8 100644 --- a/src/core/hle/kernel/wait_object.h +++ b/src/core/hle/kernel/wait_object.h @@ -7,7 +7,7 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" namespace Kernel { diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index e99fb3474..d04b6bdd6 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -10,11 +10,10 @@ #include #include #include -#include "common/bit_field.h" #include "common/common_types.h" #include "core/hle/kernel/hle_ipc.h" -#include "core/hle/kernel/kernel.h" #include "core/hle/service/sm/sm.h" +#include "core/hle/kernel/object.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace Service diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index bfd0ef553..ae3de6eff 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -4,9 +4,11 @@ #pragma once +#include #include #include -#include "core/hle/kernel/kernel.h" + +#include "core/hle/kernel/object.h" #include "core/hle/result.h" #include "core/hle/service/service.h" diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 95d0e73ee..f6b4fcccd 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -14,7 +14,7 @@ #include "common/common_types.h" #include "common/file_util.h" #include "core/file_sys/romfs_reader.h" -#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/object.h" namespace Kernel { struct AddressMapping; From 8fceac801c1a2ea7e9443433435883c34b9ca24a Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sat, 25 Aug 2018 14:05:59 +0200 Subject: [PATCH 2/2] Fix clang-format --- src/core/hle/service/service.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index d04b6bdd6..a67c3499a 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -12,8 +12,8 @@ #include #include "common/common_types.h" #include "core/hle/kernel/hle_ipc.h" -#include "core/hle/service/sm/sm.h" #include "core/hle/kernel/object.h" +#include "core/hle/service/sm/sm.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace Service