Replacing alloy::Mutex with std::mutex.

This commit is contained in:
Ben Vanik 2014-07-09 22:28:51 -07:00
parent 500647968c
commit c5f114018e
23 changed files with 170 additions and 371 deletions

View File

@ -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<std::mutex> 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);

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_BACKEND_X64_X64_CODE_CACHE_H_
#define ALLOY_BACKEND_X64_X64_CODE_CACHE_H_
#include <mutex>
#include <alloy/core.h>
@ -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_;

View File

@ -15,7 +15,6 @@
#include <alloy/arena.h>
#include <alloy/delegate.h>
#include <alloy/mutex.h>
#include <alloy/string_buffer.h>

View File

@ -11,10 +11,10 @@
#define ALLOY_DELEGATE_H_
#include <functional>
#include <mutex>
#include <vector>
#include <alloy/core.h>
#include <alloy/mutex.h>
namespace alloy {
@ -25,48 +25,37 @@ namespace alloy {
template <typename T>
class Delegate {
public:
Delegate() {
lock_ = AllocMutex();
}
~Delegate() {
FreeMutex(lock_);
}
typedef std::function<void(T&)> listener_t;
void AddListener(listener_t const& listener) {
LockMutex(lock_);
std::lock_guard<std::mutex> guard(lock_);
listeners_.push_back(listener);
UnlockMutex(lock_);
}
void RemoveListener(listener_t const& listener) {
LockMutex(lock_);
std::lock_guard<std::mutex> 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<std::mutex> guard(lock_);
listeners_.clear();
UnlockMutex(lock_);
}
void operator()(T& e) const {
LockMutex(lock_);
void operator()(T& e) {
std::lock_guard<std::mutex> guard(lock_);
for (auto &listener : listeners_) {
listener(e);
}
UnlockMutex(lock_);
}
private:
alloy::Mutex* lock_;
std::mutex lock_;
std::vector<listener_t> listeners_;
};

View File

@ -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 <xenia/common.h>
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_

View File

@ -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 <alloy/mutex.h>
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;
}

View File

@ -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 <alloy/mutex.h>
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;
}

View File

@ -9,7 +9,8 @@
#include <alloy/runtime/debugger.h>
#include <alloy/mutex.h>
#include <mutex>
#include <alloy/runtime/runtime.h>
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<std::mutex> 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<std::mutex> 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<std::mutex> 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<void(ThreadState*)> callback) {
LockMutex(threads_lock_);
std::lock_guard<std::mutex> 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<uint64_t, Breakpoint*>(breakpoint->address(), breakpoint));
UnlockMutex(breakpoints_lock_);
{
std::lock_guard<std::mutex> guard(breakpoints_lock_);
breakpoints_.insert(
std::pair<uint64_t, Breakpoint*>(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<std::mutex> 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<Breakpoint*>& out_breakpoints) {
out_breakpoints.clear();
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<Breakpoint*> 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<std::mutex> 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.
}
}

View File

@ -10,9 +10,10 @@
#ifndef ALLOY_RUNTIME_DEBUGGER_H_
#define ALLOY_RUNTIME_DEBUGGER_H_
#include <alloy/core.h>
#include <map>
#include <mutex>
#include <alloy/core.h>
namespace alloy {
@ -105,11 +106,11 @@ public:
private:
Runtime* runtime_;
Mutex* threads_lock_;
std::mutex threads_lock_;
typedef std::unordered_map<uint32_t, ThreadState*> ThreadMap;
ThreadMap threads_;
Mutex* breakpoints_lock_;
std::mutex breakpoints_lock_;
typedef std::multimap<uint64_t, Breakpoint*> BreakpointMultimap;
BreakpointMultimap breakpoints_;
};

View File

@ -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<std::mutex> 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<std::mutex> 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<Function*> EntryTable::FindWithAddress(uint64_t address) {
SCOPE_profile_cpu_f("alloy");
std::lock_guard<std::mutex> guard(lock_);
std::vector<Function*> 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<Function*> EntryTable::FindWithAddress(uint64_t address) {
}
}
}
UnlockMutex(lock_);
return fns;
}

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_RUNTIME_ENTRY_TABLE_H_
#define ALLOY_RUNTIME_ENTRY_TABLE_H_
#include <mutex>
#include <alloy/core.h>
@ -46,7 +48,7 @@ public:
private:
// TODO(benvanik): replace with a better data structure.
Mutex* lock_;
std::mutex lock_;
typedef std::unordered_map<uint64_t, Entry*> EntryMap;
EntryMap map_;
};

View File

@ -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<std::mutex> 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<std::mutex> 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<std::mutex> 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;
}

View File

@ -10,6 +10,9 @@
#ifndef ALLOY_RUNTIME_FUNCTION_H_
#define ALLOY_RUNTIME_FUNCTION_H_
#include <mutex>
#include <vector>
#include <alloy/core.h>
#include <alloy/runtime/debug_info.h>
@ -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<Breakpoint*> breakpoints_;
};

View File

@ -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<std::mutex> 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<void (FunctionInfo*)> callback) {
SCOPE_profile_cpu_f("alloy");
LockMutex(lock_);
std::lock_guard<std::mutex> 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<void (FunctionInfo*)> callback) {
callback(info);
}
}
UnlockMutex(lock_);
}
void Module::ForEachFunction(size_t since, size_t& version,
std::function<void (FunctionInfo*)> callback) {
SCOPE_profile_cpu_f("alloy");
LockMutex(lock_);
std::lock_guard<std::mutex> 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) {

View File

@ -11,6 +11,9 @@
#define ALLOY_RUNTIME_MODULE_H_
#include <functional>
#include <mutex>
#include <unordered_map>
#include <vector>
#include <alloy/core.h>
#include <alloy/memory.h>
@ -61,7 +64,7 @@ protected:
private:
// TODO(benvanik): replace with a better data structure.
Mutex* lock_;
std::mutex lock_;
typedef std::unordered_map<uint64_t, SymbolInfo*> SymbolMap;
SymbolMap map_;
typedef std::vector<SymbolInfo*> SymbolList;

View File

@ -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<std::mutex> 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<std::mutex> guard(modules_lock_);
modules_.push_back(module);
UnlockMutex(modules_lock_);
return 0;
}
Module* Runtime::GetModule(const char* name) {
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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;

View File

@ -10,6 +10,9 @@
#ifndef ALLOY_RUNTIME_RUNTIME_H_
#define ALLOY_RUNTIME_RUNTIME_H_
#include <mutex>
#include <vector>
#include <alloy/core.h>
#include <alloy/memory.h>
#include <alloy/backend/backend.h>
@ -65,7 +68,7 @@ protected:
backend::Backend* backend_;
EntryTable entry_table_;
Mutex* modules_lock_;
std::mutex modules_lock_;
ModuleList modules_;
};

View File

@ -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',
],
}],
],

View File

@ -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<std::mutex> 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<std::mutex> 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<std::mutex> guard(lock_);
if (file_) {
fflush(file_);
}
UnlockMutex(lock_);
}

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_TRACING_CHANNELS_FILE_CHANNEL_H_
#define ALLOY_TRACING_CHANNELS_FILE_CHANNEL_H_
#include <mutex>
#include <alloy/core.h>
#include <alloy/tracing/channel.h>
@ -34,7 +36,7 @@ public:
private:
char* path_;
FILE* file_;
Mutex* lock_;
std::mutex lock_;
};

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_TYPE_POOL_H_
#define ALLOY_TYPE_POOL_H_
#include <mutex>
#include <alloy/core.h>
@ -19,33 +21,28 @@ namespace alloy {
template<class T, typename A>
class TypePool {
public:
TypePool() {
lock_ = AllocMutex(10000);
}
~TypePool() {
Reset();
FreeMutex(lock_);
}
void Reset() {
LockMutex(lock_);
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> guard(lock_);
list_.push_back(value);
UnlockMutex(lock_);
}
private:
Mutex* lock_;
std::mutex lock_;
typedef std::vector<T*> TList;
TList list_;
TList list_;
};

View File

@ -9,6 +9,8 @@
#include <xenia/cpu/xenon_memory.h>
#include <mutex>
#include <alloy/runtime/tracing.h>
#include <gflags/gflags.h>
@ -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<std::mutex> 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.

View File

@ -9,8 +9,9 @@
#include <xenia/logging.h>
#include <mutex>
#include <xenia/common.h>
#include <xenia/core/mutex.h>
#include <gflags/gflags.h>
@ -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