在项目之中遇见的,感觉挺实用的,记载下来。
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sched.h>
#include <termios.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>
#include <signal.h>
#define MAX_MSGS 16
#define M_MSG_KILL 0
typedef struct {
UINT32 cmd;
void *ptr;
UINT8 arg1, arg2, arg3, arg4;
} msg_t;
typedef struct {
msg_t msgs[MAX_MSGS];
int read_index;
int write_index;
int num_msgs;
int num_readers;
int num_writers;
pthread_mutex_t mutex;
pthread_cond_t cond_read;
pthread_cond_t cond_write;
sem_t echo_event;
} msg_queue_t;
msg_queue_t hcmgr_cmd_queue;
int msg_queue_init(msg_queue_t *q)
{
q->read_index = 0;
q->write_index = 0;
q->num_msgs = 0;
q->num_readers = 0;
q->num_writers = 0;
pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->cond_read, NULL);
pthread_cond_init(&q->cond_write, NULL);
sem_init(&q->echo_event, 0, 0);
return 0;
}
int msg_queue_deinit(msg_queue_t *q)
{
pthread_mutex_destroy(&q->mutex);
pthread_cond_destroy(&q->cond_read);
pthread_cond_destroy(&q->cond_write);
return 0;
}
void msg_queue_get(msg_queue_t *q, msg_t *msg)
{
pthread_mutex_lock(&q->mutex);
while (1) {
if (q->num_msgs > 0) {
*msg = q->msgs[q->read_index];
if (++q->read_index == MAX_MSGS)
q->read_index = 0;
q->num_msgs--;
if (q->num_writers > 0) {
q->num_writers--;
pthread_cond_signal(&q->cond_write);
}
pthread_mutex_unlock(&q->mutex);
return;
}
q->num_readers++;
pthread_cond_wait(&q->cond_read, &q->mutex);
}
}
int msg_queue_peek(msg_queue_t *q, msg_t *msg)
{
int ret = 0;
pthread_mutex_lock(&q->mutex);
if (q->num_msgs > 0) {
*msg = q->msgs[q->read_index];
if (++q->read_index == MAX_MSGS)
q->read_index = 0;
q->num_msgs--;
if (q->num_writers > 0) {
q->num_writers--;
//DRV_PRINT("num_writers:%d\n",q->num_writers);
pthread_cond_signal(&q->cond_write);
}
ret = 1;
}
pthread_mutex_unlock(&q->mutex);
return ret;
}
void msg_queue_put(msg_queue_t *q, msg_t *msg)
{
pthread_mutex_lock(&q->mutex);
while (1) {
if (q->num_msgs < MAX_MSGS) {
q->msgs[q->write_index] = *msg;
if (++q->write_index == MAX_MSGS)
q->write_index = 0;
q->num_msgs++;
if (q->num_readers > 0) {
q->num_readers--;
pthread_cond_signal(&q->cond_read);
}
pthread_mutex_unlock(&q->mutex);
return;
}else
DRV_PRINT("max msg num %d\n",q->num_msgs);
q->num_writers++;
pthread_cond_wait(&q->cond_write, &q->mutex);
}
}
void msg_queue_ack(msg_queue_t *q)
{
sem_post(&q->echo_event);
}
void msg_queue_wait(msg_queue_t *q)
{
sem_wait(&q->echo_event);
}
int rcvMsg(msg_t *msg, UINT32 waitOption)
{
int err = 0;
if(waitOption == TX_WAIT_FOREVER)
msg_queue_get(&hcmgr_cmd_queue, msg);
else
err = msg_queue_peek(&hcmgr_cmd_queue, msg);
return err;
}
int sendMsg(UINT32 msg, UINT32 param1, UINT32 param2)
{
msg_t message;
message.cmd = msg;
message.arg1 = param1;
message.arg2 = param2;
msg_queue_put(&hcmgr_cmd_queue, &message);
return 0;
}
int createMsg(void)
{
int err;
err =msg_queue_init(&hcmgr_cmd_queue);
return err;
}
void cleanMsgThread(void)
{
msg_t msg;
msg.cmd = M_MSG_KILL;
msg.ptr = NULL;
msg_queue_put(&hcmgr_cmd_queue, &msg);
}