linux c多线程简单队列实现-3. 头文件JList.h

时间:2024-04-12 09:01:21
#ifndef __J_LIST__
#define __J_LIST__

#ifdef __cplusplus
extern "C" {
#endif

typedef enum _JLIST_ERR{
	JLIST_NO_DATA = -4,				// 无数据供读取
	JLIST_READ_LENGTH_ERR = -3,		// 读取缓冲区长度不够
	JLIST_WRITE_LENGTH_ERR = -2,	// 写入长度超出
	JLIST_INIT_FAIL = -1,			// 初始化异常
	JLIST_OK = 0,
	JLIST_WRITE_OVERWRITE = 1,		// 写成功,但是覆盖
}JLIST_ERR;


class JList {
public:
    JList(unsigned int size, unsigned int blockNum = 0);
    ~JList();

    int Write(unsigned char *buf, unsigned int len);
    int Read(unsigned char *buf, unsigned int len);

private:
	static unsigned int IsPower2(unsigned int size);	
	static unsigned int To2N(unsigned int size);
private:
	unsigned int m_size;		// 内存总大小
	unsigned int m_blockSize;	// 块大小
	unsigned int m_blockNum;	// 块数量
	unsigned char *m_buffer; 	// 内存地址
	unsigned int m_offset;		// 数据偏移量
	
	pthread_mutex_t m_mutex;

};

#ifdef __cplusplus
}
#endif
#endif // __J_LIST__