From 2cfdb4a84f17bea0953ff471025c28944364f12c Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Wed, 25 Jun 2025 11:29:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=A1=E5=8F=B7=E6=A7=BD=E4=BD=BF=E7=94=A8my?= =?UTF-8?q?thread?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 2 +- soft/debug.c | 84 ++++------------------------------------------ soft/mysignal.c | 20 ++++++----- soft/mysignal.h | 4 +-- soft/mythread.c | 17 ++++++---- soft/mythread.h | 9 +++-- test/riscv_test.c | 2 +- test/signal_test.c | 6 ++-- 8 files changed, 43 insertions(+), 101 deletions(-) diff --git a/main.c b/main.c index cccfd6a..4fa03a8 100644 --- a/main.c +++ b/main.c @@ -56,7 +56,7 @@ int main(int argc, char* argv[]) { DBG_INFO("hello world.%ld\n", (size_t)pthread_self()); - myth_create(thread_fun, NULL); + myth_create(thread_fun, NULL,"main_thread"); myth_join( ); } diff --git a/soft/debug.c b/soft/debug.c index 2e8cc9b..f38957f 100644 --- a/soft/debug.c +++ b/soft/debug.c @@ -7,73 +7,17 @@ #include #include #include "errno.h" +#include "pthread.h" #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{ int inited; int print_context; -#ifdef RT_THREAD - struct rt_mutex mutex; - #endif - #ifdef LINUX - int mutex; - #endif + pthread_mutex_t mutex; /* 互斥锁定义 */ dbg_dev *dev; }self_def; @@ -105,17 +49,12 @@ int debug_init(dbg_dev *dev) } if(self->inited==0) { - #ifdef RT_THREAD - rt_mutex_init(&self->mutex,"debug_mutex",RT_IPC_FLAG_FIFO); - #endif - #ifdef LINUX - self->mutex=_sem_init(); - #endif + pthread_mutex_init(&self->mutex, NULL); self->dev=dev; self->dev->init(); self->dev->write((const uint8_t *)"\r\n",2); self->inited = 1; - debug_print_context(1); + debug_print_context(0); DBG_LOG("debug inited.\r\n"); } 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 int level_str_len[]={7,6,7,6}; if(levelDBG_LEVEL_ERR) return; - #ifdef RT_THREAD - rt_mutex_take(&self->mutex,RT_WAITING_FOREVER); - #endif - #ifdef LINUX - _sem_take(self->mutex); - #endif + pthread_mutex_lock(&self->mutex); memcpy(log_buf,level_str[level],level_str_len[level]); length = level_str_len[level]; 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; self->dev->write((const uint8_t *)log_buf,length); - #ifdef RT_THREAD - rt_mutex_release(&self->mutex); - #endif - #ifdef LINUX - _sem_relase(self->mutex); - #endif + pthread_mutex_unlock(&self->mutex); + } diff --git a/soft/mysignal.c b/soft/mysignal.c index ea09f21..4757645 100644 --- a/soft/mysignal.c +++ b/soft/mysignal.c @@ -8,18 +8,19 @@ #include "stdio.h" #include "errno.h" #include +#include "exception.h" +#include "mythread.h" +#include "debug.h" - - -static void* _thread(void* arg) { +static int _thread(void* arg) { mythread_t* self = arg; slot_list_with_pars *slot_p; while (1) { slot_p = NULL; sem_wait(&self->sem); pthread_mutex_lock(&self->lock); - printf("sem take\n"); + DBG_INFO("sem take\n"); // 把槽列表里的函数取出来 if (self->slot_head) { slot_p = self->slot_head; @@ -28,14 +29,14 @@ static void* _thread(void* arg) { pthread_mutex_unlock(&self->lock); if (slot_p) { if (slot_p->func) { - printf("args_p=%p\n", slot_p); + DBG_INFO("args_p=%p\n", slot_p); slot_p->func(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); 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)); pthread_mutex_init(&self->lock, NULL); 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; } diff --git a/soft/mysignal.h b/soft/mysignal.h index 87f012f..74220bd 100644 --- a/soft/mysignal.h +++ b/soft/mysignal.h @@ -20,7 +20,7 @@ typedef struct { typedef struct { pthread_mutex_t lock; /* 互斥锁定义 */ sem_t sem; - pthread_t tid; + pthread_t *tid; slot_list_with_pars *slot_head; } mythread_t; @@ -41,7 +41,7 @@ typedef struct { 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); diff --git a/soft/mythread.c b/soft/mythread.c index c97534c..012a00d 100644 --- a/soft/mythread.c +++ b/soft/mythread.c @@ -6,6 +6,7 @@ #include "exception.h" #include "stdio.h" #include "debug.h" +#include "string.h" @@ -30,9 +31,9 @@ static void *p_thread(void *t){ throw_("th->func was null."); } th->func(th->arg); - }catch_{ - DBG_WARN("func:%08lx failed.\n",(size_t)th->func); - DBG_WARN("file=%s,line=%d,err=%s\n",exception()->file,exception()->line,exception()->log); + }catch_ { + DBG_WARN("thread(%s)=%08lx failed.\n",th->name,(size_t)th->func); + DBG_WARN("%s:%d err(%s)\n",exception()->file,exception()->line,exception()->log); } DBG_INFO("func:%08lx end.\n",(size_t)th->func); 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); m->func=func; - m->arg=t; - pthread_create(&m->th,NULL,p_thread,m); + m->arg = t; + if(name) + strncpy(m->name, name, FUNC_NAME_LEN - 1); + pthread_create(&m->th, NULL, p_thread, m); { self_def *s=&g_self; if(s->head==NULL){ @@ -60,7 +63,7 @@ int myth_create(int (*func)(void *t),void *t){ m->last=myth; } } - return m->id; + return &m->th; } diff --git a/soft/mythread.h b/soft/mythread.h index d9954dd..750d516 100644 --- a/soft/mythread.h +++ b/soft/mythread.h @@ -6,6 +6,10 @@ #include "exception.h" + +#define FUNC_NAME_LEN 20 + + typedef struct _myth_def{ struct _myth_def *next; struct _myth_def *last; @@ -14,14 +18,15 @@ typedef struct _myth_def{ size_t id; size_t end; int (*func)(void *); - void *arg; + void* arg; + char name[FUNC_NAME_LEN]; }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(); myth_def *myth_self(); diff --git a/test/riscv_test.c b/test/riscv_test.c index a71993d..dcaf65e 100644 --- a/test/riscv_test.c +++ b/test/riscv_test.c @@ -39,7 +39,7 @@ long get_file_size(FILE *stream) riscv_t riscv = { 0 }; -int thread_fun(void* t) +int thread_fun_r(void* t) { int argc; char** argv; diff --git a/test/signal_test.c b/test/signal_test.c index 9b5f06a..34d59b6 100644 --- a/test/signal_test.c +++ b/test/signal_test.c @@ -7,6 +7,7 @@ #include "stdio.h" #include "string.h" #include "exception.h" +#include "debug.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) { 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_obj2 g_sig_obj2 = { .test_var = 2, }; static test_sig_obj3 g_sig_obj3; -int thread_fun_s(void* t) { - mythread_t* th = sigthread_init(); +int thread_fun(void* t) { + mythread_t* th = sigthread_init("signal_thread"); 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_obj3, test_slot3);