Files
c_soft/test/signal_test.c

142 lines
3.3 KiB
C
Raw Normal View History

2025-06-24 10:09:14 +08:00
#include "mysignal.h"
#include "pthread.h"
#include "stdlib.h"
#include "lambda.h"
#include "sys/time.h"
#include "errno.h"
#include "stdio.h"
#include "string.h"
#include "exception.h"
2025-06-24 10:09:14 +08:00
2025-06-24 16:32:32 +08:00
#include "signal_test.h"
2025-06-24 10:09:14 +08:00
// 以下是自动生成的代码示例
2025-06-24 16:32:32 +08:00
// // 把槽函数的参数封装为结构体
// typedef struct {
// slot_list_with_pars slot_;
// void* self;
// int a;
// int b;
// }test_signal_args;
// typedef struct {
// slot_list_with_pars slot_;
// void* self;
// int a;
// int b;
// }test_slot_args;
// // 封装函数用来调用实际的槽函数
// static void test_slot_caller(void* args) {
// test_slot_args* a = args;
// printf("test_slot_caller: %d %d\n", a->a, a->b);
// printf("args_p=%p\n", a);
// test_slot(a->self, a->a, a->b);
// }
// // 信号函数的实现
// signal test_signal(test_sig_obj* self, int a, int b) {
// test_signal_args* pars = NULL;
// slot_list* slot_p = self->__sig_obj.slot_head;
// while (slot_p) {
// pars = calloc(1, sizeof(test_signal_args));
// pars->slot_.func = slot_p->func;
// // 这里self要换成槽的self
// pars->self = slot_p->slot_obj;
// pars->a = a;
// pars->b = b;
// if (slot_p->thread) {
// // 异步调用
// send_slot_fun(slot_p->thread, (slot_list_with_pars*)pars);
// } else {
// // 同步调用
// slot_p->func(pars);
// free(pars);
// }
// slot_p = slot_p->next;
// }
// }
// // 定义一个数据结构来保存槽封装函数与槽函数的关系
// typedef struct {
// void (*func)(void*);
// char *name;
// } func_name;
// static const func_name g_func_table[] = {
// {
// .func = test_slot_caller,
// .name = "test_slot"
// }
// };
// void* signal_find_slot_func(const char* name) {
// for (int i = 0; i < sizeof(g_func_table) / sizeof(func_name); i++) {
// if (strcmp(name, g_func_table[i].name) == 0) {
// return g_func_table[i].func;
// }
// }
// return 0;
// }
2025-06-24 10:09:14 +08:00
// 自动生成代码示例结束
static void mdelay(unsigned long mSec){
struct timeval tv;
tv.tv_sec=mSec/1000;
tv.tv_usec=(mSec%1000)*1000;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}
2025-06-24 16:32:32 +08:00
// 定义槽函数
slot test_slot(test_sig_obj2* obj, int a, int b) {
printf("test_slot var=%d\n", obj->test_var);
obj->test_var = a + b;
printf("test_slot var=%d\n", obj->test_var);
}
slot test_slot3(test_sig_obj3* obj, int a, int b) {
printf("test_slot3 a=%d, b=%d\n", a, b);
printf("obj->v1=%d, obj->v2=%d\n", obj->test_var, obj->test_var2);
}
slot test_slot3_2(test_sig_obj3* obj, int a, int b, const char* c) {
printf("test_slot3_2 a=%d, b=%d, c=%s\n", a, b, c);
}
2025-06-24 10:09:14 +08:00
static test_sig_obj g_sig_obj = { .test_var = 1, };
static test_sig_obj2 g_sig_obj2 = { .test_var = 2, };
2025-06-24 16:32:32 +08:00
static test_sig_obj3 g_sig_obj3;
2025-06-24 10:09:14 +08:00
int thread_fun_s(void* t) {
2025-06-24 10:09:14 +08:00
mythread_t* th = sigthread_init();
printf("thread_fun start\n");
connect(&g_sig_obj, test_signal, th, &g_sig_obj2, test_slot);
2025-06-24 16:32:32 +08:00
connect(&g_sig_obj, test_signal, th, &g_sig_obj3, test_slot3);
connect(&g_sig_obj, test_signal2, th, &g_sig_obj3, test_slot3_2);
2025-06-24 10:09:14 +08:00
mdelay(1000);
emit test_signal(&g_sig_obj, 2, 3);
mdelay(1000);
2025-06-24 16:32:32 +08:00
emit test_signal2(&g_sig_obj, 5, 6,"hello world");
mdelay(1000);
printf("test end\n");
throw_("my err");
2025-06-24 10:09:14 +08:00
return 0;
}