[Ovmsdev] Metrics & Notifications: thread safety
ovms at m4gnus.de
Wed Jan 23 23:28:29 HKT 2019
OvmsMetricBool, -Int and -Float don't seem to use a mutex, but afaik
there is no garantuee for them to be atomic. A simple fix would be to
make m_value a std::atomic<float> etc. or use a mutex just as with
Furthermore there might be a small race condition when two tasks modify
a metric. Assuming the following base situation:
// some other code...
// now between ovms_metrics.cpp line 793 and 794 the thread is interrupted and this happens:
// the value is modified, all change listener are called but none resets the modified flag.
// now execution continues with line 794 of dummy.SetValue("a"); from above
// which essentially does SetModified(false);
// but in fact the current value is "b" so it was modified.
This also applies to all other metric types, as they all seem to call
SetModified after changing the value.
The easy fix would be to put the SetModified into the mutex lock, but
that would result in a deadlock when one of the change listeners calls
I don't see an better way to avoid that race condition though :(
On Tue, 2019-01-22 at 15:49 +0100, Michael Balzer wrote:
> Brain surgery. A second pair of eyes on this would be good.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 488 bytes
Desc: This is a digitally signed message part
More information about the OvmsDev