Skip to content

Commit 8dae96f

Browse files
committed
fix(worker): worker did not enter the blocked state waiting for task (#134)
1 parent d65de98 commit 8dae96f

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

src/worker.c

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ LCUI_BOOL LCUIWorker_RunTask( LCUI_Worker worker )
9999
static void LCUIWorker_Thread( void *arg )
100100
{
101101
LCUI_Worker worker = arg;
102+
LCUIMutex_Lock( &worker->mutex );
102103
while( worker->active ) {
103104
if( LCUIWorker_RunTask( worker ) ) {
104105
continue;
@@ -107,6 +108,7 @@ static void LCUIWorker_Thread( void *arg )
107108
LCUICond_Wait( &worker->cond, &worker->mutex );
108109
}
109110
}
111+
LCUIMutex_Unlock( &worker->mutex );
110112
LCUIThread_Exit( NULL );
111113
}
112114

test/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ helloworld_LDADD = $(top_builddir)/src/libLCUI.la -lm
1111

1212
test_SOURCES = test.c test_css_parser.c test_xml_parser.c test_string.c \
1313
test_font_load.c test_image_reader.c test_widget_rect.c test_widget_layout.c \
14-
test_widget_flex_layout.c test_widget_inline_block_layout.c\
15-
test_char_render.c test_string_render.c test_widget_render.c
14+
test_widget_flex_layout.c test_widget_inline_block_layout.c test_thread.c \
15+
test_char_render.c test_string_render.c test_widget_render.c
1616

1717
test_LDADD = $(top_builddir)/src/libLCUI.la -lm $(CODE_COVERAGE_LIBS)
1818

test/test.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ int main( void )
2323
Logger_SetHandler( LoggerHandler );
2424
Logger_SetHandlerW( LoggerHandlerW );
2525
#endif
26-
ret += test_font_load();
2726
ret += test_string();
27+
ret += test_thread();
28+
ret += test_font_load();
2829
ret += test_image_reader();
2930
ret += test_css_parser();
3031
ret += test_xml_parser();

test/test.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Logger_Log("[test] %d tests, %d pass.\n", tests_count, tests_count + N)
2727
} while( 0 );
2828

2929
int test_string( void );
30+
int test_thread( void );
3031
int test_font_load( void );
3132
int test_css_parser( void );
3233
int test_xml_parser( void );

test/test_thread.c

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <string.h>
2+
#include <stdio.h>
3+
#include <LCUI_Build.h>
4+
#include <LCUI/LCUI.h>
5+
#include <LCUI/thread.h>
6+
#include "test.h"
7+
8+
typedef struct TestWorkerRec_ {
9+
char data[32];
10+
int data_count;
11+
LCUI_BOOL active;
12+
LCUI_Cond cond;
13+
LCUI_Mutex mutex;
14+
LCUI_Thread thread;
15+
} TestWorkerRec, *TestWorker;
16+
17+
static void TestWorker_Thread( void *arg )
18+
{
19+
TestWorker worker = arg;
20+
21+
worker->active = TRUE;
22+
worker->data_count = 0;
23+
LCUIMutex_Lock( &worker->mutex );
24+
while( worker->active ) {
25+
TEST_LOG( "waiting...\n" );
26+
LCUICond_Wait( &worker->cond, &worker->mutex );
27+
TEST_LOG( "get data: %s\n", worker->data );
28+
worker->data_count += 1;
29+
}
30+
LCUIMutex_Unlock( &worker->mutex );
31+
TEST_LOG( "count: %lu\n", worker->data_count );
32+
}
33+
34+
static void TestWorker_Send( TestWorker worker, const char *data )
35+
{
36+
LCUIMutex_Lock( &worker->mutex );
37+
strcpy( worker->data, data );
38+
LCUICond_Signal( &worker->cond );
39+
LCUIMutex_Unlock( &worker->mutex );
40+
}
41+
42+
static void TestWorker_Init( TestWorker worker )
43+
{
44+
LCUIMutex_Init( &worker->mutex );
45+
LCUICond_Init( &worker->cond );
46+
}
47+
48+
static void TestWorker_Destroy( TestWorker worker )
49+
{
50+
LCUIMutex_Lock( &worker->mutex );
51+
worker->active = FALSE;
52+
LCUICond_Signal( &worker->cond );
53+
LCUIMutex_Unlock( &worker->mutex );
54+
LCUIThread_Join( worker->thread, NULL );
55+
LCUIMutex_Destroy( &worker->mutex );
56+
LCUICond_Destroy( &worker->cond );
57+
}
58+
59+
int test_thread( void )
60+
{
61+
int ret = 0;
62+
TestWorkerRec worker;
63+
64+
TestWorker_Init( &worker );
65+
LCUIThread_Create( &worker.thread, TestWorker_Thread, &worker );
66+
LCUI_Sleep( 1 );
67+
TestWorker_Send( &worker, "hello" );
68+
LCUI_MSleep( 200 );
69+
TestWorker_Send( &worker, "world" );
70+
LCUI_Sleep( 1 );
71+
TestWorker_Send( &worker, "this" );
72+
LCUI_MSleep( 500 );
73+
TestWorker_Send( &worker, "is" );
74+
LCUI_Sleep( 1 );
75+
TestWorker_Send( &worker, "test" );
76+
LCUI_MSleep( 100 );
77+
TestWorker_Send( &worker, "bye!" );
78+
LCUI_Sleep( 1 );
79+
TestWorker_Destroy( &worker );
80+
CHECK( worker.data_count == 7 );
81+
return ret;
82+
}

0 commit comments

Comments
 (0)