Skip to content

Commit

Permalink
fix(worker): worker did not enter the blocked state waiting for task (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Feb 9, 2018
1 parent d65de98 commit 8dae96f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -107,6 +108,7 @@ static void LCUIWorker_Thread( void *arg )
LCUICond_Wait( &worker->cond, &worker->mutex );
}
}
LCUIMutex_Unlock( &worker->mutex );
LCUIThread_Exit( NULL );
}

Expand Down
4 changes: 2 additions & 2 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
82 changes: 82 additions & 0 deletions test/test_thread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <string.h>
#include <stdio.h>
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/thread.h>
#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;
}

0 comments on commit 8dae96f

Please sign in to comment.