diff options
author | Adrian Kummerlaender | 2016-02-11 13:57:27 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2016-02-11 13:57:27 +0100 |
commit | 5ea3620b2fb39ee9408d93fd6cd214d557d42a7f (patch) | |
tree | 8511a1ac9219e7dd9b891140a9e91681453e482f | |
parent | 801ddda073b04c9751bc9e4f935bd102d7741c7b (diff) | |
download | change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.tar change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.tar.gz change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.tar.bz2 change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.tar.lz change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.tar.xz change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.tar.zst change-5ea3620b2fb39ee9408d93fd6cd214d557d42a7f.zip |
Extract child emplacement into thread safe member
-rw-r--r-- | src/tracking/change_tracker.cc | 26 | ||||
-rw-r--r-- | 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<std::mutex> guard(this->write_mutex_); + + return this->children_.emplace( + boost::filesystem::canonical(file_path).string(), + std::make_unique<std::stringstream>() + ); +} + // 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<std::mutex> guard(this->write_mutex_); - - auto result = this->children_.emplace( - full_file_path, - std::make_unique<std::stringstream>() - ); - - guard.unlock(); + auto result = this->create_child(file_path); if ( std::get<EMPLACE_SUCCESS>(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::stringstream> + std::string, + std::unique_ptr<std::stringstream> > children_; + // threadsafe child emplacement + auto create_child(const std::string&); + }; } |