//#pragma once #ifndef __HIT_DATAQUEUE_H #define __HIT_DATAQUEUE_H #include #include #include #include #include #define MAX_BLOCK_BYTES (1024*1024*2) #define QUEUE_IS_FULL (-1) #define EMPTY_QUEUE_NO_DATA (0) class DataQueue { public: DataQueue(int64_t iSizeInKB = 0, int64_t iMaxBlockSize = MAX_BLOCK_BYTES, bool bUseMutex = true, int iReadThreadCnt=1); ~DataQueue(); //private: void* m_pDataBuf; //[DATA_BUF_LEN] int64_t m_iBufOutPos; int64_t m_iBufInPos; int64_t m_iFreeBytes; int64_t m_iQueueBufBytes; int64_t m_iRcvBlocks;//total count int64_t m_iRcvBytes;//total Bytes int64_t m_iProcBlocks; int64_t m_iPaddingBlocks;//padding block cnt in enqueue int64_t m_iProcPaddingBlocks;//processed padding block cnt in dequeue int64_t m_iMaxBlockByte; int64_t m_iReservedBufBytes; int64_t m_iReservedBlockNum; int64_t m_iEmptyTimes; int64_t m_iFullTimes; bool m_bUseMutex; pthread_mutex_t m_BufMutex;//pthread_mutex_init(&m_BufMutex, NULL); bool IsEmpty(); // bool IsFull(); inline bool CanRcv(int iNeedBytes); public: int64_t EnQueue(void* pdata, int64_t iDataBytes = 0, void* extradata = NULL, int64_t iExtraDataBytes = 0, void* extradata2 = NULL, int64_t iExtraDataBytes2 = 0); int64_t DeQueue(void** pdata);//return value: Bytes, >0 if sucess, =0 no data, < 0 error. No data copy, only return the pointer of data area uint64_t GetBlockCnt() { return m_iRcvBlocks - m_iProcBlocks; }; int64_t GetProcBlockCnt() { return m_iProcBlocks; }; int64_t GetTotalRcvBlockCnt() { return m_iRcvBlocks; }; int64_t GetFreeBytes(); int64_t GetBufBytes(); inline int64_t GetQueueFreeBytes(); int InitQueue(int64_t iTotalKB4QueueBuf, int iMaxBlockKB = MAX_BLOCK_BYTES, int iReadThreadCnt = 1);//KB bool CheckQueue(int iLineNo, int iNeedBytes); void OutPutStatus(char* strBuf, int iLen); void ResetDataQueue(); }; #define LOG_QUEUE_INFO 0 typedef void* voidPtr; extern "C" voidPtr __attribute__((visibility("default"))) OpenDataQueue(int64_t iTotalKB4QueueBuf, int iMaxBlockBytes, int iReadThreadCnt); extern "C" bool __attribute__((visibility("default"))) GetDataQueueInfo(void* pQueue, int64_t iSize[5]); extern "C" int64_t __attribute__((visibility("default"))) EnDataQueue(void* pQueue, void* e, int64_t iDataBytes, void* extradata1, int64_t iExtraDataBytes1, void* extradata2, int64_t iExtraDataBytes2); extern "C" int64_t __attribute__((visibility("default"))) DeDataQueue(void* pQueue, void** e); extern "C" bool __attribute__((visibility("default"))) DataQueueIsFull(void* pQueue); extern "C" void __attribute__((visibility("default"))) ResetDataQueue(void* pQueue); extern "C" void __attribute__((visibility("default"))) CloseDataQueue(void*& pQueue); #endif