2026-05-23 18:46:07 +08:00
|
|
|
/*
|
|
|
|
|
* lib/thread/tests/test_thread.c
|
|
|
|
|
*
|
2026-06-27 23:50:18 +08:00
|
|
|
* Copyright (c) 2023-2026, Liu Chao <hello@lcui.dev> All rights reserved.
|
2026-05-23 18:46:07 +08:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
2026-06-27 23:50:18 +08:00
|
|
|
* This file is part of LCUI, distributed under the MIT License found
|
|
|
|
|
* in the LICENSE.TXT file in the root directory of this source tree.
|
2026-05-23 18:46:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <thread.h>
|
|
|
|
|
#include <yutil.h>
|
|
|
|
|
#include <ctest.h>
|
|
|
|
|
|
|
|
|
|
typedef struct TestWorkerRec_ {
|
|
|
|
|
char data[32];
|
|
|
|
|
int data_count;
|
|
|
|
|
bool cancel;
|
|
|
|
|
bool active;
|
|
|
|
|
thread_cond_t cond;
|
|
|
|
|
thread_mutex_t mutex;
|
|
|
|
|
thread_t thread;
|
|
|
|
|
} TestWorkerRec, *TestWorker;
|
|
|
|
|
|
|
|
|
|
static void TestWorker_Thread(void *arg)
|
|
|
|
|
{
|
|
|
|
|
TestWorker worker = arg;
|
|
|
|
|
|
|
|
|
|
worker->cancel = false;
|
|
|
|
|
worker->active = true;
|
|
|
|
|
worker->data_count = 0;
|
|
|
|
|
thread_mutex_lock(&worker->mutex);
|
|
|
|
|
while (!worker->cancel && worker->data_count < 20) {
|
|
|
|
|
logger_debug("waiting...\n");
|
|
|
|
|
thread_cond_wait(&worker->cond, &worker->mutex);
|
|
|
|
|
logger_debug("get data: %s\n", worker->data);
|
|
|
|
|
worker->data_count += 1;
|
|
|
|
|
}
|
|
|
|
|
thread_mutex_unlock(&worker->mutex);
|
|
|
|
|
logger_debug("count: %d\n", worker->data_count);
|
|
|
|
|
worker->active = false;
|
|
|
|
|
thread_exit(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void TestWorker_Send(TestWorker worker, const char *data)
|
|
|
|
|
{
|
|
|
|
|
thread_mutex_lock(&worker->mutex);
|
|
|
|
|
strcpy(worker->data, data);
|
|
|
|
|
thread_cond_signal(&worker->cond);
|
|
|
|
|
thread_mutex_unlock(&worker->mutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void TestWorker_Init(TestWorker worker)
|
|
|
|
|
{
|
|
|
|
|
thread_mutex_init(&worker->mutex);
|
|
|
|
|
thread_cond_init(&worker->cond);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void TestWorker_Destroy(TestWorker worker)
|
|
|
|
|
{
|
|
|
|
|
thread_mutex_lock(&worker->mutex);
|
|
|
|
|
worker->cancel = true;
|
|
|
|
|
thread_cond_signal(&worker->cond);
|
|
|
|
|
thread_mutex_unlock(&worker->mutex);
|
|
|
|
|
thread_join(worker->thread, NULL);
|
|
|
|
|
thread_mutex_destroy(&worker->mutex);
|
|
|
|
|
thread_cond_destroy(&worker->cond);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_thread_worker(void)
|
|
|
|
|
{
|
|
|
|
|
TestWorkerRec worker;
|
|
|
|
|
|
|
|
|
|
TestWorker_Init(&worker);
|
|
|
|
|
thread_create(&worker.thread, TestWorker_Thread, &worker);
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(100);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Send(&worker, "hello");
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(20);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Send(&worker, "world");
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(100);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Send(&worker, "this");
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(50);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Send(&worker, "is");
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(100);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Send(&worker, "test");
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(50);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Send(&worker, "bye!");
|
2026-06-28 17:42:32 +08:00
|
|
|
y_sleep(100);
|
2026-05-23 18:46:07 +08:00
|
|
|
TestWorker_Destroy(&worker);
|
|
|
|
|
ctest_equal_int("should receive 7 data items", worker.data_count, 7);
|
|
|
|
|
ctest_equal_bool("should no longer be active", worker.active, false);
|
|
|
|
|
}
|