thread_lib.h
#ifndef _THREAD_LIB_H_
#define _THREAD_LIB_H_
#include "public_def.h"
#include <pthread.h>
typedef struct _THREAD_INOF_
{
unsigned char thread_name[64];
int32 thread_pid;
pthread_t thread_t;
// THREAD_INFO *pnext_thread;
}THREAD_INFO;
typedef struct _MAG_Q_
{
char name[64];
pthread_mutex_t mutex_buff;
pthread_mutex_t mutex_cond;
pthread_cond_t cond;
int max_elements;
int element_length;
int cur_element_num;
void *date_buff;
}MAG_Q,*PMAG_Q;
#define THREAD_CREATE_SUCCSESS 0x1001
#define THREAD_CREATE_FAIL 0x1002
#define MQ_MAX_ERR 0x1003
#define MQ_LENGTH_ERR 0x1004
#define MAX_THREAD_NUM 20
#define MAX_QUEUE_NUM 100
MAG_Q *message_queue[MAX_QUEUE_NUM];
bool init_thread_lib();
int32 thread_create(char *nthread_name,void *thread_func);
bool init_thread_mutex();
void thread_lock();
void thread_unlock();
void thread_wait();
int32 message_queue_create( char *name, int maxnum, int length );
int msqueue_send ( /*int id*/char *nname, void *buff, int length );
int msqueue_receive ( /*int id*/char *nname, void *buff, int length );
#endif
thread_lib.c
#include "thread_lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
pthread_mutex_t g_thread_mutex;
THREAD_INFO g_thread_fifo[MAX_THREAD_NUM];
int g_thread_count=0;
bool init_thread_lib()
{
if(true!=init_thread_mutex())
return false;
return true;
}
int32 thread_create(char *nthread_name,void *thread_func)
{
// THREAD_INFO thread_info={0};
// THREAD_INFO p_thread;
int ret;
// p_thread=(THREAD_INFO *)malloc(sizeof(THREAD_INFO));
if(g_thread_count>=MAX_THREAD_NUM)
{
debug_print("MAX thread NUM/n/n");
return THREAD_CREATE_FAIL;
}
strcpy(g_thread_fifo[g_thread_count].thread_name,nthread_name);
ret=pthread_create(&(g_thread_fifo[g_thread_count].thread_t),NULL,thread_func,NULL);
if(ret==0){
debug_print("thread %s created.../n",g_thread_fifo[g_thread_count].thread_name);
g_thread_count++;
//pthread_detach(thread_info.thread_t);
return THREAD_CREATE_SUCCSESS;
}
else
{
return THREAD_CREATE_FAIL;
}
}
bool init_thread_mutex()
{
pthread_mutex_init(&g_thread_mutex,NULL);
return true;
}
void thread_lock()
{
pthread_mutex_lock(&g_thread_mutex);
}
void thread_unlock()
{
pthread_mutex_unlock(&g_thread_mutex);
}
void thread_wait()
{
int i;
for(i=0;i<g_thread_count;i++)
pthread_join(g_thread_fifo[i].thread_t,NULL);
}
int32 message_queue_create( char *name, int maxnum, int length )
{
int i;
for(i=0;i<MAX_QUEUE_NUM;i++)
{
if(i==MAX_QUEUE_NUM)
return MQ_MAX_ERR;
if(message_queue[i]==NULL)
break;
}
message_queue[i]=(MAG_Q *)malloc(sizeof(MAG_Q));
sprintf( message_queue[i]->name, "%s", name);
message_queue[i]->max_elements= maxnum;
message_queue[i]->element_length = length;
message_queue[i]->cur_element_num = 0;
message_queue[i]->date_buff=(void *)malloc(maxnum*length);
pthread_mutex_init(&(message_queue[i]->mutex_buff), NULL);
pthread_mutex_init(&(message_queue[i]->mutex_cond), NULL);
pthread_cond_init(&(message_queue[i]->cond), NULL);
return i;
}
int msqueue_send ( /*int id*/char *nname, void *buff, int length )
{
int pos;
int id;
for(id=0;id<MAX_QUEUE_NUM;id++)
{
if(strcmp(message_queue[id]->name,nname)==0)
break;
}
if ( id<0 || id >= MAX_QUEUE_NUM ) return MQ_MAX_ERR;
if ( length != message_queue[id]->element_length )
return MQ_LENGTH_ERR;
if ( message_queue[id]->cur_element_num >= message_queue[id]->max_elements )
return MQ_LENGTH_ERR;
pthread_mutex_lock ( (&message_queue[id]->mutex_buff) );
pos = message_queue[id]->cur_element_num * message_queue[id]->element_length;
bcopy (buff, (&message_queue[id]->date_buff[pos]), message_queue[id]->element_length );
message_queue[id]->cur_element_num++;
pthread_mutex_unlock ( (&message_queue[id]->mutex_buff ));
if ( message_queue[id]->cur_element_num == 1 ) {
pthread_mutex_lock ( (&message_queue[id]->mutex_cond) );
pthread_cond_broadcast ( (&message_queue[id]->cond) );
pthread_mutex_unlock ( (&message_queue[id]->mutex_cond ));
}
return length;
}
int msqueue_receive ( /*int id*/char *nname, void *buff, int length )
{
void *temp_buff;
int id;
int pos;
for(id=0;id<MAX_QUEUE_NUM;id++)
{
if(strcmp(message_queue[id]->name,nname)==0)
break;
}
if(id<0||id>=MAX_QUEUE_NUM)return MQ_MAX_ERR;
if(length != message_queue[id]->element_length)
return MQ_LENGTH_ERR;
if ( message_queue[id]->cur_element_num == 0){
pthread_mutex_lock ( (&message_queue[id]->mutex_cond) );
pthread_cond_wait ( (&message_queue[id]->cond), (&message_queue[id]->mutex_cond ));
pthread_mutex_unlock ( (&message_queue[id]->mutex_cond ));
}
pthread_mutex_lock ( (&message_queue[id]->mutex_buff ));
temp_buff =malloc((message_queue[id]->cur_element_num-1)*(message_queue[id]->element_length) );
bcopy ((&message_queue[id]->date_buff[0]), buff, message_queue[id]->element_length );
message_queue[id]->cur_element_num --;
bcopy((&message_queue[id]->date_buff[message_queue[id]->element_length]), temp_buff,((message_queue[id]->element_length)*(message_queue[id]->cur_element_num)));
bcopy(temp_buff, (&message_queue[id]->date_buff[0]),message_queue[id]->element_length*message_queue[id]->cur_element_num);
free(temp_buff);
pthread_mutex_unlock ( (&message_queue[id]->mutex_buff ));
return length;
}
//////////////////////////////////e.g//////////////////////////////////////////////////
thread_create("thread 1", thread_fun1);
message_queue_create("test_queue", 50, sizeof(Test_Thread_S));
msqueue_send("test_queue", (void * )&Test_info,sizeof(Test_Thread_S));
msqueue_receive("test_queue", (void *)&Test_buf, sizeof(Test_Thread_S));