88 lines
1.5 KiB
C
88 lines
1.5 KiB
C
|
|
|
|
#include "pthread.h"
|
|
#include "unistd.h"
|
|
#include "mythread.h"
|
|
#include "exception.h"
|
|
#include "stdio.h"
|
|
|
|
|
|
typedef struct _myth_def{
|
|
struct _myth_def *next;
|
|
struct _myth_def *last;
|
|
pthread_t th;
|
|
exception_def except;
|
|
size_t id;
|
|
int (*func)(void *);
|
|
void *arg;
|
|
}myth_def;
|
|
|
|
typedef struct{
|
|
myth_def *head;
|
|
|
|
}self_def;
|
|
static self_def g_self;
|
|
|
|
|
|
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|
void *(*start_routine) (void *), void *arg);
|
|
|
|
|
|
|
|
static void *p_thread(void *t){
|
|
myth_def *th=(myth_def *)t;
|
|
th->id=(size_t)pthread_self();
|
|
printf("func:%08lx start id=%d.\n",(size_t)th->func,th->id);
|
|
try_{
|
|
if(!th->func){
|
|
throw_("th->func was null.");
|
|
}
|
|
th->func(th->arg);
|
|
}catch_{
|
|
printf("func:%08lx failed.\n",(size_t)th->func);
|
|
printf("file=%s,line=%d,err=%s\n",exception()->file,exception()->line,exception()->log);
|
|
}
|
|
printf("func:%08lx end.\n",(size_t)th->func);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
int myth_create(int (*func)(void *t),void *t){
|
|
myth_def *m=calloc(sizeof(myth_def),1);
|
|
m->func=func;
|
|
m->arg=t;
|
|
pthread_create(&m->th,NULL,p_thread,m);
|
|
{
|
|
self_def *s=&g_self;
|
|
if(s->head==NULL){
|
|
s->head=m;
|
|
}else{
|
|
myth_def *myth=s->head;
|
|
while(myth->next){
|
|
myth=myth->next;
|
|
}
|
|
myth->next=m;
|
|
m->last=myth;
|
|
}
|
|
}
|
|
return m->id;
|
|
}
|
|
|
|
|
|
int myth_join(){
|
|
self_def *s=&g_self;
|
|
myth_def *myth=s->head;
|
|
myth_def *old;
|
|
while(myth){
|
|
pthread_join(myth->th,NULL);
|
|
old=myth;
|
|
myth=myth->next;
|
|
free(old);
|
|
}
|
|
s->head=NULL;
|
|
return 0;
|
|
}
|
|
|