Test: PityTest - Add fs_mutex (a very primitive IPC sync method)

LIB-11
heck 2 years ago
parent e45dd9604d
commit 3ac5b641d8

@ -4,10 +4,12 @@
#ifndef PITYTEST_PITYUNIT_HH
#define PITYTEST_PITYUNIT_HH
#include <string>
#include <map>
#include "../../../src/pEpLog.hh"
#include "../../../src/std_utils.hh"
#include <string>
#include <map>
#include <memory>
#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<fs_mutex> _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 { \

@ -13,6 +13,7 @@
#include <algorithm>
#include <sstream>
#include <exception>
#include <memory>
//using namespace pEp::Adapter::pEpLog;
@ -161,10 +162,12 @@ namespace pEp {
}
template<class T>
void PityUnit<T>::run() const
void PityUnit<T>::run()
{
pEpLogClass("called");
// caller is never nullptr if called by another PityUnit
_log_mutex = std::make_shared<fs_mutex>("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<std::string, PityUnit<T>&> 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<class T>
void PityUnit<T>::logRaw(const std::string& msg) const
{
_log_mutex->aquire();
Adapter::pEpLog::log(msg, _termColor());
_log_mutex->release();
}

@ -0,0 +1,41 @@
#include "fs_mutex.hh"
#include "../../../src/std_utils.hh"
#include<fstream>
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

@ -0,0 +1,25 @@
#ifndef FS_MUTEX
#define FS_MUTEX
#include <iostream>
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
Loading…
Cancel
Save