Files
LCUI/test/cases/test_thread.c
T
YDYKand李梓豪 b46306ddea refactor(util): add yutil library (#264)
* add yutil

* add yutil to util.h

* remove util file that same as yutil's

* replace the old header file like '#include <LCUI/util/*>' with yutil's head file

* Replace the old UTIL module associated data structures and function call code

* change timer

* Replace the old UTIL module associated data structures and function call code

* fix

* fix compilation problems(RGB to YUTIL_TEST_RGB)

* turn some useless changes back to origin

* change YUTIL_TEST_RGB back to RGB and add  lcui-util/xmake.lua include

* update github action

* update github action

* update github action

* change new yutil func name

* update xmake and change way to include yutil

* update include to <yutil.h>

* update list_for_each to for(list_each())

* fix

* add graph.h to test_*

* add #include <LCUI/graph.h>

* update yutil

* change timer func name and update submodule

* update submodules

* test strncpy

* turn useless code back origin

* fix lose RGB issue

* fix yutil charset

* update yutil

* update yutil

* add yutil_test instead of lcui-util test

* change timer func name style

Co-authored-by: 李梓豪 <yidianyiko@foxmail.com>
2023-04-01 22:34:33 +08:00

86 lines
2.0 KiB
C

#include <string.h>
#include <stdio.h>
#include <LCUI.h>
#include <LCUI/thread.h>
#include <yutil.h>
#include "ctest.h"
typedef struct TestWorkerRec_ {
char data[32];
int data_count;
LCUI_BOOL cancel;
LCUI_BOOL active;
LCUI_Cond cond;
LCUI_Mutex mutex;
LCUI_Thread thread;
} TestWorkerRec, *TestWorker;
static void TestWorker_Thread(void *arg)
{
TestWorker worker = arg;
worker->cancel = FALSE;
worker->active = TRUE;
worker->data_count = 0;
LCUIMutex_Lock(&worker->mutex);
while (!worker->cancel && worker->data_count < 20) {
logger_debug("waiting...\n");
LCUICond_Wait(&worker->cond, &worker->mutex);
logger_debug("get data: %s\n", worker->data);
worker->data_count += 1;
}
LCUIMutex_Unlock(&worker->mutex);
logger_debug("count: %d\n", worker->data_count);
worker->active = FALSE;
LCUIThread_Exit(NULL);
}
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->cancel = TRUE;
LCUICond_Signal(&worker->cond);
LCUIMutex_Unlock(&worker->mutex);
LCUIThread_Join(worker->thread, NULL);
LCUIMutex_Destroy(&worker->mutex);
LCUICond_Destroy(&worker->cond);
}
void test_thread(void)
{
TestWorkerRec worker;
TestWorker_Init(&worker);
LCUIThread_Create(&worker.thread, TestWorker_Thread, &worker);
sleep_ms(100);
TestWorker_Send(&worker, "hello");
sleep_ms(20);
TestWorker_Send(&worker, "world");
sleep_ms(100);
TestWorker_Send(&worker, "this");
sleep_ms(50);
TestWorker_Send(&worker, "is");
sleep_ms(100);
TestWorker_Send(&worker, "test");
sleep_ms(50);
TestWorker_Send(&worker, "bye!");
sleep_ms(100);
TestWorker_Destroy(&worker);
it_i("check worker data count", worker.data_count, 7);
it_b("check worker is no longer active", worker.active, FALSE);
}