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

@@ -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.
}
}