use pthread_cond_timedwait() in poll thread

This does not fix the underlying causes for the hangs in HDR mode, but should
allow the HAL to recover in case of that failure, by replacing the
pthread_cond_wait()s with pthread_cond_timedwait()s that timed out after 2
seconds.

Bug: 7305012
Bug: 7301582
Change-Id: I213f52755a0157bec413d19a14d8a1340de14ae0
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/camera/mm-camera-interface/mm_camera_poll_thread.c b/camera/mm-camera-interface/mm_camera_poll_thread.c
index fedb3c7..c59369c 100644
--- a/camera/mm-camera-interface/mm_camera_poll_thread.c
+++ b/camera/mm-camera-interface/mm_camera_poll_thread.c
@@ -75,6 +75,7 @@
     cmd_evt.cmd = cmd;
     int len;
     CDBG("%s: begin", __func__);
+
     pthread_mutex_lock(&poll_cb->mutex);
     /* reset the statue to false */
     poll_cb->status = FALSE;
@@ -87,9 +88,17 @@
     }
     CDBG("%s: begin IN mutex write done, len = %d", __func__, len);
     /* wait till worker task gives positive signal */
-    if(FALSE == poll_cb->status) {
-      CDBG("%s: wait", __func__);
-        pthread_cond_wait(&poll_cb->cond_v, &poll_cb->mutex);
+    while (!poll_cb->status) {
+        int rc;
+        struct timespec ts;
+        clock_gettime(CLOCK_REALTIME, &ts);
+        ts.tv_sec += 2;
+        CDBG("%s: wait", __func__);
+        rc = pthread_cond_timedwait(&poll_cb->cond_v, &poll_cb->mutex, &ts);
+        if (rc) {
+            ALOGE("%s: error on pthread_cond_timedwait: %s", __func__, strerror(rc));
+            break;
+        }
     }
     /* done */
     pthread_mutex_unlock(&poll_cb->mutex);
@@ -298,8 +307,17 @@
     pthread_mutex_lock(&poll_cb->mutex);
     poll_cb->status = 0;
     pthread_create(&poll_cb->data.pid, NULL, mm_camera_poll_thread, (void *)poll_cb);
-    if(!poll_cb->status) {
-        pthread_cond_wait(&poll_cb->cond_v, &poll_cb->mutex);
+    while (!poll_cb->status) {
+        int rc;
+        struct timespec ts;
+
+        clock_gettime(CLOCK_REALTIME, &ts);
+        ts.tv_sec += 2;
+        rc = pthread_cond_timedwait(&poll_cb->cond_v, &poll_cb->mutex, &ts);
+        if (rc) {
+            ALOGE("%s: error on pthread_cond_timedwait: %s", __func__, strerror(rc));
+            break;
+        }
     }
     pthread_mutex_unlock(&poll_cb->mutex);
     return MM_CAMERA_OK;