消息队列

在项目之中遇见的,感觉挺实用的,记载下来。

#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);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值