Test: PityTest - Add fs_mutex (a very primitive IPC sync method)
parent
e45dd9604d
commit
3ac5b641d8
@ -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…
Reference in New Issue