From ee7a6fbbb9fc7cd04a481a4851962df2e60293f7 Mon Sep 17 00:00:00 2001 From: Derek M Date: Tue, 9 Apr 2019 12:41:12 -0700 Subject: [PATCH] Adding a check to not wait if the thread has already fully started. (#228) When starting the depth engine, only wait on the condition if we don't think the thread has started yet as it will never be released if the thread has already triggered the condition. --- src/dewrapper/dewrapper.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/dewrapper/dewrapper.c b/src/dewrapper/dewrapper.c index 555670e8..13f0b78b 100644 --- a/src/dewrapper/dewrapper.c +++ b/src/dewrapper/dewrapper.c @@ -32,6 +32,7 @@ typedef struct _dewrapper_context_t THREAD_HANDLE thread; LOCK_HANDLE lock; COND_HANDLE condition; + volatile bool thread_started; volatile bool thread_stop; k4a_result_t thread_start_result; @@ -191,6 +192,7 @@ static int depth_engine_thread(void *param) // The Start routine is blocked waiting for this thread to complete startup, so we signal it here and share our // startup status. Lock(dewrapper->lock); + dewrapper->thread_started = true; dewrapper->thread_start_result = result; Condition_Post(dewrapper->condition); Unlock(dewrapper->lock); @@ -519,6 +521,7 @@ k4a_result_t dewrapper_start(dewrapper_t dewrapper_handle, dewrapper->fps = config->camera_fps; dewrapper->depth_mode = config->depth_mode; dewrapper->thread_stop = false; + dewrapper->thread_started = false; THREADAPI_RESULT tresult = ThreadAPI_Create(&dewrapper->thread, depth_engine_thread, dewrapper); result = K4A_RESULT_FROM_BOOL(tresult == THREADAPI_OK); @@ -527,9 +530,12 @@ k4a_result_t dewrapper_start(dewrapper_t dewrapper_handle, { Lock(dewrapper->lock); locked = true; - int infinite_timeout = 0; - COND_RESULT cond_result = Condition_Wait(dewrapper->condition, dewrapper->lock, infinite_timeout); - result = K4A_RESULT_FROM_BOOL(cond_result == COND_OK); + if (!dewrapper->thread_started) + { + int infinite_timeout = 0; + COND_RESULT cond_result = Condition_Wait(dewrapper->condition, dewrapper->lock, infinite_timeout); + result = K4A_RESULT_FROM_BOOL(cond_result == COND_OK); + } } if (K4A_SUCCEEDED(result) && K4A_FAILED(dewrapper->thread_start_result))