mirror of
https://github.com/tbnobody/OpenDTU.git
synced 2026-01-06 04:44:50 +01:00
add and use configuration write guard
the configuration write guard is now required when the configuration struct shall be mutated. the write guards locks multiple writers against each other and also, more importantly, makes the writes synchronous to the main loop. all code running in the main loop can now be sure that (1) reads from the configuration struct are non-preemtive and (2) the configuration struct as a whole is in a consistent state when reading from it. NOTE that acquiring a write guard from within the main loop's task will immediately cause a deadlock and the watchdog will trigger a reset. if writing from inside the main loop should ever become necessary, the write guard must be updated to only lock the mutex but not wait for a signal.
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
|
||||
#include "PinMapping.h"
|
||||
#include <cstdint>
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
#define CONFIG_FILENAME "/config.json"
|
||||
#define CONFIG_VERSION 0x00011c00 // 0.1.28 // make sure to clean all after change
|
||||
@@ -161,15 +164,32 @@ struct CONFIG_T {
|
||||
|
||||
class ConfigurationClass {
|
||||
public:
|
||||
void init();
|
||||
void init(Scheduler& scheduler);
|
||||
bool read();
|
||||
bool write();
|
||||
void migrate();
|
||||
CONFIG_T& get();
|
||||
CONFIG_T const& get();
|
||||
|
||||
class WriteGuard {
|
||||
public:
|
||||
WriteGuard();
|
||||
CONFIG_T& getConfig();
|
||||
~WriteGuard();
|
||||
|
||||
private:
|
||||
std::unique_lock<std::mutex> _lock;
|
||||
};
|
||||
|
||||
WriteGuard getWriteGuard();
|
||||
|
||||
INVERTER_CONFIG_T* getFreeInverterSlot();
|
||||
INVERTER_CONFIG_T* getInverterConfig(const uint64_t serial);
|
||||
void deleteInverterById(const uint8_t id);
|
||||
|
||||
private:
|
||||
void loop();
|
||||
|
||||
Task _loopTask;
|
||||
};
|
||||
|
||||
extern ConfigurationClass Configuration;
|
||||
|
||||
Reference in New Issue
Block a user