msgque.h

来自「这是实际项目中的一个串口通信程序. Makefile通过开关, 可使此程序适用于」· C头文件 代码 · 共 76 行

H
76
字号
#ifdef __cplusplus
extern {
#endif


// MsgQue.h: interface for the MsgQue class.

//////////////////////////////////////////////////////////////////////
//File Name: MsgQue.h: implementation of the MsgQue and SafeMsgQue structure.
//References: man in linux
//Purpose: To implement a Safe Message Queue and some functions about it.

//History
//*Data          *Name      *comments.
//2008-Feb-23    Rock Li    Initialization

#ifndef  _MSGQUE_H
#define  _MSGQUE_H

#include <semaphore.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#define DEBUG  1

#pragma pack(1)
// message queue ;pragma pack(1) means align is 1 byte;
struct tag_MsgQue
{
	unsigned char Buf[256];
	struct tag_MsgQue* next;
} ;
#pragma pack()

typedef struct tag_MsgQue* pMsgQue;
typedef struct tag_MsgQue  MsgQue;
typedef struct tag_MsgQue  MsgNode;

struct tag_SafeMsgQue
{
//	pMsgQue pMsg;         //Message queue
	pMsgQue pHead;        //the head of the Message queue
	pMsgQue pTail;		  //the tail of the Message queue	
	
//	HANDLE hProExitEvn;   // program exit event; it should be passed by the caller.
//	HANDLE hOpSem;        // Operation semaphore; 0/1 semaphore
//	HANDLE hMsgSem;       // Message(s) exits. if there is one or more messages in the queue, the falg of hMsgSem is ture.
    pthread_mutex_t hOpLock;  //for operation
    sem_t hMsgSem;             //means there are some message(s).  
};

typedef struct tag_SafeMsgQue* pSafeMsgQue;
typedef struct tag_SafeMsgQue  SafeMsgQue;

int SafeMsgQueInit(pSafeMsgQue pSafeQue);

//int ReadOneMsg(pMsgQue* pMsg, pSafeMsgQue pSafeQue);
int ReadOneMsg(pMsgQue* pMsg, pSafeMsgQue pSafeQue, unsigned int nTimeout);

int InsertOneMsg(pMsgQue pMsg, pSafeMsgQue pSafeQue);

int LockQue(pSafeMsgQue pSafeQue);
int UnlockQue(pSafeMsgQue pSafeQue);

#endif 

#ifdef __cplusplus
}
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?