From 11c12d7cfbc53c1f881e83e9cb8695829abcc857 Mon Sep 17 00:00:00 2001 From: Seungbae Shin Date: Tue, 31 Dec 2019 14:03:18 +0900 Subject: [PATCH] Avoid infinite blocking on internalWait() by replacing cond_wait with cond_timedwait [Version] 0.5.22 [Issue Type] Deadlock Change-Id: If7064ca734a93a8c75b88b18298f5868feffdfad --- packaging/capi-media-audio-io.spec | 2 +- src/cpp/CAudioIO.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packaging/capi-media-audio-io.spec b/packaging/capi-media-audio-io.spec index 8933b38..62a471c 100644 --- a/packaging/capi-media-audio-io.spec +++ b/packaging/capi-media-audio-io.spec @@ -1,6 +1,6 @@ Name: capi-media-audio-io Summary: An Audio Input & Audio Output library in Tizen Native API -Version: 0.5.21 +Version: 0.5.22 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/cpp/CAudioIO.cpp b/src/cpp/CAudioIO.cpp index b1a278c..bda8400 100644 --- a/src/cpp/CAudioIO.cpp +++ b/src/cpp/CAudioIO.cpp @@ -19,6 +19,8 @@ #include #include "CAudioIODef.h" #include +#include +#include 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); } -- 2.7.4