信号槽使用mythread
This commit is contained in:
2
main.c
2
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( );
|
||||
}
|
||||
|
84
soft/debug.c
84
soft/debug.c
@@ -7,73 +7,17 @@
|
||||
#include <sys/sem.h>
|
||||
#include <sys/types.h>
|
||||
#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(level<DBG_LEVEL_INFO||level>DBG_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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@@ -8,18 +8,19 @@
|
||||
#include "stdio.h"
|
||||
#include "errno.h"
|
||||
#include <semaphore.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#include "exception.h"
|
||||
#include "stdio.h"
|
||||
#include "debug.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
|
||||
@@ -31,8 +32,8 @@ static void *p_thread(void *t){
|
||||
}
|
||||
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);
|
||||
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,10 +43,12 @@ 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;
|
||||
if(name)
|
||||
strncpy(m->name, name, FUNC_NAME_LEN - 1);
|
||||
pthread_create(&m->th, NULL, p_thread, m);
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
@@ -60,7 +63,7 @@ int myth_create(int (*func)(void *t),void *t){
|
||||
m->last=myth;
|
||||
}
|
||||
}
|
||||
return m->id;
|
||||
return &m->th;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -6,6 +6,10 @@
|
||||
#include "exception.h"
|
||||
|
||||
|
||||
|
||||
#define FUNC_NAME_LEN 20
|
||||
|
||||
|
||||
typedef struct _myth_def{
|
||||
struct _myth_def *next;
|
||||
struct _myth_def *last;
|
||||
@@ -15,13 +19,14 @@ typedef struct _myth_def{
|
||||
size_t end;
|
||||
int (*func)(void *);
|
||||
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();
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user