From 8ecbb86acf5e43cc1c3596807b388787046ee4cf Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 5 Sep 2013 09:33:12 +0200 Subject: [PATCH 1/2] Fixed issue #1281, thread shutdown now properly waiting for quit. --- channels/audin/client/alsa/audin_alsa.c | 40 ++++++++++++++----------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/channels/audin/client/alsa/audin_alsa.c b/channels/audin/client/alsa/audin_alsa.c index 0fd034514..c5213799e 100644 --- a/channels/audin/client/alsa/audin_alsa.c +++ b/channels/audin/client/alsa/audin_alsa.c @@ -163,14 +163,9 @@ static BOOL audin_alsa_thread_receive(AudinALSADevice* alsa, BYTE* src, int size } if (WaitForSingleObject(alsa->stopEvent, 0) == WAIT_OBJECT_0) - { - ret = 0; - frames = 0; - } + break; else - { ret = alsa->receive(encoded_data, encoded_size, alsa->user_data); - } alsa->buffer_frames = 0; @@ -182,7 +177,7 @@ static BOOL audin_alsa_thread_receive(AudinALSADevice* alsa, BYTE* src, int size frames -= cframes; } - return ret; + return (ret) ? TRUE : FALSE; } static void* audin_alsa_thread_func(void* arg) @@ -198,9 +193,6 @@ static void* audin_alsa_thread_func(void* arg) rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel; tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel; - alsa->buffer = (BYTE*) malloc(tbytes_per_frame * alsa->frames_per_packet); - ZeroMemory(alsa->buffer, tbytes_per_frame * alsa->frames_per_packet); - alsa->buffer_frames = 0; buffer = (BYTE*) malloc(rbytes_per_frame * alsa->frames_per_packet); ZeroMemory(buffer, rbytes_per_frame * alsa->frames_per_packet); freerdp_dsp_context_reset_adpcm(alsa->dsp_context); @@ -241,15 +233,11 @@ static void* audin_alsa_thread_func(void* arg) free(buffer); - free(alsa->buffer); - alsa->buffer = NULL; - if (capture_handle) snd_pcm_close(capture_handle); - SetEvent(alsa->stopEvent); - DEBUG_DVC("out"); + ExitThread(0); return NULL; } @@ -258,8 +246,6 @@ static void audin_alsa_free(IAudinDevice* device) { AudinALSADevice* alsa = (AudinALSADevice*) device; - SetEvent(alsa->stopEvent); - freerdp_dsp_context_free(alsa->dsp_context); free(alsa->device_name); @@ -337,6 +323,8 @@ static void audin_alsa_set_format(IAudinDevice* device, audinFormat* format, UIN static void audin_alsa_open(IAudinDevice* device, AudinReceive receive, void* user_data) { + int rbytes_per_frame; + int tbytes_per_frame; AudinALSADevice* alsa = (AudinALSADevice*) device; DEBUG_DVC(""); @@ -345,7 +333,14 @@ static void audin_alsa_open(IAudinDevice* device, AudinReceive receive, void* us alsa->user_data = user_data; alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - alsa->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) audin_alsa_thread_func, alsa, 0, NULL); + alsa->thread = CreateThread(NULL, 0, + (LPTHREAD_START_ROUTINE) audin_alsa_thread_func, alsa, 0, NULL); + + rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel; + tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel; + alsa->buffer = (BYTE*) malloc(tbytes_per_frame * alsa->frames_per_packet); + ZeroMemory(alsa->buffer, tbytes_per_frame * alsa->frames_per_packet); + alsa->buffer_frames = 0; } static void audin_alsa_close(IAudinDevice* device) @@ -355,7 +350,16 @@ static void audin_alsa_close(IAudinDevice* device) DEBUG_DVC(""); SetEvent(alsa->stopEvent); + WaitForSingleObject(alsa->thread, INFINITE); + CloseHandle(alsa->stopEvent); + CloseHandle(alsa->thread); + if (alsa->buffer) + free(alsa->buffer); + alsa->buffer = NULL; + + alsa->stopEvent = NULL; + alsa->thread = NULL; alsa->receive = NULL; alsa->user_data = NULL; } From 8e6b79376cafe0edaadc0b91514167287db67d9e Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 5 Sep 2013 13:41:32 +0200 Subject: [PATCH 2/2] Fixed possible race found by bmiklautz --- channels/audin/client/alsa/audin_alsa.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/channels/audin/client/alsa/audin_alsa.c b/channels/audin/client/alsa/audin_alsa.c index c5213799e..31dea232d 100644 --- a/channels/audin/client/alsa/audin_alsa.c +++ b/channels/audin/client/alsa/audin_alsa.c @@ -332,15 +332,15 @@ static void audin_alsa_open(IAudinDevice* device, AudinReceive receive, void* us alsa->receive = receive; alsa->user_data = user_data; - alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - alsa->thread = CreateThread(NULL, 0, - (LPTHREAD_START_ROUTINE) audin_alsa_thread_func, alsa, 0, NULL); - rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel; tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel; alsa->buffer = (BYTE*) malloc(tbytes_per_frame * alsa->frames_per_packet); ZeroMemory(alsa->buffer, tbytes_per_frame * alsa->frames_per_packet); alsa->buffer_frames = 0; + + alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + alsa->thread = CreateThread(NULL, 0, + (LPTHREAD_START_ROUTINE) audin_alsa_thread_func, alsa, 0, NULL); } static void audin_alsa_close(IAudinDevice* device)