From 5ea3620b2fb39ee9408d93fd6cd214d557d42a7f Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 11 Feb 2016 13:57:27 +0100 Subject: Extract child emplacement into thread safe member --- src/tracking/change_tracker.cc | 26 ++++++++++++-------------- src/tracking/change_tracker.h | 6 +++++- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/tracking/change_tracker.cc b/src/tracking/change_tracker.cc index c32a3c5..74e656f 100644 --- a/src/tracking/change_tracker.cc +++ b/src/tracking/change_tracker.cc @@ -30,8 +30,8 @@ boost::process::context getDefaultContext() { // diff -u --label $file_path - $file_path // std::string getDiffCommand( - const std::string& diff_cmd, const std::string& full_path) { - return diff_cmd + " --label " + full_path + " - " + full_path; + const std::string& diff_cmd, const std::string& file_path) { + return diff_cmd + " --label " + file_path + " - " + file_path; } } @@ -74,6 +74,15 @@ bool ChangeTracker::is_tracked(const std::string& file_path) const { ) != this->children_.end(); } +auto ChangeTracker::create_child(const std::string& file_path) { + std::lock_guard guard(this->write_mutex_); + + return this->children_.emplace( + boost::filesystem::canonical(file_path).string(), + std::make_unique() + ); +} + // Begins tracking changes to a file reachable by a given path // // _Tracking_ consists of adding the full file path to the `children_` @@ -90,18 +99,7 @@ bool ChangeTracker::is_tracked(const std::string& file_path) const { // the tracked file is modified after `diff` has been spawned. // bool ChangeTracker::track(const std::string& file_path) { - const std::string full_file_path{ - boost::filesystem::canonical(file_path).string() - }; - - std::unique_lock guard(this->write_mutex_); - - auto result = this->children_.emplace( - full_file_path, - std::make_unique() - ); - - guard.unlock(); + auto result = this->create_child(file_path); if ( std::get(result) ) { boost::filesystem::ifstream file( diff --git a/src/tracking/change_tracker.h b/src/tracking/change_tracker.h index 05f4021..04a5a00 100644 --- a/src/tracking/change_tracker.h +++ b/src/tracking/change_tracker.h @@ -25,9 +25,13 @@ class ChangeTracker { std::mutex write_mutex_; std::unordered_map< - std::string, std::unique_ptr + std::string, + std::unique_ptr > children_; + // threadsafe child emplacement + auto create_child(const std::string&); + }; } -- cgit v1.2.3