From 8dae96fe292ef3855abc0720fcdf7a2c3eb838f5 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Fri, 9 Feb 2018 14:46:08 +0800 Subject: [PATCH] fix(worker): worker did not enter the blocked state waiting for task (#134) --- src/worker.c | 2 ++ test/Makefile.am | 4 +-- test/test.c | 3 +- test/test.h | 1 + test/test_thread.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 test/test_thread.c diff --git a/src/worker.c b/src/worker.c index bbfc5ca6..9691c43a 100644 --- a/src/worker.c +++ b/src/worker.c @@ -99,6 +99,7 @@ LCUI_BOOL LCUIWorker_RunTask( LCUI_Worker worker ) static void LCUIWorker_Thread( void *arg ) { LCUI_Worker worker = arg; + LCUIMutex_Lock( &worker->mutex ); while( worker->active ) { if( LCUIWorker_RunTask( worker ) ) { continue; @@ -107,6 +108,7 @@ static void LCUIWorker_Thread( void *arg ) LCUICond_Wait( &worker->cond, &worker->mutex ); } } + LCUIMutex_Unlock( &worker->mutex ); LCUIThread_Exit( NULL ); } diff --git a/test/Makefile.am b/test/Makefile.am index 3dfbc737..da886a17 100755 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -11,8 +11,8 @@ helloworld_LDADD = $(top_builddir)/src/libLCUI.la -lm test_SOURCES = test.c test_css_parser.c test_xml_parser.c test_string.c \ test_font_load.c test_image_reader.c test_widget_rect.c test_widget_layout.c \ -test_widget_flex_layout.c test_widget_inline_block_layout.c\ -test_char_render.c test_string_render.c test_widget_render.c +test_widget_flex_layout.c test_widget_inline_block_layout.c test_thread.c \ +test_char_render.c test_string_render.c test_widget_render.c test_LDADD = $(top_builddir)/src/libLCUI.la -lm $(CODE_COVERAGE_LIBS) diff --git a/test/test.c b/test/test.c index 8d6b1fa1..9487cb6c 100644 --- a/test/test.c +++ b/test/test.c @@ -23,8 +23,9 @@ int main( void ) Logger_SetHandler( LoggerHandler ); Logger_SetHandlerW( LoggerHandlerW ); #endif - ret += test_font_load(); ret += test_string(); + ret += test_thread(); + ret += test_font_load(); ret += test_image_reader(); ret += test_css_parser(); ret += test_xml_parser(); diff --git a/test/test.h b/test/test.h index f3fb7a53..35bba635 100644 --- a/test/test.h +++ b/test/test.h @@ -27,6 +27,7 @@ Logger_Log("[test] %d tests, %d pass.\n", tests_count, tests_count + N) } while( 0 ); int test_string( void ); +int test_thread( void ); int test_font_load( void ); int test_css_parser( void ); int test_xml_parser( void ); diff --git a/test/test_thread.c b/test/test_thread.c new file mode 100644 index 00000000..8aa175d4 --- /dev/null +++ b/test/test_thread.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include "test.h" + +typedef struct TestWorkerRec_ { + char data[32]; + int data_count; + LCUI_BOOL active; + LCUI_Cond cond; + LCUI_Mutex mutex; + LCUI_Thread thread; +} TestWorkerRec, *TestWorker; + +static void TestWorker_Thread( void *arg ) +{ + TestWorker worker = arg; + + worker->active = TRUE; + worker->data_count = 0; + LCUIMutex_Lock( &worker->mutex ); + while( worker->active ) { + TEST_LOG( "waiting...\n" ); + LCUICond_Wait( &worker->cond, &worker->mutex ); + TEST_LOG( "get data: %s\n", worker->data ); + worker->data_count += 1; + } + LCUIMutex_Unlock( &worker->mutex ); + TEST_LOG( "count: %lu\n", worker->data_count ); +} + +static void TestWorker_Send( TestWorker worker, const char *data ) +{ + LCUIMutex_Lock( &worker->mutex ); + strcpy( worker->data, data ); + LCUICond_Signal( &worker->cond ); + LCUIMutex_Unlock( &worker->mutex ); +} + +static void TestWorker_Init( TestWorker worker ) +{ + LCUIMutex_Init( &worker->mutex ); + LCUICond_Init( &worker->cond ); +} + +static void TestWorker_Destroy( TestWorker worker ) +{ + LCUIMutex_Lock( &worker->mutex ); + worker->active = FALSE; + LCUICond_Signal( &worker->cond ); + LCUIMutex_Unlock( &worker->mutex ); + LCUIThread_Join( worker->thread, NULL ); + LCUIMutex_Destroy( &worker->mutex ); + LCUICond_Destroy( &worker->cond ); +} + +int test_thread( void ) +{ + int ret = 0; + TestWorkerRec worker; + + TestWorker_Init( &worker ); + LCUIThread_Create( &worker.thread, TestWorker_Thread, &worker ); + LCUI_Sleep( 1 ); + TestWorker_Send( &worker, "hello" ); + LCUI_MSleep( 200 ); + TestWorker_Send( &worker, "world" ); + LCUI_Sleep( 1 ); + TestWorker_Send( &worker, "this" ); + LCUI_MSleep( 500 ); + TestWorker_Send( &worker, "is" ); + LCUI_Sleep( 1 ); + TestWorker_Send( &worker, "test" ); + LCUI_MSleep( 100 ); + TestWorker_Send( &worker, "bye!" ); + LCUI_Sleep( 1 ); + TestWorker_Destroy( &worker ); + CHECK( worker.data_count == 7 ); + return ret; +}