Add critical section to avoid thread safety issue 45/151545/1 accepted/tizen/4.0/unified/20170922.064439 submit/tizen_4.0/20170921.073346
authorWonnam Jang <wn.jang@samsung.com>
Tue, 12 Sep 2017 01:32:13 +0000 (10:32 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Thu, 21 Sep 2017 07:22:50 +0000 (07:22 +0000)
Change-Id: Ie5fc64abd2285aae4d69e2594108dc4eefe30d66
Signed-off-by: Wonnam Jang <wn.jang@samsung.com>
(cherry picked from commit 486aa7d2f3c3634c5636110fa2f031ead312fb11)

server/sttd_recorder.c

index 16202d0..b83cc47 100644 (file)
@@ -593,9 +593,14 @@ int sttd_recorder_stop()
        if (STTD_RECORDER_STATE_READY == g_recorder_state)
                return 0;
 
        if (STTD_RECORDER_STATE_READY == g_recorder_state)
                return 0;
 
+       // critical section required because this function can be called from stt engine thread context
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
+       pthread_mutex_lock(&sttd_audio_in_handle_mutex);
+
        /* Check engine id is valid */
        if (NULL == g_recorder) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
        /* Check engine id is valid */
        if (NULL == g_recorder) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
+               pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
                return STTD_ERROR_INVALID_PARAMETER;
        }
 
                return STTD_ERROR_INVALID_PARAMETER;
        }
 
@@ -636,6 +641,8 @@ int sttd_recorder_stop()
 #ifdef BUF_SAVE_MODE
        fclose(g_pFile);
 #endif
 #ifdef BUF_SAVE_MODE
        fclose(g_pFile);
 #endif
+       pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
 
        return 0;
 }
 
        return 0;
 }