From c5f114018e638c4e7d2fd3c5a127b397ff4418cb Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Wed, 9 Jul 2014 22:28:51 -0700 Subject: [PATCH] Replacing alloy::Mutex with std::mutex. --- src/alloy/backend/x64/x64_code_cache.cc | 9 +- src/alloy/backend/x64/x64_code_cache.h | 4 +- src/alloy/core.h | 1 - src/alloy/delegate.h | 25 ++--- src/alloy/mutex.h | 32 ------ src/alloy/mutex_posix.cc | 65 ------------ src/alloy/mutex_win.cc | 55 ----------- src/alloy/runtime/debugger.cc | 109 ++++++++++----------- src/alloy/runtime/debugger.h | 9 +- src/alloy/runtime/entry_table.cc | 23 ++--- src/alloy/runtime/entry_table.h | 4 +- src/alloy/runtime/function.cc | 15 +-- src/alloy/runtime/function.h | 9 +- src/alloy/runtime/module.cc | 42 ++++---- src/alloy/runtime/module.h | 5 +- src/alloy/runtime/runtime.cc | 48 +++++---- src/alloy/runtime/runtime.h | 5 +- src/alloy/sources.gypi | 3 - src/alloy/tracing/channels/file_channel.cc | 11 +-- src/alloy/tracing/channels/file_channel.h | 4 +- src/alloy/type_pool.h | 28 +++--- src/xenia/cpu/xenon_memory.cc | 22 ++--- src/xenia/logging.cc | 13 +-- 23 files changed, 170 insertions(+), 371 deletions(-) delete mode 100644 src/alloy/mutex.h delete mode 100644 src/alloy/mutex_posix.cc delete mode 100644 src/alloy/mutex_win.cc diff --git a/src/alloy/backend/x64/x64_code_cache.cc b/src/alloy/backend/x64/x64_code_cache.cc index 9d1c2ce60..057fd771a 100644 --- a/src/alloy/backend/x64/x64_code_cache.cc +++ b/src/alloy/backend/x64/x64_code_cache.cc @@ -53,11 +53,10 @@ public: X64CodeCache::X64CodeCache(size_t chunk_size) : chunk_size_(chunk_size), head_chunk_(NULL), active_chunk_(NULL) { - lock_ = AllocMutex(); } X64CodeCache::~X64CodeCache() { - LockMutex(lock_); + std::lock_guard guard(lock_); auto chunk = head_chunk_; while (chunk) { auto next = chunk->next; @@ -65,8 +64,6 @@ X64CodeCache::~X64CodeCache() { chunk = next; } head_chunk_ = NULL; - UnlockMutex(lock_); - FreeMutex(lock_); } int X64CodeCache::Initialize() { @@ -84,7 +81,7 @@ void* X64CodeCache::PlaceCode(void* machine_code, size_t code_size, // to 16b so that all offsets are aligned. code_size = XEROUNDUP(code_size, 16); - LockMutex(lock_); + lock_.lock(); if (active_chunk_) { if (active_chunk_->capacity - active_chunk_->offset < code_size) { @@ -106,7 +103,7 @@ void* X64CodeCache::PlaceCode(void* machine_code, size_t code_size, // Add entry to fn table. active_chunk_->AddTableEntry(final_address, code_size, stack_size); - UnlockMutex(lock_); + lock_.unlock(); // Copy code. xe_copy_struct(final_address, machine_code, code_size); diff --git a/src/alloy/backend/x64/x64_code_cache.h b/src/alloy/backend/x64/x64_code_cache.h index 23ba2e639..f51df94a9 100644 --- a/src/alloy/backend/x64/x64_code_cache.h +++ b/src/alloy/backend/x64/x64_code_cache.h @@ -10,6 +10,8 @@ #ifndef ALLOY_BACKEND_X64_X64_CODE_CACHE_H_ #define ALLOY_BACKEND_X64_X64_CODE_CACHE_H_ +#include + #include @@ -34,7 +36,7 @@ public: private: const static size_t DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024; - Mutex* lock_; + std::mutex lock_; size_t chunk_size_; X64CodeChunk* head_chunk_; X64CodeChunk* active_chunk_; diff --git a/src/alloy/core.h b/src/alloy/core.h index 3beb11ba4..cf8c95c44 100644 --- a/src/alloy/core.h +++ b/src/alloy/core.h @@ -15,7 +15,6 @@ #include #include -#include #include diff --git a/src/alloy/delegate.h b/src/alloy/delegate.h index 176ff4b6b..2cbfde0ac 100644 --- a/src/alloy/delegate.h +++ b/src/alloy/delegate.h @@ -11,10 +11,10 @@ #define ALLOY_DELEGATE_H_ #include +#include #include #include -#include namespace alloy { @@ -25,48 +25,37 @@ namespace alloy { template class Delegate { public: - Delegate() { - lock_ = AllocMutex(); - } - ~Delegate() { - FreeMutex(lock_); - } - typedef std::function listener_t; void AddListener(listener_t const& listener) { - LockMutex(lock_); + std::lock_guard guard(lock_); listeners_.push_back(listener); - UnlockMutex(lock_); } void RemoveListener(listener_t const& listener) { - LockMutex(lock_); + std::lock_guard guard(lock_); for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { if (it == listener) { listeners_.erase(it); break; } } - UnlockMutex(lock_); } void RemoveAllListeners() { - LockMutex(lock_); + std::lock_guard guard(lock_); listeners_.clear(); - UnlockMutex(lock_); } - void operator()(T& e) const { - LockMutex(lock_); + void operator()(T& e) { + std::lock_guard guard(lock_); for (auto &listener : listeners_) { listener(e); } - UnlockMutex(lock_); } private: - alloy::Mutex* lock_; + std::mutex lock_; std::vector listeners_; }; diff --git a/src/alloy/mutex.h b/src/alloy/mutex.h deleted file mode 100644 index 3b6ed6391..000000000 --- a/src/alloy/mutex.h +++ /dev/null @@ -1,32 +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 ALLOY_MUTEX_H_ -#define ALLOY_MUTEX_H_ - -#include - - -namespace alloy { - - -typedef struct Mutex_t Mutex; - -Mutex* AllocMutex(uint32_t spin_count = 10000); -void FreeMutex(Mutex* mutex); - -int LockMutex(Mutex* mutex); -int TryLockMutex(Mutex* mutex); -int UnlockMutex(Mutex* mutex); - - -} // namespace alloy - - -#endif // ALLOY_MUTEX_H_ diff --git a/src/alloy/mutex_posix.cc b/src/alloy/mutex_posix.cc deleted file mode 100644 index 8638e9446..000000000 --- a/src/alloy/mutex_posix.cc +++ /dev/null @@ -1,65 +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 - -using namespace alloy; - - -namespace alloy { - struct Mutex_t { - pthread_mutex_t value; - }; -} // namespace alloy - - -Mutex* alloy::AllocMutex(uint32_t spin_count) { - Mutex* mutex = (Mutex*)calloc(1, sizeof(Mutex)); - - int result = pthread_mutex_init(&mutex->value, NULL); - switch (result) { - case ENOMEM: - case EINVAL: - xe_free(mutex); - return NULL; - } - - return mutex; -} - -void alloy::FreeMutex(Mutex* mutex) { - int result = pthread_mutex_destroy(&mutex->value); - switch (result) { - case EBUSY: - case EINVAL: - break; - default: - break; - } - free(mutex); -} - -int alloy::LockMutex(Mutex* mutex) { - return pthread_mutex_lock(&mutex->value) == EINVAL ? 1 : 0; -} - -int alloy::TryLockMutex(Mutex* mutex) { - int result = pthread_mutex_trylock(&mutex->value); - switch (result) { - case EBUSY: - case EINVAL: - return 1; - default: - return 0; - } -} - -int alloy::UnlockMutex(Mutex* mutex) { - return pthread_mutex_unlock(&mutex->value) == EINVAL ? 1 : 0; -} diff --git a/src/alloy/mutex_win.cc b/src/alloy/mutex_win.cc deleted file mode 100644 index da37247cc..000000000 --- a/src/alloy/mutex_win.cc +++ /dev/null @@ -1,55 +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 - -using namespace alloy; - - -namespace alloy { - struct Mutex_t { - CRITICAL_SECTION value; - }; -} // namespace alloy - - -Mutex* alloy::AllocMutex(uint32_t spin_count) { - Mutex* mutex = (Mutex*)xe_calloc(sizeof(Mutex)); - - if (spin_count) { - XEIGNORE(InitializeCriticalSectionAndSpinCount(&mutex->value, spin_count)); - } else { - InitializeCriticalSection(&mutex->value); - } - - return mutex; -} - -void alloy::FreeMutex(Mutex* mutex) { - DeleteCriticalSection(&mutex->value); - free(mutex); -} - -int alloy::LockMutex(Mutex* mutex) { - EnterCriticalSection(&mutex->value); - return 0; -} - -int alloy::TryLockMutex(Mutex* mutex) { - if (TryEnterCriticalSection(&mutex->value) == TRUE) { - return 0; - } else { - return 1; - } -} - -int alloy::UnlockMutex(Mutex* mutex) { - LeaveCriticalSection(&mutex->value); - return 0; -} diff --git a/src/alloy/runtime/debugger.cc b/src/alloy/runtime/debugger.cc index 3dbb7b906..565a6d6cb 100644 --- a/src/alloy/runtime/debugger.cc +++ b/src/alloy/runtime/debugger.cc @@ -9,7 +9,8 @@ #include -#include +#include + #include using namespace alloy; @@ -24,34 +25,28 @@ Breakpoint::~Breakpoint() { } Debugger::Debugger(Runtime* runtime) : - runtime_(runtime) { - threads_lock_ = AllocMutex(); - breakpoints_lock_ = AllocMutex(); -} + runtime_(runtime) {} -Debugger::~Debugger() { - FreeMutex(breakpoints_lock_); - FreeMutex(threads_lock_); -} +Debugger::~Debugger() {} int Debugger::SuspendAllThreads(uint32_t timeout_ms) { + std::lock_guard guard(threads_lock_); + int result = 0; - LockMutex(threads_lock_); for (auto it = threads_.begin(); it != threads_.end(); ++it) { ThreadState* thread_state = it->second; if (thread_state->Suspend(timeout_ms)) { result = 1; } } - UnlockMutex(threads_lock_); return result; } int Debugger::ResumeThread(uint32_t thread_id) { - LockMutex(threads_lock_); + std::lock_guard guard(threads_lock_); + auto it = threads_.find(thread_id); if (it == threads_.end()) { - UnlockMutex(threads_lock_); return 1; } @@ -59,38 +54,38 @@ int Debugger::ResumeThread(uint32_t thread_id) { ThreadState* thread_state = it->second; int result = thread_state->Resume(); - UnlockMutex(threads_lock_); return result; } int Debugger::ResumeAllThreads(bool force) { + std::lock_guard guard(threads_lock_); + int result = 0; - LockMutex(threads_lock_); for (auto it = threads_.begin(); it != threads_.end(); ++it) { ThreadState* thread_state = it->second; if (thread_state->Resume(force)) { result = 1; } } - UnlockMutex(threads_lock_); return result; } void Debugger::ForEachThread(std::function callback) { - LockMutex(threads_lock_); + std::lock_guard guard(threads_lock_); + for (auto it = threads_.begin(); it != threads_.end(); ++it) { ThreadState* thread_state = it->second; callback(thread_state); } - UnlockMutex(threads_lock_); } int Debugger::AddBreakpoint(Breakpoint* breakpoint) { // Add to breakpoints map. - LockMutex(breakpoints_lock_); - breakpoints_.insert( - std::pair(breakpoint->address(), breakpoint)); - UnlockMutex(breakpoints_lock_); + { + std::lock_guard guard(breakpoints_lock_); + breakpoints_.insert( + std::pair(breakpoint->address(), breakpoint)); + } // Find all functions that contain the breakpoint address. auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address()); @@ -108,23 +103,23 @@ int Debugger::AddBreakpoint(Breakpoint* breakpoint) { int Debugger::RemoveBreakpoint(Breakpoint* breakpoint) { // Remove from breakpoint map. - LockMutex(breakpoints_lock_); - auto range = breakpoints_.equal_range(breakpoint->address()); - if (range.first == range.second) { - UnlockMutex(breakpoints_lock_); - return 1; - } - bool found = false; - for (auto it = range.first; it != range.second; ++it) { - if (it->second == breakpoint) { - breakpoints_.erase(it); - found = true; - break; + { + std::lock_guard guard(breakpoints_lock_); + auto range = breakpoints_.equal_range(breakpoint->address()); + if (range.first == range.second) { + return 1; + } + bool found = false; + for (auto it = range.first; it != range.second; ++it) { + if (it->second == breakpoint) { + breakpoints_.erase(it); + found = true; + break; + } + } + if (!found) { + return 1; } - } - UnlockMutex(breakpoints_lock_); - if (!found) { - return 1; } // Find all functions that have the breakpoint set. @@ -141,13 +136,12 @@ int Debugger::RemoveBreakpoint(Breakpoint* breakpoint) { void Debugger::FindBreakpoints( uint64_t address, std::vector& out_breakpoints) { - out_breakpoints.clear(); + std::lock_guard guard(breakpoints_lock_); - LockMutex(breakpoints_lock_); + out_breakpoints.clear(); auto range = breakpoints_.equal_range(address); if (range.first == range.second) { - UnlockMutex(breakpoints_lock_); return; } @@ -155,42 +149,39 @@ void Debugger::FindBreakpoints( Breakpoint* breakpoint = it->second; out_breakpoints.push_back(breakpoint); } - - UnlockMutex(breakpoints_lock_); } void Debugger::OnThreadCreated(ThreadState* thread_state) { - LockMutex(threads_lock_); + std::lock_guard guard(threads_lock_); threads_[thread_state->thread_id()] = thread_state; - UnlockMutex(threads_lock_); } void Debugger::OnThreadDestroyed(ThreadState* thread_state) { - LockMutex(threads_lock_); + std::lock_guard guard(threads_lock_); auto it = threads_.find(thread_state->thread_id()); if (it != threads_.end()) { threads_.erase(it); } - UnlockMutex(threads_lock_); } void Debugger::OnFunctionDefined(FunctionInfo* symbol_info, Function* function) { // Man, I'd love not to take this lock. std::vector breakpoints; - LockMutex(breakpoints_lock_); - for (uint64_t address = symbol_info->address(); - address <= symbol_info->end_address(); address += 4) { - auto range = breakpoints_.equal_range(address); - if (range.first == range.second) { - continue; - } - for (auto it = range.first; it != range.second; ++it) { - Breakpoint* breakpoint = it->second; - breakpoints.push_back(breakpoint); + { + std::lock_guard guard(breakpoints_lock_); + for (uint64_t address = symbol_info->address(); + address <= symbol_info->end_address(); address += 4) { + auto range = breakpoints_.equal_range(address); + if (range.first == range.second) { + continue; + } + for (auto it = range.first; it != range.second; ++it) { + Breakpoint* breakpoint = it->second; + breakpoints.push_back(breakpoint); + } } } - UnlockMutex(breakpoints_lock_); if (breakpoints.size()) { // Breakpoints to add! @@ -210,4 +201,4 @@ void Debugger::OnBreakpointHit( breakpoint_hit(e); // Note that we stay suspended. -} \ No newline at end of file +} diff --git a/src/alloy/runtime/debugger.h b/src/alloy/runtime/debugger.h index c61381bfe..344cb80ab 100644 --- a/src/alloy/runtime/debugger.h +++ b/src/alloy/runtime/debugger.h @@ -10,9 +10,10 @@ #ifndef ALLOY_RUNTIME_DEBUGGER_H_ #define ALLOY_RUNTIME_DEBUGGER_H_ -#include - #include +#include + +#include namespace alloy { @@ -105,11 +106,11 @@ public: private: Runtime* runtime_; - Mutex* threads_lock_; + std::mutex threads_lock_; typedef std::unordered_map ThreadMap; ThreadMap threads_; - Mutex* breakpoints_lock_; + std::mutex breakpoints_lock_; typedef std::multimap BreakpointMultimap; BreakpointMultimap breakpoints_; }; diff --git a/src/alloy/runtime/entry_table.cc b/src/alloy/runtime/entry_table.cc index ebec56ea4..5238ec6ae 100644 --- a/src/alloy/runtime/entry_table.cc +++ b/src/alloy/runtime/entry_table.cc @@ -13,23 +13,19 @@ using namespace alloy; using namespace alloy::runtime; -EntryTable::EntryTable() { - lock_ = AllocMutex(10000); -} +EntryTable::EntryTable() = default; EntryTable::~EntryTable() { - LockMutex(lock_); + std::lock_guard guard(lock_); EntryMap::iterator it = map_.begin(); for (; it != map_.end(); ++it) { Entry* entry = it->second; delete entry; } - UnlockMutex(lock_); - FreeMutex(lock_); } Entry* EntryTable::Get(uint64_t address) { - LockMutex(lock_); + std::lock_guard guard(lock_); EntryMap::const_iterator it = map_.find(address); Entry* entry = it != map_.end() ? it->second : NULL; if (entry) { @@ -38,12 +34,11 @@ Entry* EntryTable::Get(uint64_t address) { entry = NULL; } } - UnlockMutex(lock_); return entry; } Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) { - LockMutex(lock_); + lock_.lock(); EntryMap::const_iterator it = map_.find(address); Entry* entry = it != map_.end() ? it->second : NULL; Entry::Status status; @@ -52,10 +47,10 @@ Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) { if (entry->status == Entry::STATUS_COMPILING) { // Still compiling, so spin. do { - UnlockMutex(lock_); + lock_.unlock(); // TODO(benvanik): sleep for less time? Sleep(0); - LockMutex(lock_); + lock_.lock(); } while (entry->status == Entry::STATUS_COMPILING); } status = entry->status; @@ -69,16 +64,15 @@ Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) { map_[address] = entry; status = Entry::STATUS_NEW; } - UnlockMutex(lock_); + lock_.unlock(); *out_entry = entry; return status; } std::vector EntryTable::FindWithAddress(uint64_t address) { SCOPE_profile_cpu_f("alloy"); - + std::lock_guard guard(lock_); std::vector fns; - LockMutex(lock_); for (auto it = map_.begin(); it != map_.end(); ++it) { Entry* entry = it->second; if (address >= entry->address && @@ -88,6 +82,5 @@ std::vector EntryTable::FindWithAddress(uint64_t address) { } } } - UnlockMutex(lock_); return fns; } diff --git a/src/alloy/runtime/entry_table.h b/src/alloy/runtime/entry_table.h index acbabc26e..9ff7729b1 100644 --- a/src/alloy/runtime/entry_table.h +++ b/src/alloy/runtime/entry_table.h @@ -10,6 +10,8 @@ #ifndef ALLOY_RUNTIME_ENTRY_TABLE_H_ #define ALLOY_RUNTIME_ENTRY_TABLE_H_ +#include + #include @@ -46,7 +48,7 @@ public: private: // TODO(benvanik): replace with a better data structure. - Mutex* lock_; + std::mutex lock_; typedef std::unordered_map EntryMap; EntryMap map_; }; diff --git a/src/alloy/runtime/function.cc b/src/alloy/runtime/function.cc index 2dd0ddce5..a5993be99 100644 --- a/src/alloy/runtime/function.cc +++ b/src/alloy/runtime/function.cc @@ -20,16 +20,12 @@ using namespace alloy::runtime; Function::Function(FunctionInfo* symbol_info) : address_(symbol_info->address()), symbol_info_(symbol_info), debug_info_(0) { - // TODO(benvanik): create on demand? - lock_ = AllocMutex(); } -Function::~Function() { - FreeMutex(lock_); -} +Function::~Function() = default; int Function::AddBreakpoint(Breakpoint* breakpoint) { - LockMutex(lock_); + std::lock_guard guard(lock_); bool found = false; for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { if (*it == breakpoint) { @@ -40,12 +36,11 @@ int Function::AddBreakpoint(Breakpoint* breakpoint) { breakpoints_.push_back(breakpoint); AddBreakpointImpl(breakpoint); } - UnlockMutex(lock_); return found ? 1 : 0; } int Function::RemoveBreakpoint(Breakpoint* breakpoint) { - LockMutex(lock_); + std::lock_guard guard(lock_); bool found = false; for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { if (*it == breakpoint) { @@ -55,12 +50,11 @@ int Function::RemoveBreakpoint(Breakpoint* breakpoint) { break; } } - UnlockMutex(lock_); return found ? 0 : 1; } Breakpoint* Function::FindBreakpoint(uint64_t address) { - LockMutex(lock_); + std::lock_guard guard(lock_); Breakpoint* result = NULL; for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { Breakpoint* breakpoint = *it; @@ -69,7 +63,6 @@ Breakpoint* Function::FindBreakpoint(uint64_t address) { break; } } - UnlockMutex(lock_); return result; } diff --git a/src/alloy/runtime/function.h b/src/alloy/runtime/function.h index 22f4df0aa..e34ff5119 100644 --- a/src/alloy/runtime/function.h +++ b/src/alloy/runtime/function.h @@ -10,6 +10,9 @@ #ifndef ALLOY_RUNTIME_FUNCTION_H_ #define ALLOY_RUNTIME_FUNCTION_H_ +#include +#include + #include #include @@ -46,12 +49,12 @@ protected: uint64_t return_address) = 0; protected: - uint64_t address_; + uint64_t address_; FunctionInfo* symbol_info_; - DebugInfo* debug_info_; + DebugInfo* debug_info_; // TODO(benvanik): move elsewhere? DebugData? - Mutex* lock_; + std::mutex lock_; std::vector breakpoints_; }; diff --git a/src/alloy/runtime/module.cc b/src/alloy/runtime/module.cc index 5e38c3902..4cd4550c9 100644 --- a/src/alloy/runtime/module.cc +++ b/src/alloy/runtime/module.cc @@ -19,19 +19,15 @@ using namespace alloy::runtime; Module::Module(Runtime* runtime) : - runtime_(runtime), memory_(runtime->memory()) { - lock_ = AllocMutex(10000); -} + runtime_(runtime), memory_(runtime->memory()) {} Module::~Module() { - LockMutex(lock_); + std::lock_guard guard(lock_); SymbolMap::iterator it = map_.begin(); for (; it != map_.end(); ++it) { SymbolInfo* symbol_info = it->second; delete symbol_info; } - UnlockMutex(lock_); - FreeMutex(lock_); } bool Module::ContainsAddress(uint64_t address) { @@ -39,7 +35,7 @@ bool Module::ContainsAddress(uint64_t address) { } SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { - LockMutex(lock_); + lock_.lock(); SymbolMap::const_iterator it = map_.find(address); SymbolInfo* symbol_info = it != map_.end() ? it->second : NULL; if (symbol_info) { @@ -47,10 +43,10 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { // Some other thread is declaring the symbol - wait. if (wait) { do { - UnlockMutex(lock_); + lock_.unlock(); // TODO(benvanik): sleep for less time? Sleep(0); - LockMutex(lock_); + lock_.lock(); } while (symbol_info->status() == SymbolInfo::STATUS_DECLARING); } else { // Immediate request, just return. @@ -58,31 +54,31 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { } } } - UnlockMutex(lock_); + lock_.unlock(); return symbol_info; } SymbolInfo::Status Module::DeclareSymbol( SymbolInfo::Type type, uint64_t address, SymbolInfo** out_symbol_info) { *out_symbol_info = NULL; - LockMutex(lock_); + lock_.lock(); SymbolMap::const_iterator it = map_.find(address); SymbolInfo* symbol_info = it != map_.end() ? it->second : NULL; SymbolInfo::Status status; if (symbol_info) { // If we exist but are the wrong type, die. if (symbol_info->type() != type) { - UnlockMutex(lock_); + lock_.unlock(); return SymbolInfo::STATUS_FAILED; } // If we aren't ready yet spin and wait. if (symbol_info->status() == SymbolInfo::STATUS_DECLARING) { // Still declaring, so spin. do { - UnlockMutex(lock_); + lock_.unlock(); // TODO(benvanik): sleep for less time? Sleep(0); - LockMutex(lock_); + lock_.lock(); } while (symbol_info->status() == SymbolInfo::STATUS_DECLARING); } status = symbol_info->status(); @@ -100,7 +96,7 @@ SymbolInfo::Status Module::DeclareSymbol( list_.push_back(symbol_info); status = SymbolInfo::STATUS_NEW; } - UnlockMutex(lock_); + lock_.unlock(); *out_symbol_info = symbol_info; // Get debug info from providers, if this is new. @@ -130,7 +126,7 @@ SymbolInfo::Status Module::DeclareVariable( } SymbolInfo::Status Module::DefineSymbol(SymbolInfo* symbol_info) { - LockMutex(lock_); + lock_.lock(); SymbolInfo::Status status; if (symbol_info->status() == SymbolInfo::STATUS_DECLARED) { // Declared but undefined, so request caller define it. @@ -139,16 +135,16 @@ SymbolInfo::Status Module::DefineSymbol(SymbolInfo* symbol_info) { } else if (symbol_info->status() == SymbolInfo::STATUS_DEFINING) { // Still defining, so spin. do { - UnlockMutex(lock_); + lock_.unlock(); // TODO(benvanik): sleep for less time? Sleep(0); - LockMutex(lock_); + lock_.lock(); } while (symbol_info->status() == SymbolInfo::STATUS_DEFINING); status = symbol_info->status(); } else { status = symbol_info->status(); } - UnlockMutex(lock_); + lock_.unlock(); return status; } @@ -162,8 +158,7 @@ SymbolInfo::Status Module::DefineVariable(VariableInfo* symbol_info) { void Module::ForEachFunction(std::function callback) { SCOPE_profile_cpu_f("alloy"); - - LockMutex(lock_); + std::lock_guard guard(lock_); for (auto it = list_.begin(); it != list_.end(); ++it) { SymbolInfo* symbol_info = *it; if (symbol_info->type() == SymbolInfo::TYPE_FUNCTION) { @@ -171,14 +166,12 @@ void Module::ForEachFunction(std::function callback) { callback(info); } } - UnlockMutex(lock_); } void Module::ForEachFunction(size_t since, size_t& version, std::function callback) { SCOPE_profile_cpu_f("alloy"); - - LockMutex(lock_); + std::lock_guard guard(lock_); size_t count = list_.size(); version = count; for (size_t n = since; n < count; n++) { @@ -188,7 +181,6 @@ void Module::ForEachFunction(size_t since, size_t& version, callback(info); } } - UnlockMutex(lock_); } int Module::ReadMap(const char* file_name) { diff --git a/src/alloy/runtime/module.h b/src/alloy/runtime/module.h index c05e009ca..63ca706fe 100644 --- a/src/alloy/runtime/module.h +++ b/src/alloy/runtime/module.h @@ -11,6 +11,9 @@ #define ALLOY_RUNTIME_MODULE_H_ #include +#include +#include +#include #include #include @@ -61,7 +64,7 @@ protected: private: // TODO(benvanik): replace with a better data structure. - Mutex* lock_; + std::mutex lock_; typedef std::unordered_map SymbolMap; SymbolMap map_; typedef std::vector SymbolList; diff --git a/src/alloy/runtime/runtime.cc b/src/alloy/runtime/runtime.cc index 7be29dce1..cf9cce22c 100644 --- a/src/alloy/runtime/runtime.cc +++ b/src/alloy/runtime/runtime.cc @@ -27,18 +27,17 @@ DEFINE_string(runtime_backend, "any", Runtime::Runtime(Memory* memory) : memory_(memory), debugger_(0), backend_(0), frontend_(0) { tracing::Initialize(); - modules_lock_ = AllocMutex(10000); } Runtime::~Runtime() { - LockMutex(modules_lock_); - for (ModuleList::iterator it = modules_.begin(); - it != modules_.end(); ++it) { - Module* module = *it; - delete module; + { + std::lock_guard guard(modules_lock_); + for (ModuleList::iterator it = modules_.begin(); + it != modules_.end(); ++it) { + Module* module = *it; + delete module; + } } - UnlockMutex(modules_lock_); - FreeMutex(modules_lock_); delete frontend_; delete backend_; @@ -111,15 +110,14 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) { } int Runtime::AddModule(Module* module) { - LockMutex(modules_lock_); + std::lock_guard guard(modules_lock_); modules_.push_back(module); - UnlockMutex(modules_lock_); return 0; } Module* Runtime::GetModule(const char* name) { + std::lock_guard guard(modules_lock_); Module* result = NULL; - LockMutex(modules_lock_); for (ModuleList::iterator it = modules_.begin(); it != modules_.end(); ++it) { Module* module = *it; @@ -128,15 +126,12 @@ Module* Runtime::GetModule(const char* name) { break; } } - UnlockMutex(modules_lock_); return result; } Runtime::ModuleList Runtime::GetModules() { - ModuleList clone; - LockMutex(modules_lock_); - clone = modules_; - UnlockMutex(modules_lock_); + std::lock_guard guard(modules_lock_); + ModuleList clone = modules_; return clone; } @@ -188,18 +183,19 @@ int Runtime::LookupFunctionInfo( // Find the module that contains the address. Module* code_module = NULL; - LockMutex(modules_lock_); - // TODO(benvanik): sort by code address (if contiguous) so can bsearch. - // TODO(benvanik): cache last module low/high, as likely to be in there. - for (ModuleList::const_iterator it = modules_.begin(); - it != modules_.end(); ++it) { - Module* module = *it; - if (module->ContainsAddress(address)) { - code_module = module; - break; + { + std::lock_guard guard(modules_lock_); + // TODO(benvanik): sort by code address (if contiguous) so can bsearch. + // TODO(benvanik): cache last module low/high, as likely to be in there. + for (ModuleList::const_iterator it = modules_.begin(); + it != modules_.end(); ++it) { + Module* module = *it; + if (module->ContainsAddress(address)) { + code_module = module; + break; + } } } - UnlockMutex(modules_lock_); if (!code_module) { // No module found that could contain the address. return 1; diff --git a/src/alloy/runtime/runtime.h b/src/alloy/runtime/runtime.h index a6c506fc5..68d4f488d 100644 --- a/src/alloy/runtime/runtime.h +++ b/src/alloy/runtime/runtime.h @@ -10,6 +10,9 @@ #ifndef ALLOY_RUNTIME_RUNTIME_H_ #define ALLOY_RUNTIME_RUNTIME_H_ +#include +#include + #include #include #include @@ -65,7 +68,7 @@ protected: backend::Backend* backend_; EntryTable entry_table_; - Mutex* modules_lock_; + std::mutex modules_lock_; ModuleList modules_; }; diff --git a/src/alloy/sources.gypi b/src/alloy/sources.gypi index 808af15b8..89bca0b1d 100644 --- a/src/alloy/sources.gypi +++ b/src/alloy/sources.gypi @@ -10,7 +10,6 @@ 'delegate.h', 'memory.cc', 'memory.h', - 'mutex.h', 'string_buffer.cc', 'string_buffer.h', 'type_pool.h', @@ -19,7 +18,6 @@ 'conditions': [ ['OS == "mac" or OS == "linux"', { 'sources': [ - 'mutex_posix.cc', ], }], ['OS == "linux"', { @@ -32,7 +30,6 @@ }], ['OS == "win"', { 'sources': [ - 'mutex_win.cc', ], }], ], diff --git a/src/alloy/tracing/channels/file_channel.cc b/src/alloy/tracing/channels/file_channel.cc index 115f79387..7b2db652b 100644 --- a/src/alloy/tracing/channels/file_channel.cc +++ b/src/alloy/tracing/channels/file_channel.cc @@ -15,37 +15,32 @@ using namespace alloy::tracing::channels; FileChannel::FileChannel(const char* path) { - lock_ = AllocMutex(10000); path_ = xestrdupa(path); file_ = fopen(path, "wb"); } FileChannel::~FileChannel() { - LockMutex(lock_); + std::lock_guard guard(lock_); fclose(file_); file_ = 0; free(path_); path_ = 0; - UnlockMutex(lock_); - FreeMutex(lock_); } void FileChannel::Write( size_t buffer_count, size_t buffer_lengths[], const uint8_t* buffers[]) { - LockMutex(lock_); + std::lock_guard guard(lock_); if (file_) { for (size_t n = 0; n < buffer_count; n++) { fwrite(buffers[n], buffer_lengths[n], 1, file_); } } - UnlockMutex(lock_); } void FileChannel::Flush() { - LockMutex(lock_); + std::lock_guard guard(lock_); if (file_) { fflush(file_); } - UnlockMutex(lock_); } diff --git a/src/alloy/tracing/channels/file_channel.h b/src/alloy/tracing/channels/file_channel.h index 9f57fc4af..55e8e71fb 100644 --- a/src/alloy/tracing/channels/file_channel.h +++ b/src/alloy/tracing/channels/file_channel.h @@ -10,6 +10,8 @@ #ifndef ALLOY_TRACING_CHANNELS_FILE_CHANNEL_H_ #define ALLOY_TRACING_CHANNELS_FILE_CHANNEL_H_ +#include + #include #include @@ -34,7 +36,7 @@ public: private: char* path_; FILE* file_; - Mutex* lock_; + std::mutex lock_; }; diff --git a/src/alloy/type_pool.h b/src/alloy/type_pool.h index e88026399..beb9dbd43 100644 --- a/src/alloy/type_pool.h +++ b/src/alloy/type_pool.h @@ -10,6 +10,8 @@ #ifndef ALLOY_TYPE_POOL_H_ #define ALLOY_TYPE_POOL_H_ +#include + #include @@ -19,33 +21,28 @@ namespace alloy { template class TypePool { public: - TypePool() { - lock_ = AllocMutex(10000); - } - ~TypePool() { Reset(); - FreeMutex(lock_); } void Reset() { - LockMutex(lock_); + std::lock_guard guard(lock_); for (TList::iterator it = list_.begin(); it != list_.end(); ++it) { T* value = *it; delete value; } list_.clear(); - UnlockMutex(lock_); } T* Allocate(A arg0) { T* result = 0; - LockMutex(lock_); - if (list_.size()) { - result = list_.back(); - list_.pop_back(); + { + std::lock_guard guard(lock_); + if (list_.size()) { + result = list_.back(); + list_.pop_back(); + } } - UnlockMutex(lock_); if (!result) { result = new T(arg0); } @@ -53,15 +50,14 @@ public: } void Release(T* value) { - LockMutex(lock_); + std::lock_guard guard(lock_); list_.push_back(value); - UnlockMutex(lock_); } private: - Mutex* lock_; + std::mutex lock_; typedef std::vector TList; - TList list_; + TList list_; }; diff --git a/src/xenia/cpu/xenon_memory.cc b/src/xenia/cpu/xenon_memory.cc index 8e3bf6dc5..c20130fae 100644 --- a/src/xenia/cpu/xenon_memory.cc +++ b/src/xenia/cpu/xenon_memory.cc @@ -9,6 +9,8 @@ #include +#include + #include #include @@ -112,7 +114,7 @@ private: XenonMemory* memory_; uint32_t heap_id_; bool is_physical_; - Mutex* lock_; + std::mutex lock_; size_t size_; uint8_t* ptr_; mspace space_; @@ -615,19 +617,13 @@ uint32_t XenonMemory::QueryProtect(uint64_t address) { XenonMemoryHeap::XenonMemoryHeap(XenonMemory* memory, bool is_physical) : memory_(memory), is_physical_(is_physical) { heap_id_ = next_heap_id_++; - lock_ = AllocMutex(10000); } XenonMemoryHeap::~XenonMemoryHeap() { - if (lock_ && space_) { - LockMutex(lock_); + if (space_) { + std::lock_guard guard(lock_); destroy_mspace(space_); space_ = NULL; - UnlockMutex(lock_); - } - if (lock_) { - FreeMutex(lock_); - lock_ = NULL; } if (ptr_) { @@ -661,7 +657,7 @@ int XenonMemoryHeap::Initialize(uint64_t low, uint64_t high) { uint64_t XenonMemoryHeap::Alloc( uint64_t base_address, size_t size, uint32_t flags, uint32_t alignment) { - XEIGNORE(LockMutex(lock_)); + lock_.lock(); size_t alloc_size = size; size_t heap_guard_size = FLAGS_heap_guard_pages * 4096; if (heap_guard_size) { @@ -686,7 +682,7 @@ uint64_t XenonMemoryHeap::Alloc( if (FLAGS_log_heap) { Dump(); } - XEIGNORE(UnlockMutex(lock_)); + lock_.unlock(); if (!p) { return 0; } @@ -747,7 +743,7 @@ uint64_t XenonMemoryHeap::Free(uint64_t address, size_t size) { memset(p + heap_guard_size, 0xDC, size); } - XEIGNORE(LockMutex(lock_)); + lock_.lock(); if (FLAGS_heap_guard_pages) { DWORD old_protect; VirtualProtect( @@ -761,7 +757,7 @@ uint64_t XenonMemoryHeap::Free(uint64_t address, size_t size) { if (FLAGS_log_heap) { Dump(); } - XEIGNORE(UnlockMutex(lock_)); + lock_.unlock(); if (is_physical_) { // If physical, decommit from physical ranges too. diff --git a/src/xenia/logging.cc b/src/xenia/logging.cc index 3f9d840f9..77106bc54 100644 --- a/src/xenia/logging.cc +++ b/src/xenia/logging.cc @@ -9,8 +9,9 @@ #include +#include + #include -#include #include @@ -20,7 +21,7 @@ DEFINE_bool(fast_stdout, false, namespace { -xe_mutex_t* log_lock = xe_mutex_alloc(); +std::mutex log_lock; } // namespace @@ -69,7 +70,7 @@ void xe_log_line(const char* file_path, const uint32_t line_number, va_end(args); if (!FLAGS_fast_stdout) { - xe_mutex_lock(log_lock); + log_lock.lock(); } #if 0// defined(OutputDebugString) OutputDebugStringA(buffer); @@ -78,7 +79,7 @@ void xe_log_line(const char* file_path, const uint32_t line_number, fflush(stdout); #endif // OutputDebugString if (!FLAGS_fast_stdout) { - xe_mutex_unlock(log_lock); + log_lock.unlock(); } } @@ -94,7 +95,7 @@ void xe_handle_fatal( va_end(args); if (!FLAGS_fast_stdout) { - xe_mutex_lock(log_lock); + log_lock.lock(); } #if defined(OutputDebugString) OutputDebugStringA(buffer); @@ -103,7 +104,7 @@ void xe_handle_fatal( fflush(stderr); #endif // OutputDebugString if (!FLAGS_fast_stdout) { - xe_mutex_unlock(log_lock); + log_lock.unlock(); } #if XE_LIKE_WIN32