信号槽使用mythread

This commit is contained in:
2025-06-25 11:29:06 +08:00
parent b64401d556
commit 2cfdb4a84f
8 changed files with 43 additions and 101 deletions

2
main.c
View File

@@ -56,7 +56,7 @@ int main(int argc, char* argv[]) {
DBG_INFO("hello world.%ld\n", (size_t)pthread_self()); DBG_INFO("hello world.%ld\n", (size_t)pthread_self());
myth_create(thread_fun, NULL); myth_create(thread_fun, NULL,"main_thread");
myth_join( ); myth_join( );
} }

View File

@@ -7,73 +7,17 @@
#include <sys/sem.h> #include <sys/sem.h>
#include <sys/types.h> #include <sys/types.h>
#include "errno.h" #include "errno.h"
#include "pthread.h"
#define CONSOLEBUF_SIZE 1024 #define CONSOLEBUF_SIZE 1024
#ifdef LINUX
union semun{
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO
(Linux-specific) */
};
void _sem_take(int semid)//得到钥匙
{
struct sembuf sop;
sop.sem_num = 0;//控制第一个信号量
sop.sem_op = -1;//钥匙数量减一6
sop.sem_flg = SEM_UNDO;
semop(semid,&sop,1);
}
void _sem_relase(int semid)//放回钥匙
{
struct sembuf sop;
sop.sem_num = 0;
sop.sem_op = 1;
sop.sem_flg = SEM_UNDO;
semop(semid,&sop,1);
}
int _sem_init(){
key_t key;
int mutex;
key = ftok(".",5345);
// printf("sem init, key=%llu\n",key);
mutex = semget(key,1,IPC_CREAT);//创建信号量
// printf("sem init, mutex=%d\n",mutex);
if(mutex<=0){
// printf("%d\n",errno);
}
union semun set;
set.val = 1;//钥匙数量为0
semctl(mutex,0,SETVAL,set);
return mutex;
}
#endif
#ifdef RT_THREAD
#include "rtthread.h"
#endif
typedef struct{ typedef struct{
int inited; int inited;
int print_context; int print_context;
#ifdef RT_THREAD pthread_mutex_t mutex; /* 互斥锁定义 */
struct rt_mutex mutex;
#endif
#ifdef LINUX
int mutex;
#endif
dbg_dev *dev; dbg_dev *dev;
}self_def; }self_def;
@@ -105,17 +49,12 @@ int debug_init(dbg_dev *dev)
} }
if(self->inited==0) if(self->inited==0)
{ {
#ifdef RT_THREAD pthread_mutex_init(&self->mutex, NULL);
rt_mutex_init(&self->mutex,"debug_mutex",RT_IPC_FLAG_FIFO);
#endif
#ifdef LINUX
self->mutex=_sem_init();
#endif
self->dev=dev; self->dev=dev;
self->dev->init(); self->dev->init();
self->dev->write((const uint8_t *)"\r\n",2); self->dev->write((const uint8_t *)"\r\n",2);
self->inited = 1; self->inited = 1;
debug_print_context(1); debug_print_context(0);
DBG_LOG("debug inited.\r\n"); DBG_LOG("debug inited.\r\n");
} }
return 0; return 0;
@@ -136,12 +75,7 @@ void debug_log(const char *file,const char *fun,int line,int level,const char *f
static const char *level_str[]={"[info] ","[log] ","[warn] ","[err] "}; static const char *level_str[]={"[info] ","[log] ","[warn] ","[err] "};
static int level_str_len[]={7,6,7,6}; static int level_str_len[]={7,6,7,6};
if(level<DBG_LEVEL_INFO||level>DBG_LEVEL_ERR) return; if(level<DBG_LEVEL_INFO||level>DBG_LEVEL_ERR) return;
#ifdef RT_THREAD pthread_mutex_lock(&self->mutex);
rt_mutex_take(&self->mutex,RT_WAITING_FOREVER);
#endif
#ifdef LINUX
_sem_take(self->mutex);
#endif
memcpy(log_buf,level_str[level],level_str_len[level]); memcpy(log_buf,level_str[level],level_str_len[level]);
length = level_str_len[level]; length = level_str_len[level];
if (self->print_context) { if (self->print_context) {
@@ -159,12 +93,8 @@ void debug_log(const char *file,const char *fun,int line,int level,const char *f
} }
log_buf[length]=0; log_buf[length]=0;
self->dev->write((const uint8_t *)log_buf,length); self->dev->write((const uint8_t *)log_buf,length);
#ifdef RT_THREAD pthread_mutex_unlock(&self->mutex);
rt_mutex_release(&self->mutex);
#endif
#ifdef LINUX
_sem_relase(self->mutex);
#endif
} }

View File

@@ -8,18 +8,19 @@
#include "stdio.h" #include "stdio.h"
#include "errno.h" #include "errno.h"
#include <semaphore.h> #include <semaphore.h>
#include "exception.h"
#include "mythread.h"
#include "debug.h"
static int _thread(void* arg) {
static void* _thread(void* arg) {
mythread_t* self = arg; mythread_t* self = arg;
slot_list_with_pars *slot_p; slot_list_with_pars *slot_p;
while (1) { while (1) {
slot_p = NULL; slot_p = NULL;
sem_wait(&self->sem); sem_wait(&self->sem);
pthread_mutex_lock(&self->lock); pthread_mutex_lock(&self->lock);
printf("sem take\n"); DBG_INFO("sem take\n");
// 把槽列表里的函数取出来 // 把槽列表里的函数取出来
if (self->slot_head) { if (self->slot_head) {
slot_p = self->slot_head; slot_p = self->slot_head;
@@ -28,14 +29,14 @@ static void* _thread(void* arg) {
pthread_mutex_unlock(&self->lock); pthread_mutex_unlock(&self->lock);
if (slot_p) { if (slot_p) {
if (slot_p->func) { if (slot_p->func) {
printf("args_p=%p\n", slot_p); DBG_INFO("args_p=%p\n", slot_p);
slot_p->func(slot_p); slot_p->func(slot_p);
} }
// 槽函数执行完之后释放 // 槽函数执行完之后释放
free(slot_p); free(slot_p);
} }
} }
return NULL; return 0;
} }
@@ -52,7 +53,7 @@ void send_slot_fun(mythread_t *self, slot_list_with_pars* slot_p) {
} }
pthread_mutex_unlock(&self->lock); pthread_mutex_unlock(&self->lock);
sem_post(&self->sem); sem_post(&self->sem);
printf("add_slot_fun\n"); DBG_INFO("add_slot_fun\n");
} }
@@ -79,11 +80,12 @@ void _connect(void* sig_obj, void* sig_fun, mythread_t* thread, void* slot_obj,
} }
mythread_t *sigthread_init() { mythread_t *sigthread_init(const char *name) {
mythread_t* self = calloc(1, sizeof(mythread_t)); mythread_t* self = calloc(1, sizeof(mythread_t));
pthread_mutex_init(&self->lock, NULL); pthread_mutex_init(&self->lock, NULL);
sem_init(&self->sem, 0, 0); sem_init(&self->sem, 0, 0);
pthread_create(&self->tid, NULL, _thread, self); // pthread_create(&self->tid, NULL, _thread, self);
self->tid = myth_create(_thread, self, name);
return self; return self;
} }

View File

@@ -20,7 +20,7 @@ typedef struct {
typedef struct { typedef struct {
pthread_mutex_t lock; /* 互斥锁定义 */ pthread_mutex_t lock; /* 互斥锁定义 */
sem_t sem; sem_t sem;
pthread_t tid; pthread_t *tid;
slot_list_with_pars *slot_head; slot_list_with_pars *slot_head;
} mythread_t; } mythread_t;
@@ -41,7 +41,7 @@ typedef struct {
void send_slot_fun(mythread_t* self, slot_list_with_pars* slot_p); void send_slot_fun(mythread_t* self, slot_list_with_pars* slot_p);
mythread_t* sigthread_init(); mythread_t* sigthread_init(const char* name);
void _connect(void* sig_obj, void* sig_fun, mythread_t* thread, void* slot_obj, const char* slot_fun); void _connect(void* sig_obj, void* sig_fun, mythread_t* thread, void* slot_obj, const char* slot_fun);

View File

@@ -6,6 +6,7 @@
#include "exception.h" #include "exception.h"
#include "stdio.h" #include "stdio.h"
#include "debug.h" #include "debug.h"
#include "string.h"
@@ -30,9 +31,9 @@ static void *p_thread(void *t){
throw_("th->func was null."); throw_("th->func was null.");
} }
th->func(th->arg); th->func(th->arg);
}catch_{ }catch_ {
DBG_WARN("func:%08lx failed.\n",(size_t)th->func); DBG_WARN("thread(%s)=%08lx failed.\n",th->name,(size_t)th->func);
DBG_WARN("file=%s,line=%d,err=%s\n",exception()->file,exception()->line,exception()->log); DBG_WARN("%s:%d err(%s)\n",exception()->file,exception()->line,exception()->log);
} }
DBG_INFO("func:%08lx end.\n",(size_t)th->func); DBG_INFO("func:%08lx end.\n",(size_t)th->func);
th->end=1; th->end=1;
@@ -42,11 +43,13 @@ static void *p_thread(void *t){
int myth_create(int (*func)(void *t),void *t){ pthread_t* myth_create(int (*func)(void *t),void *t,const char *name){
myth_def *m=calloc(sizeof(myth_def),1); myth_def *m=calloc(sizeof(myth_def),1);
m->func=func; m->func=func;
m->arg=t; m->arg = t;
pthread_create(&m->th,NULL,p_thread,m); if(name)
strncpy(m->name, name, FUNC_NAME_LEN - 1);
pthread_create(&m->th, NULL, p_thread, m);
{ {
self_def *s=&g_self; self_def *s=&g_self;
if(s->head==NULL){ if(s->head==NULL){
@@ -60,7 +63,7 @@ int myth_create(int (*func)(void *t),void *t){
m->last=myth; m->last=myth;
} }
} }
return m->id; return &m->th;
} }

View File

@@ -6,6 +6,10 @@
#include "exception.h" #include "exception.h"
#define FUNC_NAME_LEN 20
typedef struct _myth_def{ typedef struct _myth_def{
struct _myth_def *next; struct _myth_def *next;
struct _myth_def *last; struct _myth_def *last;
@@ -14,14 +18,15 @@ typedef struct _myth_def{
size_t id; size_t id;
size_t end; size_t end;
int (*func)(void *); int (*func)(void *);
void *arg; void* arg;
char name[FUNC_NAME_LEN];
}myth_def; }myth_def;
int myth_create(int (*func)(void *t),void *t); pthread_t* myth_create(int (*func)(void *t),void *t,const char *name);
int myth_join(); int myth_join();
myth_def *myth_self(); myth_def *myth_self();

View File

@@ -39,7 +39,7 @@ long get_file_size(FILE *stream)
riscv_t riscv = { 0 }; riscv_t riscv = { 0 };
int thread_fun(void* t) int thread_fun_r(void* t)
{ {
int argc; int argc;
char** argv; char** argv;

View File

@@ -7,6 +7,7 @@
#include "stdio.h" #include "stdio.h"
#include "string.h" #include "string.h"
#include "exception.h" #include "exception.h"
#include "debug.h"
#include "signal_test.h" #include "signal_test.h"
@@ -115,14 +116,15 @@ slot test_slot3(test_sig_obj3* obj, int a, int b) {
slot test_slot3_2(test_sig_obj3* obj, int a, int b, const char* c) { 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); printf("test_slot3_2 a=%d, b=%d, c=%s\n", a, b, c);
throw_("test_slot3_2 err");
} }
static test_sig_obj g_sig_obj = { .test_var = 1, }; static test_sig_obj g_sig_obj = { .test_var = 1, };
static test_sig_obj2 g_sig_obj2 = { .test_var = 2, }; static test_sig_obj2 g_sig_obj2 = { .test_var = 2, };
static test_sig_obj3 g_sig_obj3; static test_sig_obj3 g_sig_obj3;
int thread_fun_s(void* t) { int thread_fun(void* t) {
mythread_t* th = sigthread_init(); mythread_t* th = sigthread_init("signal_thread");
printf("thread_fun start\n"); printf("thread_fun start\n");
connect(&g_sig_obj, test_signal, th, &g_sig_obj2, test_slot); connect(&g_sig_obj, test_signal, th, &g_sig_obj2, test_slot);
connect(&g_sig_obj, test_signal, th, &g_sig_obj3, test_slot3); connect(&g_sig_obj, test_signal, th, &g_sig_obj3, test_slot3);