Avoid infinite blocking on internalWait() by replacing cond_wait with cond_timedwait
[platform/core/api/audio-io.git] / src / cpp / CAudioIO.cpp
index b1a278c..bda8400 100644 (file)
@@ -19,6 +19,8 @@
 #include <assert.h>
 #include "CAudioIODef.h"
 #include <sound_manager_internal.h>
+#include <sys/time.h>
+#include <string.h>
 
 using namespace std;
 using namespace tizen_media_audio;
@@ -98,9 +100,27 @@ void CAudioIO::internalWait() {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
     AUDIO_IO_LOGD(COLOR_RED "WAIT" COLOR_END);
 #endif
-
     pthread_mutex_lock(&__mCondMutex);
-    pthread_cond_wait(&__mCond, &__mCondMutex);
+
+    struct timeval now = { 0, };
+    struct timeval to_wait = { 0, };
+    struct timeval until = { 0, };
+    struct timespec until_ts = { 0, };
+
+    constexpr int COND_TIMEOUT_MS = 200;
+
+    gettimeofday(&now, nullptr);
+    to_wait.tv_sec = COND_TIMEOUT_MS / 1000UL;
+    to_wait.tv_usec = (COND_TIMEOUT_MS % 1000UL) * 1000UL;
+    timeradd(&now, &to_wait, &until);
+    until_ts.tv_sec = until.tv_sec;
+    until_ts.tv_nsec = until.tv_usec * 1000UL;
+
+    if (pthread_cond_timedwait(&__mCond, &__mCondMutex, &until_ts) != 0) {
+        char str_error[256];
+        AUDIO_IO_LOGE("pthread_cond_timedwait error=%s", strerror_r(errno, str_error, sizeof(str_error)));
+    }
+
     pthread_mutex_unlock(&__mCondMutex);
 }