diff --git a/src/debug_lldb.cc b/src/debug_lldb.cc index 2469897..f7340d9 100644 --- a/src/debug_lldb.cc +++ b/src/debug_lldb.cc @@ -250,13 +250,13 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat } void Debug::LLDB::continue_debug() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) process->Continue(); } void Debug::LLDB::stop() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateRunning) { auto error = process->Stop(); if(error.Fail()) @@ -265,7 +265,7 @@ void Debug::LLDB::stop() { } void Debug::LLDB::kill() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(process) { auto error = process->Kill(); if(error.Fail()) @@ -274,21 +274,21 @@ void Debug::LLDB::kill() { } void Debug::LLDB::step_over() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { process->GetSelectedThread().StepOver(); } } void Debug::LLDB::step_into() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { process->GetSelectedThread().StepInto(); } } void Debug::LLDB::step_out() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { process->GetSelectedThread().StepOut(); } @@ -296,7 +296,7 @@ void Debug::LLDB::step_out() { std::pair Debug::LLDB::run_command(const std::string &command) { std::pair command_return; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped || state == lldb::StateType::eStateRunning) { lldb::SBCommandReturnObject command_return_object; debugger->GetCommandInterpreter().HandleCommand(command.c_str(), command_return_object, true); @@ -312,7 +312,7 @@ std::pair Debug::LLDB::run_command(const std::string & std::vector Debug::LLDB::get_backtrace() { std::vector backtrace; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { auto thread = process->GetSelectedThread(); for(uint32_t c_f = 0; c_f < thread.GetNumFrames(); c_f++) { @@ -348,7 +348,7 @@ std::vector Debug::LLDB::get_backtrace() { std::vector Debug::LLDB::get_variables() { std::vector variables; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { for(uint32_t c_t = 0; c_t < process->GetNumThreads(); c_t++) { auto thread = process->GetThreadAtIndex(c_t); @@ -402,7 +402,7 @@ std::vector Debug::LLDB::get_variables() { } void Debug::LLDB::select_frame(uint32_t frame_index, uint32_t thread_index_id) { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { if(thread_index_id != 0) process->SetSelectedThreadByIndexID(thread_index_id); @@ -419,7 +419,7 @@ void Debug::LLDB::cancel() { std::string Debug::LLDB::get_value(const std::string &variable, const boost::filesystem::path &file_path, unsigned int line_nr, unsigned int line_index) { std::string variable_value; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { auto frame = process->GetSelectedThread().GetSelectedFrame(); @@ -462,7 +462,7 @@ std::string Debug::LLDB::get_value(const std::string &variable, const boost::fil std::string Debug::LLDB::get_return_value(const boost::filesystem::path &file_path, unsigned int line_nr, unsigned int line_index) { std::string return_value; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateStopped) { auto thread = process->GetSelectedThread(); auto thread_return_value = thread.GetStopReturnValue(); @@ -484,22 +484,22 @@ std::string Debug::LLDB::get_return_value(const boost::filesystem::path &file_pa } bool Debug::LLDB::is_invalid() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); return state == lldb::StateType::eStateInvalid; } bool Debug::LLDB::is_stopped() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); return state == lldb::StateType::eStateStopped; } bool Debug::LLDB::is_running() { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); return state == lldb::StateType::eStateRunning; } void Debug::LLDB::add_breakpoint(const boost::filesystem::path &file_path, int line_nr) { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::eStateStopped || state == lldb::eStateRunning) { if(!(process->GetTarget().BreakpointCreateByLocation(file_path.string().c_str(), line_nr)).IsValid()) Terminal::get().async_print("Error (debug): Could not create breakpoint at: " + file_path.string() + ":" + std::to_string(line_nr) + '\n', true); @@ -507,7 +507,7 @@ void Debug::LLDB::add_breakpoint(const boost::filesystem::path &file_path, int l } void Debug::LLDB::remove_breakpoint(const boost::filesystem::path &file_path, int line_nr, int line_count) { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::eStateStopped || state == lldb::eStateRunning) { auto target = process->GetTarget(); for(int line_nr_try = line_nr; line_nr_try < line_count; line_nr_try++) { @@ -532,7 +532,7 @@ void Debug::LLDB::remove_breakpoint(const boost::filesystem::path &file_path, in } void Debug::LLDB::write(const std::string &buffer) { - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(state == lldb::StateType::eStateRunning) { process->PutSTDIN(buffer.c_str(), buffer.size()); } diff --git a/src/dispatcher.cc b/src/dispatcher.cc index 17f99c0..f4f78f6 100644 --- a/src/dispatcher.cc +++ b/src/dispatcher.cc @@ -5,7 +5,7 @@ Dispatcher::Dispatcher() { connection = dispatcher.connect([this] { std::vector>::iterator> its; { - std::unique_lock lock(functions_mutex); + std::lock_guard lock(functions_mutex); if(functions.empty()) return; its.reserve(functions.size()); @@ -15,7 +15,7 @@ Dispatcher::Dispatcher() { for(auto &it : its) (*it)(); { - std::unique_lock lock(functions_mutex); + std::lock_guard lock(functions_mutex); for(auto &it : its) functions.erase(it); } diff --git a/src/dispatcher.h b/src/dispatcher.h index cdcc7cd..7f735f4 100644 --- a/src/dispatcher.h +++ b/src/dispatcher.h @@ -18,7 +18,7 @@ public: template void post(T &&function) { { - std::unique_lock lock(functions_mutex); + std::lock_guard lock(functions_mutex); functions.emplace_back(std::forward(function)); } dispatcher(); diff --git a/src/git.cc b/src/git.cc index 9da2d7b..b6fe6c9 100644 --- a/src/git.cc +++ b/src/git.cc @@ -182,7 +182,7 @@ int Git::Repository::status_callback(const char *path, unsigned int status_flags Git::Repository::Status Git::Repository::get_status() { { - std::unique_lock lock(saved_status_mutex); + std::lock_guard lock(saved_status_mutex); if(has_saved_status) return saved_status; } @@ -217,7 +217,7 @@ Git::Repository::Status Git::Repository::get_status() { } void Git::Repository::clear_saved_status() { - std::unique_lock lock(saved_status_mutex); + std::lock_guard lock(saved_status_mutex); saved_status.added.clear(); saved_status.modified.clear(); has_saved_status = false; diff --git a/src/project.cc b/src/project.cc index 2e40e61..f50eb89 100644 --- a/src/project.cc +++ b/src/project.cc @@ -409,7 +409,7 @@ void Project::LLDB::debug_start() { boost::filesystem::path stop_path; unsigned stop_line = 0, stop_column = 0; - std::unique_lock lock(Debug::LLDB::get().mutex); + std::lock_guard lock(Debug::LLDB::get().mutex); auto process = lldb::SBProcess::GetProcessFromEvent(event); auto state = lldb::SBProcess::GetStateFromEvent(event); lldb::SBStream stream; diff --git a/src/source_clang.cc b/src/source_clang.cc index 1e4668f..3ddf709 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -550,7 +550,7 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa std::smatch sm; if(std::regex_match(line, sm, regex)) { { - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); autocomplete.prefix = sm.length(2) ? sm[3].str() : sm.length(4) ? sm[5].str() : sm[6].str(); if(!sm.length(2) && !sm.length(4)) enable_snippets = true; @@ -559,7 +559,7 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa } else if(is_possible_argument()) { show_parameters = true; - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); autocomplete.prefix = ""; return true; } @@ -572,7 +572,7 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa iter.forward_char(); { - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); autocomplete.prefix = get_buffer()->get_text(iter, end_iter); } auto prev1 = iter; diff --git a/src/source_diff.cc b/src/source_diff.cc index d3c2594..ee7cc6f 100644 --- a/src/source_diff.cc +++ b/src/source_diff.cc @@ -149,7 +149,7 @@ void Source::DiffView::configure() { delayed_monitor_changed_connection = Glib::signal_timeout().connect([this]() { monitor_changed = true; parse_state = ParseState::STARTING; - std::unique_lock lock(parse_mutex); + std::lock_guard lock(parse_mutex); diff = nullptr; return false; }, 500); @@ -298,7 +298,7 @@ std::string Source::DiffView::git_get_diff_details() { auto iter = get_buffer()->get_iter_at_line(line_nr); if(iter.has_tag(renderer->tag_removed_above)) --line_nr; - std::unique_lock lock(parse_mutex); + std::lock_guard lock(parse_mutex); parse_buffer = get_buffer()->get_text(); details = diff->get_details(parse_buffer.raw(), line_nr); } @@ -312,7 +312,7 @@ std::unique_ptr Source::DiffView::get_diff() { auto work_path = filesystem::get_normal_path(repository->get_work_path()); boost::filesystem::path relative_path; { - std::unique_lock lock(canonical_file_path_mutex); + std::lock_guard lock(canonical_file_path_mutex); if(!filesystem::file_in_path(canonical_file_path, work_path)) throw std::runtime_error("not a relative path"); relative_path = filesystem::get_relative_path(canonical_file_path, work_path); diff --git a/src/source_generic.h b/src/source_generic.h index 7b1101a..63caf4e 100644 --- a/src/source_generic.h +++ b/src/source_generic.h @@ -19,6 +19,7 @@ namespace Source { std::pair get_word(Gtk::TextIter iter); std::vector> get_words(const Gtk::TextIter &start, const Gtk::TextIter &end); + std::mutex buffer_words_mutex; std::map buffer_words; void setup_buffer_words(); std::atomic show_prefix_buffer_word = {false}; /// To avoid showing the current word if it is unique in document diff --git a/src/source_language_protocol.cc b/src/source_language_protocol.cc index 016fe75..500e6c5 100644 --- a/src/source_language_protocol.cc +++ b/src/source_language_protocol.cc @@ -85,7 +85,7 @@ LanguageProtocol::Client::~Client() { }); result_processed.get_future().get(); - std::unique_lock lock(timeout_threads_mutex); + std::lock_guard lock(timeout_threads_mutex); for(auto &thread : timeout_threads) thread.join(); @@ -103,7 +103,7 @@ LanguageProtocol::Client::~Client() { LanguageProtocol::Capabilities LanguageProtocol::Client::initialize(Source::LanguageProtocolView *view) { if(view) { - std::unique_lock lock(views_mutex); + std::lock_guard lock(views_mutex); views.emplace(view); } @@ -145,12 +145,12 @@ LanguageProtocol::Capabilities LanguageProtocol::Client::initialize(Source::Lang void LanguageProtocol::Client::close(Source::LanguageProtocolView *view) { { - std::unique_lock lock(views_mutex); + std::lock_guard lock(views_mutex); auto it = views.find(view); if(it != views.end()) views.erase(it); } - std::unique_lock lock(read_write_mutex); + std::lock_guard lock(read_write_mutex); for(auto it = handlers.begin(); it != handlers.end();) { if(it->second.first == view) it = handlers.erase(it); @@ -272,11 +272,11 @@ void LanguageProtocol::Client::write_request(Source::LanguageProtocolView *view, handlers.emplace(message_id, std::make_pair(view, std::move(function))); auto message_id = this->message_id; - std::unique_lock lock(timeout_threads_mutex); + std::lock_guard lock(timeout_threads_mutex); timeout_threads.emplace_back([this, message_id] { for(size_t c = 0; c < 20; ++c) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); - std::unique_lock lock(read_write_mutex); + std::lock_guard lock(read_write_mutex); auto id_it = handlers.find(message_id); if(id_it == handlers.end()) return; @@ -311,7 +311,7 @@ void LanguageProtocol::Client::write_request(Source::LanguageProtocolView *view, } void LanguageProtocol::Client::write_notification(const std::string &method, const std::string ¶ms) { - std::unique_lock lock(read_write_mutex); + std::lock_guard lock(read_write_mutex); std::string content(R"({"jsonrpc":"2.0","method":")" + method + R"(","params":{)" + params + "}}"); auto message = "Content-Length: " + std::to_string(content.size()) + "\r\n\r\n" + content; if(Config::get().log.language_server) @@ -332,7 +332,7 @@ void LanguageProtocol::Client::handle_server_request(const std::string &method, catch(...) { } } - std::unique_lock lock(views_mutex); + std::lock_guard lock(views_mutex); for(auto view : views) { if(uri.size() >= 7 && uri.compare(7, std::string::npos, view->file_path.string()) == 0) { view->update_diagnostics(std::move(diagnostics)); @@ -1261,7 +1261,7 @@ void Source::LanguageProtocolView::setup_autocomplete() { std::smatch sm; if(std::regex_match(line, sm, regex)) { { - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); autocomplete.prefix = sm.length(2) ? sm[3].str() : sm.length(4) ? sm[5].str() : sm[6].str(); if(!sm.length(2) && !sm.length(4)) autocomplete_enable_snippets = true; @@ -1270,7 +1270,7 @@ void Source::LanguageProtocolView::setup_autocomplete() { } else if(is_possible_argument()) { autocomplete_show_parameters = true; - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); autocomplete.prefix = ""; return true; } @@ -1283,7 +1283,7 @@ void Source::LanguageProtocolView::setup_autocomplete() { iter.forward_char(); { - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); autocomplete.prefix = get_buffer()->get_text(iter, end_iter); } auto prev1 = iter; @@ -1401,7 +1401,7 @@ void Source::LanguageProtocolView::setup_autocomplete() { if(autocomplete_enable_snippets) { std::string prefix; { - std::unique_lock lock(autocomplete.prefix_mutex); + std::lock_guard lock(autocomplete.prefix_mutex); prefix = autocomplete.prefix; } std::lock_guard lock(snippets_mutex); diff --git a/src/terminal.cc b/src/terminal.cc index a397dab..6662eed 100644 --- a/src/terminal.cc +++ b/src/terminal.cc @@ -120,7 +120,7 @@ void Terminal::async_process(const std::string &command, const boost::filesystem } void Terminal::kill_last_async_process(bool force) { - std::unique_lock lock(processes_mutex); + std::lock_guard lock(processes_mutex); if(processes.empty()) Info::get().print("No running processes"); else @@ -128,7 +128,7 @@ void Terminal::kill_last_async_process(bool force) { } void Terminal::kill_async_processes(bool force) { - std::unique_lock lock(processes_mutex); + std::lock_guard lock(processes_mutex); for(auto &process : processes) process->kill(force); } @@ -393,7 +393,7 @@ bool Terminal::on_button_press_event(GdkEventButton *button_event) { } bool Terminal::on_key_press_event(GdkEventKey *event) { - std::unique_lock lock(processes_mutex); + std::lock_guard lock(processes_mutex); bool debug_is_running = false; #ifdef JUCI_ENABLE_DEBUG debug_is_running = Project::current ? Project::current->debug_is_running() : false; diff --git a/src/usages_clang.cc b/src/usages_clang.cc index 4f5acb9..782c1b1 100644 --- a/src/usages_clang.cc +++ b/src/usages_clang.cc @@ -170,7 +170,7 @@ std::vector Usages::Clang::get_usages(const boost::filesy // Use cache for(auto it = potential_paths.begin(); it != potential_paths.end();) { - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); auto caches_it = caches.find(*it); // Load cache from file if not found in memory and if cache file exists @@ -222,7 +222,7 @@ std::vector Usages::Clang::get_usages(const boost::filesy boost::filesystem::path path; { static std::mutex mutex; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); if(it == potential_paths.end()) return; path = *it; @@ -231,7 +231,7 @@ std::vector Usages::Clang::get_usages(const boost::filesy { static std::mutex mutex; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); // std::cout << "parsing: " << path << std::endl; } // auto before_time = std::chrono::system_clock::now(); @@ -258,7 +258,7 @@ std::vector Usages::Clang::get_usages(const boost::filesy { static std::mutex mutex; - std::unique_lock lock(mutex); + std::lock_guard lock(mutex); add_usages(project_path, build_path, path, usages, visited, spelling, cursor, &translation_unit, true); add_usages_from_includes(project_path, build_path, usages, visited, spelling, cursor, &translation_unit, true); } @@ -295,7 +295,7 @@ void Usages::Clang::cache(const boost::filesystem::path &project_path, const boo return; { - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); if(project_paths_in_use.count(project_path)) { caches.erase(path); caches.emplace(path, Cache(project_path, build_path, path, before_parse_time, translation_unit, tokens)); @@ -330,7 +330,7 @@ void Usages::Clang::cache(const boost::filesystem::path &project_path, const boo if(file_size == static_cast(-1) || ec) continue; auto tokens = translation_unit->get_tokens(path.string(), 0, file_size - 1); - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); if(project_paths_in_use.count(project_path)) { caches.erase(path); caches.emplace(path, Cache(project_path, build_path, path, before_parse_time, translation_unit, tokens.get())); @@ -341,7 +341,7 @@ void Usages::Clang::cache(const boost::filesystem::path &project_path, const boo } void Usages::Clang::erase_unused_caches(const PathSet &project_paths_in_use) { - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); for(auto it = caches.begin(); it != caches.end();) { bool found = false; for(auto &project_path : project_paths_in_use) { @@ -360,7 +360,7 @@ void Usages::Clang::erase_unused_caches(const PathSet &project_paths_in_use) { } void Usages::Clang::erase_cache(const boost::filesystem::path &path) { - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); auto it = caches.find(path); if(it == caches.end()) @@ -378,7 +378,7 @@ void Usages::Clang::erase_all_caches_for_project(const boost::filesystem::path & if(cache_in_progress_count != 0) std::this_thread::sleep_for(std::chrono::milliseconds(10)); - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); boost::system::error_code ec; auto usages_clang_path = build_path / cache_folder; if(boost::filesystem::exists(usages_clang_path, ec) && boost::filesystem::is_directory(usages_clang_path, ec)) { @@ -441,7 +441,7 @@ void Usages::Clang::add_usages(const boost::filesystem::path &project_path, cons } if(store_in_cache && filesystem::file_in_path(path, project_path)) { - std::unique_lock lock(caches_mutex); + std::lock_guard lock(caches_mutex); caches.erase(path); caches.emplace(path, Cache(project_path, build_path, path, before_parse_time, translation_unit, tokens.get())); }