cec: cleaned up CCECBusDevice::GetStatus()
authorLars Op den Kamp <lars@opdenkamp.eu>
Fri, 17 Feb 2012 10:14:08 +0000 (11:14 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Fri, 17 Feb 2012 10:39:30 +0000 (11:39 +0100)
src/lib/devices/CECBusDevice.cpp

index a0d612ed5daa8178740a58475d2ff8b415da3170..3c393716d0270e81be0265106d8d176b8465eb30 100644 (file)
@@ -86,6 +86,7 @@ bool CCECBusDevice::HandleCommand(const cec_command &command)
     CLockObject lock(m_mutex);
     m_iLastActive = GetTimeMs();
 
+    /* don't call GetStatus() here, just read the value with the mutex locked */
     if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
       m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
 
@@ -439,20 +440,27 @@ bool CCECBusDevice::NeedsPoll(void)
 
 cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */)
 {
-  CLockObject lock(m_mutex);
-  if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC &&
-      (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll))
+  cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
+  bool bNeedsPoll(false);
+
+  {
+    CLockObject lock(m_mutex);
+    status = m_deviceStatus;
+    bNeedsPoll = (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC &&
+        (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll));
+  }
+
+  if (bNeedsPoll)
   {
-    lock.Unlock();
     bool bPollAcked(false);
-    if (bForcePoll || NeedsPoll())
+    if (bNeedsPoll || NeedsPoll())
       bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
 
-    lock.Lock();
-    m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
+    status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
+    SetDeviceStatus(status);
   }
 
-  return m_deviceStatus;
+  return status;
 }
 
 //@}