From c1edb29d02e56627f9f7226d3c8a48328045a72c Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 7 Apr 2016 20:51:23 +0200 Subject: [PATCH] Further mutex cleanup --- src/debug_clang.cc | 12 +++--------- src/dispatcher.cc | 7 ++++--- src/terminal.cc | 37 +++++++++++-------------------------- src/terminal.h | 3 +-- 4 files changed, 19 insertions(+), 40 deletions(-) diff --git a/src/debug_clang.cc b/src/debug_clang.cc index 11585ac..9e398bd 100644 --- a/src/debug_clang.cc +++ b/src/debug_clang.cc @@ -428,24 +428,18 @@ std::string Debug::Clang::get_return_value(const boost::filesystem::path &file_p } bool Debug::Clang::is_invalid() { - bool invalid; std::unique_lock lock(event_mutex); - invalid=state==lldb::StateType::eStateInvalid; - return invalid; + return state==lldb::StateType::eStateInvalid; } bool Debug::Clang::is_stopped() { - bool stopped; std::unique_lock lock(event_mutex); - stopped=state==lldb::StateType::eStateStopped; - return stopped; + return state==lldb::StateType::eStateStopped; } bool Debug::Clang::is_running() { - bool running; std::unique_lock lock(event_mutex); - running=state==lldb::StateType::eStateRunning; - return running; + return state==lldb::StateType::eStateRunning; } void Debug::Clang::add_breakpoint(const boost::filesystem::path &file_path, int line_nr) { diff --git a/src/dispatcher.cc b/src/dispatcher.cc index 5d6cfaf..d1b6ee5 100644 --- a/src/dispatcher.cc +++ b/src/dispatcher.cc @@ -17,9 +17,10 @@ Dispatcher::~Dispatcher() { } void Dispatcher::post(std::function &&function) { - std::unique_lock lock(functions_mutex); - functions.emplace_back(function); - lock.unlock(); + { + std::unique_lock lock(functions_mutex); + functions.emplace_back(function); + } dispatcher(); } diff --git a/src/terminal.cc b/src/terminal.cc index c837535..d72196f 100644 --- a/src/terminal.cc +++ b/src/terminal.cc @@ -9,10 +9,7 @@ Terminal::InProgress::InProgress(const std::string& start_msg): stop(false) { } Terminal::InProgress::~InProgress() { - { - std::unique_lock lock(stop_mutex); - stop=true; - } + stop=true; if(wait_thread.joinable()) wait_thread.join(); } @@ -21,12 +18,7 @@ void Terminal::InProgress::start(const std::string& msg) { line_nr=Terminal::get().print(msg+"...\n"); wait_thread=std::thread([this](){ size_t c=0; - while(true) { - { - std::unique_lock lock(stop_mutex); - if(stop) - break; - } + while(!stop) { if(c%100==0) Terminal::get().async_print(line_nr-1, "."); std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -36,21 +28,15 @@ void Terminal::InProgress::start(const std::string& msg) { } void Terminal::InProgress::done(const std::string& msg) { - std::unique_lock lock(stop_mutex); - if(!stop) { - stop=true; - lock.unlock(); + bool expected=false; + if(stop.compare_exchange_strong(expected, true)) Terminal::get().async_print(line_nr-1, msg); - } } void Terminal::InProgress::cancel(const std::string& msg) { - std::unique_lock lock(stop_mutex); - if(!stop) { - stop=true; - lock.unlock(); + bool expected=false; + if(stop.compare_exchange_strong(expected, true)) Terminal::get().async_print(line_nr-1, msg); - } } Terminal::Terminal() { @@ -135,7 +121,7 @@ void Terminal::async_process(const std::string &command, const boost::filesystem auto exit_status=process->get_exit_status(); - processes_lock = std::unique_lock(processes_mutex); + processes_lock.lock(); for(auto it=processes.begin();it!=processes.end();it++) { if((*it)->get_id()==pid) { processes.erase(it); @@ -272,12 +258,11 @@ void Terminal::configure() { } void Terminal::clear() { - std::unique_lock lock(in_progresses_mutex); - for(auto &in_progress: in_progresses) { - std::unique_lock stop_lock(in_progress->stop_mutex); - in_progress->stop=true; + { + std::unique_lock lock(in_progresses_mutex); + for(auto &in_progress: in_progresses) + in_progress->stop=true; } - lock.unlock(); while(g_main_context_pending(NULL)) g_main_context_iteration(NULL, false); get_buffer()->set_text(""); diff --git a/src/terminal.h b/src/terminal.h index 2140969..9920496 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -25,8 +25,7 @@ public: void start(const std::string& msg); size_t line_nr; - bool stop; - std::mutex stop_mutex; + std::atomic stop; std::thread wait_thread; };