sensor: Implement RAII object for GPIO unlock and lock
There is a bug where GPIO lock may not be called if there is an
exception after GPIO unlock. Adding an RAII object for GPIO lock
and replacing the current usage of gpioUnlock, gpioLock in
sensor.cpp and mainloop.cpp.
Bug: openbmc/phosphor-hwmon#11
Signed-off-by: Brandon Kim <brandonkim@google.com>
Change-Id: Ica094e716a6ff9dc99165651f83fc496d5ed5a17
diff --git a/sensor.cpp b/sensor.cpp
index d45f23a..89f8330 100644
--- a/sensor.cpp
+++ b/sensor.cpp
@@ -146,7 +146,8 @@
// its status is functional, read the input value.
if (!statusIface || (statusIface && statusIface->functional()))
{
- unlockGpio();
+ // RAII object for GPIO unlock / lock
+ GpioLock gpioLock(getGpio());
// Retry for up to a second if device is busy
// or has a transient error.
@@ -154,7 +155,6 @@
hwmon::entry::cinput, std::get<size_t>(retryIO),
std::get<std::chrono::milliseconds>(retryIO));
- lockGpio();
val = adjustValue(val);
}
@@ -243,7 +243,17 @@
return iface;
}
-void Sensor::unlockGpio()
+GpioLock::GpioLock(const gpioplus::HandleInterface* handle) : _handle(handle)
+{
+ unlockGpio();
+}
+
+GpioLock::~GpioLock()
+{
+ lockGpio();
+}
+
+void GpioLock::unlockGpio()
{
if (_handle)
{
@@ -252,7 +262,7 @@
}
}
-void Sensor::lockGpio()
+void GpioLock::lockGpio()
{
if (_handle)
{