@@ -61,65 +61,41 @@ class StatefulService {
6161 }
6262
6363 void updateWithoutPropagation (std::function<void (T&)> callback) {
64- #ifdef ESP32
65- xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
66- #endif
64+ beginTransaction ();
6765 callback (_state);
68- #ifdef ESP32
69- xSemaphoreGiveRecursive (_accessMutex);
70- #endif
66+ endTransaction ();
7167 }
7268
7369 void updateWithoutPropagation (JsonObject& jsonObject, JsonDeserializer<T> deserializer) {
74- #ifdef ESP32
75- xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
76- #endif
70+ beginTransaction ();
7771 deserializer (jsonObject, _state);
78- #ifdef ESP32
79- xSemaphoreGiveRecursive (_accessMutex);
80- #endif
72+ endTransaction ();
8173 }
8274
8375 void update (std::function<void (T&)> callback, const String& originId) {
84- #ifdef ESP32
85- xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
86- #endif
76+ beginTransaction ();
8777 callback (_state);
8878 callUpdateHandlers (originId);
89- #ifdef ESP32
90- xSemaphoreGiveRecursive (_accessMutex);
91- #endif
79+ endTransaction ();
9280 }
9381
9482 void update (JsonObject& jsonObject, JsonDeserializer<T> deserializer, const String& originId) {
95- #ifdef ESP32
96- xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
97- #endif
83+ beginTransaction ();
9884 deserializer (jsonObject, _state);
9985 callUpdateHandlers (originId);
100- #ifdef ESP32
101- xSemaphoreGiveRecursive (_accessMutex);
102- #endif
86+ endTransaction ();
10387 }
10488
10589 void read (std::function<void (T&)> callback) {
106- #ifdef ESP32
107- xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
108- #endif
90+ beginTransaction ();
10991 callback (_state);
110- #ifdef ESP32
111- xSemaphoreGiveRecursive (_accessMutex);
112- #endif
92+ endTransaction ();
11393 }
11494
11595 void read (JsonObject& jsonObject, JsonSerializer<T> serializer) {
116- #ifdef ESP32
117- xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
118- #endif
96+ beginTransaction ();
11997 serializer (_state, jsonObject);
120- #ifdef ESP32
121- xSemaphoreGiveRecursive (_accessMutex);
122- #endif
98+ endTransaction ();
12399 }
124100
125101 void callUpdateHandlers (const String& originId) {
@@ -131,6 +107,18 @@ class StatefulService {
131107 protected:
132108 T _state;
133109
110+ inline void beginTransaction () {
111+ #ifdef ESP32
112+ xSemaphoreTakeRecursive (_accessMutex, portMAX_DELAY);
113+ #endif
114+ }
115+
116+ inline void endTransaction () {
117+ #ifdef ESP32
118+ xSemaphoreGiveRecursive (_accessMutex);
119+ #endif
120+ }
121+
134122 private:
135123#ifdef ESP32
136124 SemaphoreHandle_t _accessMutex;
0 commit comments