aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-12-27 10:07:24 +0100
committerAdrian Kummerlaender2015-12-27 10:07:24 +0100
commit0f083843bc31bf985991728125ef74efca6ef7b7 (patch)
treeaf51d088fa7e6ea70a7ca47421eddb09494c8788
parent0f1b6aea1fc7a2753cfde98a74c1ab74e258576e (diff)
downloadchange-0f083843bc31bf985991728125ef74efca6ef7b7.tar
change-0f083843bc31bf985991728125ef74efca6ef7b7.tar.gz
change-0f083843bc31bf985991728125ef74efca6ef7b7.tar.bz2
change-0f083843bc31bf985991728125ef74efca6ef7b7.tar.lz
change-0f083843bc31bf985991728125ef74efca6ef7b7.tar.xz
change-0f083843bc31bf985991728125ef74efca6ef7b7.tar.zst
change-0f083843bc31bf985991728125ef74efca6ef7b7.zip
Introduce thread synchronization for tracking init and logging
This obviously leads to synchronizing syscalls that would otherwise happen in parallel. Luckily the goal of this library is to monitor file changes performed by single, user facing applications and as such it doesn't matter if some operations are slightly slower.
-rw-r--r--src/tracking/change_tracker.cc4
-rw-r--r--src/tracking/change_tracker.h2
-rw-r--r--src/utility/logger.h7
3 files changed, 13 insertions, 0 deletions
diff --git a/src/tracking/change_tracker.cc b/src/tracking/change_tracker.cc
index 1b554aa..ca07214 100644
--- a/src/tracking/change_tracker.cc
+++ b/src/tracking/change_tracker.cc
@@ -82,6 +82,8 @@ bool ChangeTracker::track(const std::string& 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<boost::process::child>(
@@ -92,6 +94,8 @@ bool ChangeTracker::track(const std::string& file_path) {
)
);
+ guard.unlock();
+
if ( std::get<EMPLACE_SUCCESS>(result) ) {
boost::filesystem::ifstream file(
std::get<FILE_NAME>(*result.first)
diff --git a/src/tracking/change_tracker.h b/src/tracking/change_tracker.h
index 24251e5..e694b56 100644
--- a/src/tracking/change_tracker.h
+++ b/src/tracking/change_tracker.h
@@ -2,6 +2,7 @@
#define CHANGE_SRC_TRACKING_CHANGE_TRACKER_H_
#include <unordered_map>
+#include <mutex>
#include <boost/process.hpp>
@@ -22,6 +23,7 @@ class ChangeTracker {
private:
const std::string diff_cmd_;
utility::Logger* const logger_;
+ std::mutex write_mutex_;
std::unordered_map<
std::string, std::unique_ptr<boost::process::child>
diff --git a/src/utility/logger.h b/src/utility/logger.h
index e21f6fd..30a6b44 100644
--- a/src/utility/logger.h
+++ b/src/utility/logger.h
@@ -1,6 +1,8 @@
#ifndef CHANGE_SRC_LOGGER_H_
#define CHANGE_SRC_LOGGER_H_
+#include <mutex>
+
#include <ext/stdio_filebuf.h>
#include <boost/process.hpp>
@@ -14,6 +16,8 @@ class Logger {
stream_(&this->buffer_) { }
void append(const std::string& msg) {
+ std::lock_guard<std::mutex> guard(this->write_mutex_);
+
this->stream_ << msg << std::endl;
}
@@ -25,6 +29,8 @@ class Logger {
// flag.
//
void forward(boost::process::pistream& stream) {
+ std::lock_guard<std::mutex> guard(this->write_mutex_);
+
this->stream_ << std::string(
(std::istreambuf_iterator<char>(stream)),
(std::istreambuf_iterator<char>())
@@ -34,6 +40,7 @@ class Logger {
private:
__gnu_cxx::stdio_filebuf<char> buffer_;
std::ostream stream_;
+ std::mutex write_mutex_;
};