From bca49bed4b07603cba197ee706249e381cc85007 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sat, 16 Aug 2014 00:56:50 -0700 Subject: [PATCH] Removing xe_mutex_t. --- src/xenia/apu/audio_system.cc | 15 +-- src/xenia/apu/audio_system.h | 7 +- src/xenia/core.h | 2 +- src/xenia/core/mutex.h | 27 ----- src/xenia/core/mutex_posix.cc | 60 ---------- src/xenia/core/mutex_win.cc | 50 -------- src/xenia/core/run_loop_win.cc | 1 - src/xenia/core/sources.gypi | 3 - src/xenia/cpu/processor.cc | 7 +- src/xenia/cpu/processor.h | 6 +- src/xenia/kernel/dispatcher.cc | 6 +- src/xenia/kernel/dispatcher.h | 4 +- .../kernel/fs/devices/host_path_device.cc | 2 +- src/xenia/kernel/kernel_state.cc | 20 +--- src/xenia/kernel/kernel_state.h | 4 +- src/xenia/kernel/object_table.cc | 108 +++++++++--------- src/xenia/kernel/object_table.h | 4 +- src/xenia/kernel/objects/xnotify_listener.cc | 13 +-- src/xenia/kernel/objects/xnotify_listener.h | 4 +- src/xenia/kernel/objects/xthread.cc | 12 +- src/xenia/kernel/objects/xthread.h | 3 +- src/xenia/kernel/xobject.cc | 17 +-- src/xenia/kernel/xobject.h | 2 - 23 files changed, 101 insertions(+), 276 deletions(-) delete mode 100644 src/xenia/core/mutex.h delete mode 100644 src/xenia/core/mutex_posix.cc delete mode 100644 src/xenia/core/mutex_win.cc diff --git a/src/xenia/apu/audio_system.cc b/src/xenia/apu/audio_system.cc index 464211411..4eb659120 100644 --- a/src/xenia/apu/audio_system.cc +++ b/src/xenia/apu/audio_system.cc @@ -23,7 +23,6 @@ using namespace xe::cpu; AudioSystem::AudioSystem(Emulator* emulator) : emulator_(emulator), memory_(emulator->memory()), thread_(0), running_(false) { - lock_ = xe_mutex_alloc(); memset(clients_, 0, sizeof(clients_)); for (size_t i = 0; i < maximum_client_count_; ++i) { client_wait_handles_[i] = CreateEvent(NULL, TRUE, FALSE, NULL); @@ -35,7 +34,6 @@ AudioSystem::~AudioSystem() { for (size_t i = 0; i < maximum_client_count_; ++i) { CloseHandle(client_wait_handles_[i]); } - xe_mutex_free(lock_); } X_STATUS AudioSystem::Setup() { @@ -90,10 +88,10 @@ void AudioSystem::ThreadStart() { if (result >= WAIT_OBJECT_0 && result <= WAIT_OBJECT_0 + (maximum_client_count_ - 1)) { size_t index = result - WAIT_OBJECT_0; do { - xe_mutex_lock(lock_); + lock_.lock(); uint32_t client_callback = clients_[index].callback; uint32_t client_callback_arg = clients_[index].wrapped_callback_arg; - xe_mutex_unlock(lock_); + lock_.unlock(); if (client_callback) { uint64_t args[] = { client_callback_arg }; processor->Execute(thread_state_, client_callback, args, XECOUNT(args)); @@ -132,7 +130,7 @@ void AudioSystem::Shutdown() { X_STATUS AudioSystem::RegisterClient( uint32_t callback, uint32_t callback_arg, size_t* out_index) { assert_true(unused_clients_.size()); - xe_mutex_lock(lock_); + std::lock_guard lock(lock_); auto index = unused_clients_.front(); @@ -157,30 +155,27 @@ X_STATUS AudioSystem::RegisterClient( *out_index = index; } - xe_mutex_unlock(lock_); return X_STATUS_SUCCESS; } void AudioSystem::SubmitFrame(size_t index, uint32_t samples_ptr) { SCOPE_profile_cpu_f("apu"); - xe_mutex_lock(lock_); + std::lock_guard lock(lock_); assert_true(index < maximum_client_count_); assert_true(clients_[index].driver != NULL); (clients_[index].driver)->SubmitFrame(samples_ptr); ResetEvent(client_wait_handles_[index]); - xe_mutex_unlock(lock_); } void AudioSystem::UnregisterClient(size_t index) { SCOPE_profile_cpu_f("apu"); - xe_mutex_lock(lock_); + std::lock_guard lock(lock_); assert_true(index < maximum_client_count_); DestroyDriver(clients_[index].driver); clients_[index] = { 0 }; unused_clients_.push(index); - xe_mutex_unlock(lock_); } // free60 may be useful here, however it looks like it's using a different diff --git a/src/xenia/apu/audio_system.h b/src/xenia/apu/audio_system.h index 964e331cf..4eadcaa71 100644 --- a/src/xenia/apu/audio_system.h +++ b/src/xenia/apu/audio_system.h @@ -10,11 +10,12 @@ #ifndef XENIA_APU_AUDIO_SYSTEM_H_ #define XENIA_APU_AUDIO_SYSTEM_H_ +#include +#include + #include #include -#include - XEDECLARECLASS1(xe, Emulator); XEDECLARECLASS2(xe, cpu, Processor); XEDECLARECLASS2(xe, cpu, XenonThreadState); @@ -74,7 +75,7 @@ protected: uint32_t thread_block_; bool running_; - xe_mutex_t* lock_; + std::mutex lock_; static const size_t maximum_client_count_ = 8; diff --git a/src/xenia/core.h b/src/xenia/core.h index 8368a6eb0..a447d6167 100644 --- a/src/xenia/core.h +++ b/src/xenia/core.h @@ -19,8 +19,8 @@ namespace xe { #include #include -#include #include +#include #include #include #include diff --git a/src/xenia/core/mutex.h b/src/xenia/core/mutex.h deleted file mode 100644 index 74856d878..000000000 --- a/src/xenia/core/mutex.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#ifndef XENIA_CORE_MUTEX_H_ -#define XENIA_CORE_MUTEX_H_ - -#include - - -typedef struct xe_mutex xe_mutex_t; - - -xe_mutex_t* xe_mutex_alloc(uint32_t spin_count = 10000); -void xe_mutex_free(xe_mutex_t* mutex); - -int xe_mutex_lock(xe_mutex_t* mutex); -int xe_mutex_trylock(xe_mutex_t* mutex); -int xe_mutex_unlock(xe_mutex_t* mutex); - - -#endif // XENIA_CORE_MUTEX_H_ diff --git a/src/xenia/core/mutex_posix.cc b/src/xenia/core/mutex_posix.cc deleted file mode 100644 index aae3d042c..000000000 --- a/src/xenia/core/mutex_posix.cc +++ /dev/null @@ -1,60 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#include - - -struct xe_mutex { - pthread_mutex_t value; -}; - -xe_mutex_t* xe_mutex_alloc(uint32_t spin_count) { - xe_mutex_t* mutex = (xe_mutex_t*)xe_calloc(sizeof(xe_mutex_t)); - - int result = pthread_mutex_init(&mutex->value, NULL); - switch (result) { - case ENOMEM: - case EINVAL: - xe_free(mutex); - return NULL; - } - - return mutex; -} - -void xe_mutex_free(xe_mutex_t* mutex) { - int result = pthread_mutex_destroy(&mutex->value); - switch (result) { - case EBUSY: - case EINVAL: - break; - default: - break; - } - xe_free(mutex); -} - -int xe_mutex_lock(xe_mutex_t* mutex) { - return pthread_mutex_lock(&mutex->value) == EINVAL ? 1 : 0; -} - -int xe_mutex_trylock(xe_mutex_t* mutex) { - int result = pthread_mutex_trylock(&mutex->value); - switch (result) { - case EBUSY: - case EINVAL: - return 1; - default: - return 0; - } -} - -int xe_mutex_unlock(xe_mutex_t* mutex) { - return pthread_mutex_unlock(&mutex->value) == EINVAL ? 1 : 0; -} diff --git a/src/xenia/core/mutex_win.cc b/src/xenia/core/mutex_win.cc deleted file mode 100644 index 575786aec..000000000 --- a/src/xenia/core/mutex_win.cc +++ /dev/null @@ -1,50 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#include - - -struct xe_mutex { - CRITICAL_SECTION value; -}; - -xe_mutex_t* xe_mutex_alloc(uint32_t spin_count) { - xe_mutex_t* mutex = (xe_mutex_t*)xe_calloc(sizeof(xe_mutex_t)); - - if (spin_count) { - InitializeCriticalSectionAndSpinCount(&mutex->value, spin_count); - } else { - InitializeCriticalSection(&mutex->value); - } - - return mutex; -} - -void xe_mutex_free(xe_mutex_t* mutex) { - DeleteCriticalSection(&mutex->value); - xe_free(mutex); -} - -int xe_mutex_lock(xe_mutex_t* mutex) { - EnterCriticalSection(&mutex->value); - return 0; -} - -int xe_mutex_trylock(xe_mutex_t* mutex) { - if (TryEnterCriticalSection(&mutex->value) == TRUE) { - return 0; - } else { - return 1; - } -} - -int xe_mutex_unlock(xe_mutex_t* mutex) { - LeaveCriticalSection(&mutex->value); - return 0; -} diff --git a/src/xenia/core/run_loop_win.cc b/src/xenia/core/run_loop_win.cc index a2f9efaba..0365bfd93 100644 --- a/src/xenia/core/run_loop_win.cc +++ b/src/xenia/core/run_loop_win.cc @@ -9,7 +9,6 @@ #include -#include #include diff --git a/src/xenia/core/sources.gypi b/src/xenia/core/sources.gypi index b4c226cc1..ae943948b 100644 --- a/src/xenia/core/sources.gypi +++ b/src/xenia/core/sources.gypi @@ -4,7 +4,6 @@ 'hash.cc', 'hash.h', 'mmap.h', - 'mutex.h', 'pal.h', 'path.cc', 'path.h', @@ -20,7 +19,6 @@ ['OS == "mac" or OS == "linux"', { 'sources': [ 'mmap_posix.cc', - 'mutex_posix.cc', 'path_posix.cc', 'socket_posix.cc', ], @@ -38,7 +36,6 @@ ['OS == "win"', { 'sources': [ 'mmap_win.cc', - 'mutex_win.cc', 'pal_win.cc', 'path_win.cc', 'run_loop_win.cc', diff --git a/src/xenia/cpu/processor.cc b/src/xenia/cpu/processor.cc index f94ecf588..220c9dd4a 100644 --- a/src/xenia/cpu/processor.cc +++ b/src/xenia/cpu/processor.cc @@ -53,7 +53,7 @@ namespace { Processor::Processor(Emulator* emulator) : emulator_(emulator), export_resolver_(emulator->export_resolver()), runtime_(0), memory_(emulator->memory()), - interrupt_thread_lock_(NULL), interrupt_thread_state_(NULL), + interrupt_thread_state_(NULL), interrupt_thread_block_(0) { InitializeIfNeeded(); } @@ -62,7 +62,6 @@ Processor::~Processor() { if (interrupt_thread_block_) { memory_->HeapFree(interrupt_thread_block_, 2048); delete interrupt_thread_state_; - xe_mutex_free(interrupt_thread_lock_); } delete runtime_; @@ -103,7 +102,6 @@ int Processor::Setup() { return result; } - interrupt_thread_lock_ = xe_mutex_alloc(10000); interrupt_thread_state_ = new XenonThreadState( runtime_, 0, 16 * 1024, 0); interrupt_thread_state_->set_name("Interrupt"); @@ -171,7 +169,7 @@ uint64_t Processor::ExecuteInterrupt( SCOPE_profile_cpu_f("cpu"); // Acquire lock on interrupt thread (we can only dispatch one at a time). - xe_mutex_lock(interrupt_thread_lock_); + std::lock_guard lock(interrupt_thread_lock_); // Set 0x10C(r13) to the current CPU ID. uint8_t* p = memory_->membase(); @@ -180,6 +178,5 @@ uint64_t Processor::ExecuteInterrupt( // Execute interrupt. uint64_t result = Execute(interrupt_thread_state_, address, args, arg_count); - xe_mutex_unlock(interrupt_thread_lock_); return result; } diff --git a/src/xenia/cpu/processor.h b/src/xenia/cpu/processor.h index d5e79b0f8..6dc1634bd 100644 --- a/src/xenia/cpu/processor.h +++ b/src/xenia/cpu/processor.h @@ -10,9 +10,11 @@ #ifndef XENIA_CPU_PROCESSOR_H_ #define XENIA_CPU_PROCESSOR_H_ +#include +#include + #include -#include XEDECLARECLASS2(alloy, runtime, Breakpoint); XEDECLARECLASS1(xe, Emulator); @@ -66,7 +68,7 @@ private: Memory* memory_; Irql irql_; - xe_mutex_t* interrupt_thread_lock_; + std::mutex interrupt_thread_lock_; XenonThreadState* interrupt_thread_state_; uint64_t interrupt_thread_block_; }; diff --git a/src/xenia/kernel/dispatcher.cc b/src/xenia/kernel/dispatcher.cc index df11b62f4..1781ef76a 100644 --- a/src/xenia/kernel/dispatcher.cc +++ b/src/xenia/kernel/dispatcher.cc @@ -19,21 +19,19 @@ namespace kernel { Dispatcher::Dispatcher(KernelState* kernel_state) : kernel_state_(kernel_state) { - lock_ = xe_mutex_alloc(); dpc_list_ = new NativeList(kernel_state->memory()); } Dispatcher::~Dispatcher() { delete dpc_list_; - xe_mutex_free(lock_); } void Dispatcher::Lock() { - xe_mutex_lock(lock_); + lock_.lock(); } void Dispatcher::Unlock() { - xe_mutex_unlock(lock_); + lock_.unlock(); } } // namespace kernel diff --git a/src/xenia/kernel/dispatcher.h b/src/xenia/kernel/dispatcher.h index 6f8bb6cf4..ff7e9b189 100644 --- a/src/xenia/kernel/dispatcher.h +++ b/src/xenia/kernel/dispatcher.h @@ -10,6 +10,8 @@ #ifndef XENIA_KERNEL_XBOXKRNL_DISPATCHER_H_ #define XENIA_KERNEL_XBOXKRNL_DISPATCHER_H_ +#include + #include #include @@ -40,7 +42,7 @@ private: private: KernelState* kernel_state_; - xe_mutex_t* lock_; + std::mutex lock_; NativeList* dpc_list_; }; diff --git a/src/xenia/kernel/fs/devices/host_path_device.cc b/src/xenia/kernel/fs/devices/host_path_device.cc index c134071ba..9bd4c4a29 100644 --- a/src/xenia/kernel/fs/devices/host_path_device.cc +++ b/src/xenia/kernel/fs/devices/host_path_device.cc @@ -9,8 +9,8 @@ #include +#include #include - #include using namespace xe; diff --git a/src/xenia/kernel/kernel_state.cc b/src/xenia/kernel/kernel_state.cc index 1d1b81a07..ed3537af2 100644 --- a/src/xenia/kernel/kernel_state.cc +++ b/src/xenia/kernel/kernel_state.cc @@ -45,7 +45,6 @@ KernelState::KernelState(Emulator* emulator) : user_profile_ = std::make_unique(); object_table_ = new ObjectTable(); - object_mutex_ = xe_mutex_alloc(10000); assert_null(shared_kernel_state_); shared_kernel_state_ = this; @@ -57,7 +56,6 @@ KernelState::~KernelState() { SetExecutableModule(NULL); // Delete all objects. - xe_mutex_free(object_mutex_); delete object_table_; // Shutdown apps. @@ -124,41 +122,37 @@ void KernelState::SetExecutableModule(XUserModule* module) { } void KernelState::RegisterThread(XThread* thread) { - xe_mutex_lock(object_mutex_); + std::lock_guard lock(object_mutex_); threads_by_id_[thread->thread_id()] = thread; - xe_mutex_unlock(object_mutex_); } void KernelState::UnregisterThread(XThread* thread) { - xe_mutex_lock(object_mutex_); + std::lock_guard lock(object_mutex_); auto it = threads_by_id_.find(thread->thread_id()); if (it != threads_by_id_.end()) { threads_by_id_.erase(it); } - xe_mutex_unlock(object_mutex_); } XThread* KernelState::GetThreadByID(uint32_t thread_id) { + std::lock_guard lock(object_mutex_); XThread* thread = NULL; - xe_mutex_lock(object_mutex_); auto it = threads_by_id_.find(thread_id); if (it != threads_by_id_.end()) { thread = it->second; // Caller must release. thread->Retain(); } - xe_mutex_unlock(object_mutex_); return thread; } void KernelState::RegisterNotifyListener(XNotifyListener* listener) { - xe_mutex_lock(object_mutex_); + std::lock_guard lock(object_mutex_); notify_listeners_.push_back(listener); - xe_mutex_unlock(object_mutex_); } void KernelState::UnregisterNotifyListener(XNotifyListener* listener) { - xe_mutex_lock(object_mutex_); + std::lock_guard lock(object_mutex_); for (auto it = notify_listeners_.begin(); it != notify_listeners_.end(); ++it) { if (*it == listener) { @@ -166,16 +160,14 @@ void KernelState::UnregisterNotifyListener(XNotifyListener* listener) { break; } } - xe_mutex_unlock(object_mutex_); } void KernelState::BroadcastNotification(XNotificationID id, uint32_t data) { - xe_mutex_lock(object_mutex_); + std::lock_guard lock(object_mutex_); for (auto it = notify_listeners_.begin(); it != notify_listeners_.end(); ++it) { (*it)->EnqueueNotification(id, data); } - xe_mutex_unlock(object_mutex_); } void KernelState::CompleteOverlapped(uint32_t overlapped_ptr, X_RESULT result, uint32_t length) { diff --git a/src/xenia/kernel/kernel_state.h b/src/xenia/kernel/kernel_state.h index 097566f90..3587e3c46 100644 --- a/src/xenia/kernel/kernel_state.h +++ b/src/xenia/kernel/kernel_state.h @@ -11,6 +11,7 @@ #define XENIA_KERNEL_KERNEL_STATE_H_ #include +#include #include #include @@ -55,6 +56,7 @@ public: UserProfile* user_profile() const { return user_profile_.get(); } ObjectTable* object_table() const { return object_table_; } + std::mutex& object_mutex() { return object_mutex_; } void RegisterModule(XModule* module); void UnregisterModule(XModule* module); @@ -85,7 +87,7 @@ private: std::unique_ptr user_profile_; ObjectTable* object_table_; - xe_mutex_t* object_mutex_; + std::mutex object_mutex_; std::unordered_map threads_by_id_; std::vector notify_listeners_; diff --git a/src/xenia/kernel/object_table.cc b/src/xenia/kernel/object_table.cc index 0b338d072..b6199db69 100644 --- a/src/xenia/kernel/object_table.cc +++ b/src/xenia/kernel/object_table.cc @@ -21,12 +21,10 @@ ObjectTable::ObjectTable() : table_capacity_(0), table_(NULL), last_free_entry_(0) { - table_mutex_ = xe_mutex_alloc(0); - assert_not_null(table_mutex_); } ObjectTable::~ObjectTable() { - xe_mutex_lock(table_mutex_); + std::lock_guard lock(table_mutex_); // Release all objects. for (uint32_t n = 0; n < table_capacity_; n++) { @@ -41,11 +39,6 @@ ObjectTable::~ObjectTable() { last_free_entry_ = 0; xe_free(table_); table_ = NULL; - - xe_mutex_unlock(table_mutex_); - - xe_mutex_free(table_mutex_); - table_mutex_ = NULL; } X_STATUS ObjectTable::FindFreeSlot(uint32_t* out_slot) { @@ -91,25 +84,25 @@ X_STATUS ObjectTable::AddHandle(XObject* object, X_HANDLE* out_handle) { assert_not_null(out_handle); X_STATUS result = X_STATUS_SUCCESS; - - xe_mutex_lock(table_mutex_); - - // Find a free slot. + uint32_t slot = 0; - result = FindFreeSlot(&slot); + { + std::lock_guard lock(table_mutex_); - // Stash. - if (XSUCCEEDED(result)) { - ObjectTableEntry& entry = table_[slot]; - entry.object = object; + // Find a free slot. + result = FindFreeSlot(&slot); - // Retain so long as the object is in the table. - object->RetainHandle(); - object->Retain(); + // Stash. + if (XSUCCEEDED(result)) { + ObjectTableEntry& entry = table_[slot]; + entry.object = object; + + // Retain so long as the object is in the table. + object->RetainHandle(); + object->Retain(); + } } - xe_mutex_unlock(table_mutex_); - if (XSUCCEEDED(result)) { *out_handle = slot << 2; } @@ -125,27 +118,27 @@ X_STATUS ObjectTable::RemoveHandle(X_HANDLE handle) { return X_STATUS_INVALID_HANDLE; } - xe_mutex_lock(table_mutex_); - - // Lower 2 bits are ignored. - uint32_t slot = handle >> 2; - - // Verify slot. XObject* object = NULL; - if (slot > table_capacity_) { - result = X_STATUS_INVALID_HANDLE; - } else { - ObjectTableEntry& entry = table_[slot]; - if (entry.object) { - // Release after we lose the lock. - object = entry.object; - } else { + { + std::lock_guard lock(table_mutex_); + + // Lower 2 bits are ignored. + uint32_t slot = handle >> 2; + + // Verify slot. + if (slot > table_capacity_) { result = X_STATUS_INVALID_HANDLE; + } else { + ObjectTableEntry& entry = table_[slot]; + if (entry.object) { + // Release after we lose the lock. + object = entry.object; + } else { + result = X_STATUS_INVALID_HANDLE; + } } } - xe_mutex_unlock(table_mutex_); - if (object) { // Release the object handle now that it is out of the table. object->ReleaseHandle(); @@ -165,30 +158,31 @@ X_STATUS ObjectTable::GetObject(X_HANDLE handle, XObject** out_object) { return X_STATUS_INVALID_HANDLE; } - xe_mutex_lock(table_mutex_); - - // Lower 2 bits are ignored. - uint32_t slot = handle >> 2; - - // Verify slot. XObject* object = NULL; - if (slot > table_capacity_) { - result = X_STATUS_INVALID_HANDLE; - } else { - ObjectTableEntry& entry = table_[slot]; - if (entry.object) { - object = entry.object; - } else { + { + std::lock_guard lock(table_mutex_); + + // Lower 2 bits are ignored. + uint32_t slot = handle >> 2; + + // Verify slot. + if (slot > table_capacity_) { result = X_STATUS_INVALID_HANDLE; + } else { + ObjectTableEntry& entry = table_[slot]; + if (entry.object) { + object = entry.object; + } else { + result = X_STATUS_INVALID_HANDLE; + } } - } - // Retain the object pointer. - if (object) { - object->Retain(); - } + // Retain the object pointer. + if (object) { + object->Retain(); + } - xe_mutex_unlock(table_mutex_); + } *out_object = object; return result; diff --git a/src/xenia/kernel/object_table.h b/src/xenia/kernel/object_table.h index ddd0dc4cc..3cd5cf87e 100644 --- a/src/xenia/kernel/object_table.h +++ b/src/xenia/kernel/object_table.h @@ -10,6 +10,8 @@ #ifndef XENIA_KERNEL_XBOXKRNL_OBJECT_TABLE_H_ #define XENIA_KERNEL_XBOXKRNL_OBJECT_TABLE_H_ +#include + #include #include @@ -40,7 +42,7 @@ private: XObject* object; } ObjectTableEntry; - xe_mutex_t* table_mutex_; + std::mutex table_mutex_; uint32_t table_capacity_; ObjectTableEntry* table_; uint32_t last_free_entry_; diff --git a/src/xenia/kernel/objects/xnotify_listener.cc b/src/xenia/kernel/objects/xnotify_listener.cc index e205f6b6d..9de2224ea 100644 --- a/src/xenia/kernel/objects/xnotify_listener.cc +++ b/src/xenia/kernel/objects/xnotify_listener.cc @@ -16,12 +16,11 @@ using namespace xe::kernel; XNotifyListener::XNotifyListener(KernelState* kernel_state) : XObject(kernel_state, kTypeNotifyListener), - wait_handle_(NULL), lock_(0), mask_(0), notification_count_(0) { + wait_handle_(NULL), mask_(0), notification_count_(0) { } XNotifyListener::~XNotifyListener() { kernel_state_->UnregisterNotifyListener(this); - xe_mutex_free(lock_); if (wait_handle_) { CloseHandle(wait_handle_); } @@ -30,7 +29,6 @@ XNotifyListener::~XNotifyListener() { void XNotifyListener::Initialize(uint64_t mask) { assert_null(wait_handle_); - lock_ = xe_mutex_alloc(); wait_handle_ = CreateEvent(NULL, TRUE, FALSE, NULL); mask_ = mask; @@ -43,7 +41,7 @@ void XNotifyListener::EnqueueNotification(XNotificationID id, uint32_t data) { return; } - xe_mutex_lock(lock_); + std::lock_guard lock(lock_); if (notifications_.count(id)) { // Already exists. Overwrite. notifications_[id] = data; @@ -53,13 +51,12 @@ void XNotifyListener::EnqueueNotification(XNotificationID id, uint32_t data) { notifications_.insert({ id, data }); } SetEvent(wait_handle_); - xe_mutex_unlock(lock_); } bool XNotifyListener::DequeueNotification( XNotificationID* out_id, uint32_t* out_data) { + std::lock_guard lock(lock_); bool dequeued = false; - xe_mutex_lock(lock_); if (notification_count_) { dequeued = true; auto it = notifications_.begin(); @@ -71,14 +68,13 @@ bool XNotifyListener::DequeueNotification( ResetEvent(wait_handle_); } } - xe_mutex_unlock(lock_); return dequeued; } bool XNotifyListener::DequeueNotification( XNotificationID id, uint32_t* out_data) { + std::lock_guard lock(lock_); bool dequeued = false; - xe_mutex_lock(lock_); if (notification_count_) { dequeued = true; auto it = notifications_.find(id); @@ -91,6 +87,5 @@ bool XNotifyListener::DequeueNotification( } } } - xe_mutex_unlock(lock_); return dequeued; } diff --git a/src/xenia/kernel/objects/xnotify_listener.h b/src/xenia/kernel/objects/xnotify_listener.h index 436c4434a..7a7fc6e6a 100644 --- a/src/xenia/kernel/objects/xnotify_listener.h +++ b/src/xenia/kernel/objects/xnotify_listener.h @@ -13,6 +13,8 @@ #ifndef XENIA_KERNEL_XBOXKRNL_XNOTIFY_LISTENER_H_ #define XENIA_KERNEL_XBOXKRNL_XNOTIFY_LISTENER_H_ +#include + #include #include @@ -37,7 +39,7 @@ public: private: HANDLE wait_handle_; - xe_mutex_t* lock_; + std::mutex lock_; std::unordered_map notifications_; size_t notification_count_; uint64_t mask_; diff --git a/src/xenia/kernel/objects/xthread.cc b/src/xenia/kernel/objects/xthread.cc index d24127db8..70918dd8c 100644 --- a/src/xenia/kernel/objects/xthread.cc +++ b/src/xenia/kernel/objects/xthread.cc @@ -26,7 +26,7 @@ using namespace xe::kernel; namespace { static uint32_t next_xthread_id = 0; static thread_local XThread* current_thread_tls; - static xe_mutex_t* critical_region_ = xe_mutex_alloc(10000); + static std::mutex critical_region_; static XThread* shared_kernel_thread_ = 0; } @@ -58,7 +58,6 @@ XThread::XThread(KernelState* kernel_state, creation_params_.stack_size = 16 * 1024; } - apc_lock_ = xe_mutex_alloc(); apc_list_ = new NativeList(kernel_state->memory()); event_ = new XEvent(kernel_state); @@ -79,7 +78,6 @@ XThread::~XThread() { kernel_state_->UnregisterThread(this); delete apc_list_; - xe_mutex_free(apc_lock_); event_->Release(); @@ -421,11 +419,11 @@ void XThread::Execute() { void XThread::EnterCriticalRegion() { // Global critical region. This isn't right, but is easy. - xe_mutex_lock(critical_region_); + critical_region_.lock(); } void XThread::LeaveCriticalRegion() { - xe_mutex_unlock(critical_region_); + critical_region_.unlock(); } uint32_t XThread::RaiseIrql(uint32_t new_irql) { @@ -437,12 +435,12 @@ void XThread::LowerIrql(uint32_t new_irql) { } void XThread::LockApc() { - xe_mutex_lock(apc_lock_); + apc_lock_.lock(); } void XThread::UnlockApc() { bool needs_apc = apc_list_->HasPending(); - xe_mutex_unlock(apc_lock_); + apc_lock_.unlock(); if (needs_apc) { QueueUserAPC(reinterpret_cast(DeliverAPCs), thread_handle_, diff --git a/src/xenia/kernel/objects/xthread.h b/src/xenia/kernel/objects/xthread.h index 3b1f236aa..06c93fc73 100644 --- a/src/xenia/kernel/objects/xthread.h +++ b/src/xenia/kernel/objects/xthread.h @@ -11,6 +11,7 @@ #define XENIA_KERNEL_XBOXKRNL_XTHREAD_H_ #include +#include #include @@ -98,7 +99,7 @@ private: char* name_; std::atomic irql_; - xe_mutex_t* apc_lock_; + std::mutex apc_lock_; NativeList* apc_list_; XEvent* event_; diff --git a/src/xenia/kernel/xobject.cc b/src/xenia/kernel/xobject.cc index 5574b3454..555271832 100644 --- a/src/xenia/kernel/xobject.cc +++ b/src/xenia/kernel/xobject.cc @@ -147,16 +147,8 @@ X_STATUS XObject::WaitMultiple( return result; } -void XObject::LockType() { - xe_mutex_lock(KernelState::shared()->object_mutex_); -} - -void XObject::UnlockType() { - xe_mutex_unlock(KernelState::shared()->object_mutex_); -} - void XObject::SetNativePointer(uint32_t native_ptr) { - XObject::LockType(); + std::lock_guard lock(kernel_state_->object_mutex()); DISPATCH_HEADER* header_be = (DISPATCH_HEADER*)kernel_state_->memory()->Translate(native_ptr); @@ -173,8 +165,6 @@ void XObject::SetNativePointer(uint32_t native_ptr) { object_ptr |= 0x1; header_be->wait_list_flink = poly::byte_swap((uint32_t)(object_ptr >> 32)); header_be->wait_list_blink = poly::byte_swap((uint32_t)(object_ptr & 0xFFFFFFFF)); - - XObject::UnlockType(); } XObject* XObject::GetObject(KernelState* kernel_state, void* native_ptr, @@ -188,7 +178,7 @@ XObject* XObject::GetObject(KernelState* kernel_state, void* native_ptr, // We identify this by checking the low bit of wait_list_blink - if it's 1, // we have already put our pointer in there. - XObject::LockType(); + std::lock_guard lock(kernel_state->object_mutex()); DISPATCH_HEADER* header_be = (DISPATCH_HEADER*)native_ptr; DISPATCH_HEADER header; @@ -208,7 +198,6 @@ XObject* XObject::GetObject(KernelState* kernel_state, void* native_ptr, ((header.wait_list_blink) & ~0x1); XObject* object = reinterpret_cast(object_ptr); // TODO(benvanik): assert nothing has been changed in the struct. - XObject::UnlockType(); return object; } else { // First use, create new. @@ -252,7 +241,6 @@ XObject* XObject::GetObject(KernelState* kernel_state, void* native_ptr, case 24: // ThreadedDpcObject default: assert_always(); - XObject::UnlockType(); return NULL; } @@ -262,7 +250,6 @@ XObject* XObject::GetObject(KernelState* kernel_state, void* native_ptr, header_be->wait_list_flink = poly::byte_swap((uint32_t)(object_ptr >> 32)); header_be->wait_list_blink = poly::byte_swap((uint32_t)(object_ptr & 0xFFFFFFFF)); - XObject::UnlockType(); return object; } } diff --git a/src/xenia/kernel/xobject.h b/src/xenia/kernel/xobject.h index 9be1a632e..3f267579a 100644 --- a/src/xenia/kernel/xobject.h +++ b/src/xenia/kernel/xobject.h @@ -74,8 +74,6 @@ public: uint32_t wait_type, uint32_t wait_reason, uint32_t processor_mode, uint32_t alertable, uint64_t* opt_timeout); - static void LockType(); - static void UnlockType(); static XObject* GetObject(KernelState* kernel_state, void* native_ptr, int32_t as_type = -1);