mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-12-01 16:56:34 +01:00
fix(worker): worker did not enter the blocked state waiting for task (#134)
This commit is contained in:
@@ -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 );
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -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)
|
||||
|
||||
|
||||
+2
-1
@@ -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();
|
||||
|
||||
@@ -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 );
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user