varch/test/test_coroutine.c
Lamdonn 49025692ca Add the initial version coroutine module
TODO:
1. Add feature to bind CoTask and CoTimer to the specified CoScheduler
2. Compatibility with coroutine API highly intensive IO, sockets, etc
3. Improve functional safety
2025-11-11 22:42:25 +08:00

322 lines
7.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#if defined(TEST_TARGET_coroutine)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/coroutine.h>
#else
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "coroutine.h"
#endif
#ifdef _WIN32
#include <windows.h>
uint64_t GetTimerUsec(void)
{
LARGE_INTEGER li;
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&li);
return (uint64_t)li.QuadPart * 1000000 / frequency.QuadPart;
}
#else
#include <sys/time.h>
uint64_t GetTimerUsec(void)
{
struct timeval mstime;
uint64_t us = 0;
gettimeofday(&mstime, NULL);
us = mstime.tv_sec * 1000000 + mstime.tv_usec;
return us;
}
#endif
#define MUTI_THREAD_NUM 2
static pthread_t threads[MUTI_THREAD_NUM];
static pthread_t testThread;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static CoScheduler gMutiScheduler[MUTI_THREAD_NUM];
static CoScheduler gSingleScheduler;
static CoEvent gEvent = COEVENT_STATIC_VALUE;
static CoTask_t gTickCoroutine = NULL;
static CoTask_t gTockCoroutine = NULL;
static uint8_t gStackTick[1024 * 10];
static uint8_t gStackTock[1024 * 10];
static CoTimer_t gTimer0Handle = NULL;
static CoTimer_t gTimer1Handle = NULL;
static void thread_lock(void)
{
pthread_mutex_lock(&mutex);
}
static void thread_unlock(void)
{
pthread_mutex_unlock(&mutex);
}
static void *tick(void *arg)
{
while (1)
{
CoTask_Wait(.ms=1000);
printf("tick-running...\n");
CoEvent_Notify(&gEvent, 0x02);
}
}
static void *tock(void *arg)
{
while (1)
{
uint32_t evs = CoTask_Wait(.pEvent=&gEvent);
if (evs & 0x01)
{
printf("event 0x01 triggered...\n");
}
if (evs & 0x02)
{
printf("event 0x02 triggered...\n");
}
}
}
static void timer0(void)
{
printf("timer0-running...\n");
}
static void timer1(void)
{
printf("timer1-running... %d\n", CoScheduler_CurLoad(CoTimer_Self()->pScheduler));
// CoEvent_Notify(&gEvent, 0x01 | 0x02);
}
/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/
// #define EXIT_TEST
extern uint64_t unitt_clock(void);
static int test_0(void)
{
for (int i = 0; i < 100; i++)
{
if (0)
{
#if defined (EXIT_TEST)
exit(0);
#endif
return UNITT_E_FAIL;
}
}
return UNITT_E_OK;
}
static void unitt_task(void)
{
static UNITT_TCASE rand_tests[] = {
UNITT_TCASE(test_0),
// UNITT_TCASE(test_1),
// UNITT_TCASE(test_2),
};
static UNITT suites[] = {
{ "coroutine suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
};
UNITT_EXE(suites);
}
/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/
static void test_base(void)
{
}
void test_singleThread(void)
{
CoScheduler_Init(&gSingleScheduler, GetTimerUsec, 1000);
gTickCoroutine = CoTask_Create(tick, gStackTick, sizeof(gStackTick));
gTockCoroutine = CoTask_Create(tock, gStackTock, sizeof(gStackTock));
gTimer0Handle = CoTimer_CreateMs(timer0, 1000);
gTimer1Handle = CoTimer_CreateMs(timer1, 1000);
CoScheduler_Start(&gSingleScheduler);
}
static void *muti_thread_entry(void *arg)
{
int threadId = *(int *)arg;
CoScheduler_Init(&gMutiScheduler[threadId], GetTimerUsec, 1000, thread_lock, thread_unlock);
if (threadId == 0)
{
gTickCoroutine = CoTask_Create(tick, gStackTick, sizeof(gStackTick));
}
else
{
gTockCoroutine = CoTask_Create(tock, gStackTock, sizeof(gStackTock));
}
CoScheduler_Start(&gMutiScheduler[threadId]);
}
void test_mutiThread(void)
{
int threadId[MUTI_THREAD_NUM];
for (int i = 0; i < MUTI_THREAD_NUM; i++)
{
threadId[i] = i;
pthread_create(&threads[i], NULL, muti_thread_entry, &threadId[i]);
/* You can bind thread to specific core */
/* So that each CoScheduler can run on different core */
}
for (int i = 0; i < MUTI_THREAD_NUM; i++)
{
pthread_join(threads[i], NULL);
}
}
/************************************************************************************/
/************************************* Command ************************************/
/************************************************************************************/
static void usage(void)
{
printf(
"Usage: coroutine [opt] [arg] ...\n"
"\n"
"options:\n"
" -e <execute> Specifies the function to execute, the default is the <base> test\n"
" <base> Test base function\n"
" <ut> Unit test\n"
" <single> Test single thread\n"
" <muti> Test muti thread\n"
" ...\n"
" -h Print help\n"
" -v Print version\n"
" -u [<period>] Unit test period, unit ms, the default is 1000ms\n"
"\n"
);
}
static int test(int argc, char *argv[])
{
char *execute = NULL;
int ut_period = 1000;
/* reset getopt */
command_opt_init();
while (1)
{
int opt = command_getopt(argc, argv, "e:hvu::");
if (opt == -1) break;
switch (opt)
{
// Add others opt here
case 'u' :
if (command_optarg) ut_period = atoi(command_optarg);
break;
case 'e' :
execute = command_optarg;
break;
case 'v' :
printf("coroutine version %d.%d.%d\r\n", COROUTINE_V_MAJOR, COROUTINE_V_MINOR, COROUTINE_V_PATCH);
return 0;
case '?':
printf("Unknown option `%c`\r\n", command_optopt);
return -1;
case 'h' :
default:
usage();
return 0;
}
}
if (execute)
{
if (!strcmp(execute, "base"))
{
test_base();
}
else if (!strcmp(execute, "ut"))
{
#if defined(TEST_TARGET_coroutine)
while (1)
{
unitt_task();
usleep(1000 * ut_period);
}
#else
printf("create task %d\r\n", task_create(ut_period, unitt_task));
#endif
}
else if (!strcmp(execute, "single"))
{
test_singleThread();
}
else if (!strcmp(execute, "muti"))
{
test_mutiThread();
}
}
else
{
test_base();
}
#if 0
if (1 == command_optind) // no opt
{
}
for (int index = command_optind; index < argc; index++)
{
if (!strcmp(argv[index], "base"))
{
test_base();
}
}
if (1 == argc)
{
test_base();
}
#endif
return 0;
}
/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/
#if defined(TEST_TARGET_coroutine)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_coroutine(void)
{
command_export("coroutine", test);
}
init_export_app(test_coroutine);
#endif