From 599c3e54dc17015690955d8333f970cb9cf243d3 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 8 Jun 2021 22:31:31 +0200 Subject: [PATCH] Test: PityTest - Add fs_mutex (a very primitive IPC sync method) --- src/PityUnit.hh | 11 ++++++----- src/PityUnit.hxx | 17 ++++++++--------- src/fs_mutex.cc | 41 +++++++++++++++++++++++++++++++++++++++++ src/fs_mutex.hh | 25 +++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 src/fs_mutex.cc create mode 100644 src/fs_mutex.hh diff --git a/src/PityUnit.hh b/src/PityUnit.hh index 0aeb316..3bccaed 100644 --- a/src/PityUnit.hh +++ b/src/PityUnit.hh @@ -4,10 +4,12 @@ #ifndef PITYTEST_PITYUNIT_HH #define PITYTEST_PITYUNIT_HH -#include -#include #include "../../../src/pEpLog.hh" #include "../../../src/std_utils.hh" +#include +#include +#include +#include "fs_mutex.hh" // Yes, the mem mgmt is purely static on purpose (so far) @@ -49,7 +51,7 @@ namespace pEp { // Main funcs - void run() const; + void run(); std::string to_string(bool recursive = true, int indent = 0) const; static std::string to_string(const ExecutionMode& emode); @@ -106,17 +108,16 @@ namespace pEp { int procUnitNr; static int procUnitsCount; // will be increased in everuy constructor + std::shared_ptr _log_mutex = nullptr; // internal logging Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; }; - class PityAssertException : public std::runtime_error { public: PityAssertException(const std::string& string) : runtime_error(string) {} }; - #ifndef PTASSERT #define PTASSERT(condition) \ do { \ diff --git a/src/PityUnit.hxx b/src/PityUnit.hxx index 10e44b3..c0307de 100644 --- a/src/PityUnit.hxx +++ b/src/PityUnit.hxx @@ -13,6 +13,7 @@ #include #include #include +#include //using namespace pEp::Adapter::pEpLog; @@ -161,10 +162,12 @@ namespace pEp { } template - void PityUnit::run() const + void PityUnit::run() { pEpLogClass("called"); - // caller is never nullptr if called by another PityUnit + _log_mutex = std::make_shared("fds"); + _log_mutex->release(); + if (_isRootUnit()) { _init(); } @@ -285,11 +288,6 @@ namespace pEp { logH3("INIT"); Utils::dir_ensure(getGlobalRootDir()); recreateDirsRecursively(); - // if (!_children.empty()) { - // for (const std::pair&> child : _children) { - // _recreateDir(child.second.processDir()); - // } - // } logH3("INIT DONE"); } @@ -311,8 +309,7 @@ namespace pEp { logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_termColor()))); } catch (const std::exception& e) { logRaw("reason: " + std::string(e.what())); - logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_termColor()) - )); + logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_termColor()))); } } else { logRaw("No function to execute"); @@ -464,7 +461,9 @@ namespace pEp { template void PityUnit::logRaw(const std::string& msg) const { + _log_mutex->aquire(); Adapter::pEpLog::log(msg, _termColor()); + _log_mutex->release(); } diff --git a/src/fs_mutex.cc b/src/fs_mutex.cc new file mode 100644 index 0000000..ede8b6d --- /dev/null +++ b/src/fs_mutex.cc @@ -0,0 +1,41 @@ +#include "fs_mutex.hh" +#include "../../../src/std_utils.hh" +#include + + +namespace pEp { + namespace PityTest11 { + fs_mutex::fs_mutex(std::string mutexpath) : mutexpath{ mutexpath } {} + + void fs_mutex::aquire() const + { + if (mutexpath.empty()) { + throw std::runtime_error("no mutexpath set"); + } else { + std::string mutex_file = mutexpath; + while (Utils::path_exists(mutex_file)) { + Utils::sleep_millis(5); + } + std::ofstream msgfile = Utils::file_create(mutexpath); + } + } + + void fs_mutex::release() const + { + if (mutexpath.empty()) { + throw std::runtime_error("no mutexpath set"); + } else { + + try { + Utils::path_delete(mutexpath); + // Give others a chance to pickup + Utils::sleep_millis(100); + } catch (...) { + // pEpLogClass("Error releasing fsmutex"); + } + } + } + + } // namespace PityTest +} // namespace pEp + diff --git a/src/fs_mutex.hh b/src/fs_mutex.hh new file mode 100644 index 0000000..a71e394 --- /dev/null +++ b/src/fs_mutex.hh @@ -0,0 +1,25 @@ +#ifndef FS_MUTEX +#define FS_MUTEX + +#include + +namespace pEp { + namespace PityTest11 { + // a very primitive IPC sync method + // also unreliable + // but good enough for what i just needed it for + class fs_mutex { + public: + fs_mutex() = delete; + fs_mutex(std::string mutexpath); + + void aquire() const; + void release() const; + + private: + const std::string mutexpath; + }; + } // namespace PityTest11 +} // namespace pEp + +#endif // FS_MUTEX \ No newline at end of file