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/mainloop.cpp b/mainloop.cpp
index 7f446a7..f99decb 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -397,14 +397,15 @@
                 std::unique_ptr<sensor::Sensor>& sensor =
                     _sensorObjects[i.first];
 
-                sensor->unlockGpio();
+                {
+                    // RAII object for GPIO unlock / lock
+                    sensor::GpioLock gpioLock(sensor->getGpio());
 
-                value = _ioAccess.read(i.first.first, i.first.second, input,
-                                       hwmonio::retries, hwmonio::delay);
+                    value = _ioAccess.read(i.first.first, i.first.second, input,
+                                           hwmonio::retries, hwmonio::delay);
 
-                sensor->lockGpio();
-
-                value = sensor->adjustValue(value);
+                    value = sensor->adjustValue(value);
+                }
 
                 for (auto& iface : obj)
                 {