人到中年,是一个转折点,以后的路要思考清楚。见多了文人莫名奇怪的相轻。感谢某个业界大神鼓励说30岁是一个程序员的巅峰时刻。
本文实现的消息队列,是用list.h实现的。适用于以下场景:线程之间异步性比较明显(执行需要时间,需要事件回调)的情况,消息类型比较多的情况。
这篇文章的内容是博主为一个windows服务器写的消息队列,等下去书香门第上河坊散步,就之间粘贴了,不再修改为linu,如有需要可自行编辑一把(工作量倒不是很大,顺便加深理解,哈哈)。
说来也奇怪,在2年前博主还大骂windows烂,骂golang烂。真香定理表明,博主后面既写了windows server,也用golang写了server _
// 类的核心成员变量:
class SERVICE_CORE_CLASS CWHDataQueue
{
private:
struct list_head free_head_list = {}; // 链表头链表
std::map<WORD, struct list_head *> block_manager = {}; // 数据块资源池
// 基于linux kernel的环形队列
public:
//struct kfifo_ fifo; // linux kernel ring queue
// 基于linux kernel 的链表队列
public:
struct list_head queue_head; // linux kernel list queue
}
其中block_manager 是数据快的资源池,格式为:数据块size作为 key,数据块链表头指针作为value(链表每个节点上面都挂一个数据块对象),在程序跑起来后,把资源池撑大后,申请内存块就跟数组一样的效率,但是没有大数组带来的内存页频繁加载切换问题(索引数据块要比大数组快很多。)
free_head_list 是一个链表,链表节点取下后都可作为其它链表的链表头,众所周知Linux kernel list是有头链表(头不存数据,节点才存数据),block_manager的链表头指针(即key对应的value),都是从free_head_list申请的。
queue_head是消息队列实体,存放各种不同类型的消息。
linux kernel list:
// linux_list.h
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//内核链表的节点
struct list_head {
struct list_head *next, *prev;
};
//定义别名
// typedef unsigned long size_t;
typedef void(*op_t)(void *data);
//求结构体成员的偏移量
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
//根据结构体成员的地址计算出结构体的首地址
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
//头结点的初始化
#define LIST_HEAD_INIT(name) { &(name), &(name) }
//定义一个头结点 并且初始化
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
//struct list_head name = {&name, &name};
//使用函数的方法初始化
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/*
* Insert a node entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
//在知道节点的前驱和后继的情况下,在他们之间插入一个node节点
static inline void __list_add(struct list_head *node,
struct list_head *prev,
struct list_head *next)
{
next->prev = node;
node->next = next;
node->prev = prev;
prev->next = node;
}
/**
* list_add - add a node entry
* @node: node entry to be added
* @head: list head to add it after
*
* Insert a node entry after the specified head.
* This is good for implementing stacks.
*/
//内核链表的头插法
inline void list_add(struct list_head *node, struct list_head *head)
{
__list_add(node, head, head->next);
}
/**
* list_add_tail - add a node entry
* @node: node entry to be added
* @head: list head to add it before
*
* Insert a node entry before the specified head.
* This is useful for implementing queues.
*/
//内核链表的尾插法
static inline void list_add_tail(struct list_head *node, struct list_head *head)
{
__list_add(node, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
//在我们知道删除节点的前驱和后继的情况下, 删除中间的节点
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
//删除指定的结点
static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
// 删除指定的节点,并且将节点指向空
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
//#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
//#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
//#define LIST_POISON1 ((void *) 0x00100100)
//#define LIST_POISON2 ((void *) 0x00200200)
//entry->next = (struct list_head *)LIST_POISON1;
//entry->prev = (struct list_head *)LIST_POISON2;
}
/**
* list_replace - replace old entry by node one
* @old : the element to be replaced
* @node : the node element to insert
*
* If @old was empty, it will be overwritten.
*/
//使用node节点替换原来的old节点
static inline void list_replace(struct list_head *old,
struct list_head *node)
{
node->next = old->next;
node->next->prev = node;
node->prev = old->prev;
node->prev->next = node;
}
//使用node节点替换old节点 并且初始化old节点
static inline void list_replace_init(struct list_head *old,
struct list_head *node)
{
list_replace(old, node);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
//删除指定的节点,并且初始化节点
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
//将list从一个链表中删除,然后作为另外一个链表的第一个节点
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del_entry(list);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
//将list从一个链表中删除,然后通过尾插法插入到另外一个链表中
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del_entry(list);
list_add_tail(list, head);
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
//测试指定的节点是否为该链表的最后一个节点
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
//测试链表为空的情况
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
//同步问题
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
/**
* list_rotate_left - rotate the list to the left
* @head: the head of the list
*/
//将链表的第一个节点移动到链表的最后一个节点位置
static inline void list_rotate_left(struct list_head *head)
{
struct list_head *first;
if (!list_empty(head)) {
first = head->next;
list_move_tail(first, head);
}
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
//测试链表只有一个节点
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
//--------------------->
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *node_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = node_first;
node_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a node list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
if (list_empty(head))
return;
if (list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if (entry == head)
INIT_LIST_HEAD(list);
else
__list_cut_position(list, head, entry);
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the node list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the node list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the node list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the node list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
// ------------------------->
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
//找到结构体的首地址
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*
* Note, that list is expected to be not empty.
*/
//找到下一个节点的结构体的首地址
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
//遍历链表的循环
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* __list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* This variant doesn't differ from list_for_each() any more.
* We don't do prefetching in either case.
*/
//同上
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
//逆向遍历
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
//比较安全的方法遍历链表
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
//比较安全的方法遍历链表 方向是逆向的
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
//遍历结构体的
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
//逆向遍历
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_struct within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
//检测一个节点是否为空
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
//遍历
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
//反向遍历
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
//从指定的节点中遍历
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
//比较安全的方式遍历
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member), \
n = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.prev, typeof(*n), member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_struct within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_entry(pos->member.next, typeof(*pos), member)
#endif
// queue.h
#pragma once
#include "linux_list.h"
#include "kfifo.h"
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <stdbool.h>
#include <atomic>
#include "DataLocker.h"
//数据头信息
struct tagDataHead
{
WORD wDataSize; // 数据大小
WORD wIdentifier; // 类型标识,数据id
};
//负荷信息
struct tagBurthenInfo
{
DWORD dwDataSize; //缓冲区写入实际字节数
DWORD dwBufferSize; //缓冲区总长度
DWORD dwDataPacketCount; //数据包数
};
//数据信息
struct tagDataBuffer
{
WORD wDataSize; //数据大小
LPVOID pDataBuffer; //数据指针
};
struct block_node
{
struct list_head list;
tagDataHead header;
WORD size;
LPBYTE data;
};
class SERVICE_CORE_CLASS Queue
{
//数据变量
protected:
DWORD m_dwDataSize; // 队列缓冲区中实际写入的字节数
DWORD m_dwDataPacketCount; // 队列缓冲区中数据包个数
DWORD m_dwBufferSize; // 缓冲区总长度
private:
std::atomic<bool> m_AtomicVar = { false };
CCriticalSection m_CriticalSection = {}; //缓冲锁定
private:
struct list_head free_head_list = {}; // 链表头链表
std::map<WORD, struct list_head *> block_manager = {}; // 数据块资源池
// 基于linux kernel的环形队列
public:
//struct kfifo_ fifo; // linux kernel ring queue
// 基于linux kernel 的链表队列
public:
struct list_head queue_head; // linux kernel list queue
// 获取对应size的数据块,优先从数据快资源池获取,如果链表不存在或链表为空,则从堆空间申请新数据块
block_node * get_block(WORD size) {
struct block_node * block = NULL;
struct list_head * pos = NULL, * n = NULL, * head = NULL;
auto search = block_manager.find(size);
if (search != block_manager.end()) {
head = search->second;
if (list_empty(head)) {
//block_manager.erase(size);
//list_add(head, &free_head_list);
// 预申请10个数据快
for (auto i = 0; i < 10; i++) {
block = new block_node[1];
if (block == NULL) {
break;
}
INIT_LIST_HEAD(&block->list);
block->size = size;
block->data = new BYTE[size];
if (block->data == NULL) {
SafeDelete(block);
break;
}
ZeroMemory(block->data, block->size);
list_add(&block->list, head);
}
// 为本次申请1个数据块
block = new block_node[1];
if (block == NULL) {
return NULL;
}
INIT_LIST_HEAD(&block->list);
block->size = size;
block->data = new BYTE[size];
if (block->data == NULL) {
SafeDelete(block);
return NULL;
}
ZeroMemory(block->data, block->size);
return block;
}
else {
list_for_each_safe(pos, n, head) {
list_del(pos);
//block = (struct block_node *)((char *)pos - offsetof(struct block_node, list));
block = (struct block_node *)pos;
INIT_LIST_HEAD(&block->list);
ZeroMemory(block->data, block->size);
return block;
}
}
}
else {
block = new block_node[1];
if (block == NULL) {
return block;
}
INIT_LIST_HEAD(&block->list);
block->size = size;
block->data = new BYTE[size];
if (block->data == NULL) {
SafeDelete(block);
return NULL;
}
ZeroMemory(block->data, block->size);
return block;
}
}
// 释放数据块到对应size的链表
bool free_block(block_node * block) {
struct list_head * pos = NULL, * n = NULL, * head = NULL, * node = NULL;
ZeroMemory(block->data, block->size);
INIT_LIST_HEAD(&block->list);
// 释放到已存在链表尾
auto search = block_manager.find(block->size);
if (search != block_manager.end()) {
// 插入block_manager对应链表的尾
head = search->second;
if (!list_empty(head)) {
list_add(&block->list, head);
return true;
}
}
// 释放到新创建链表尾
if (list_empty(&free_head_list)) {
node = (struct list_head *)malloc(sizeof(struct list_head));
if (node == NULL) {
return false;
}
INIT_LIST_HEAD(node);
list_add(node, &free_head_list);
}
list_for_each_safe(pos, n, &free_head_list) {
list_del(pos);
INIT_LIST_HEAD(pos);
list_add(&block->list, pos);
block_manager[block->size] = pos;
return true;
}
}
private:
void queue_creat(struct list_head *list)
{
INIT_LIST_HEAD(list);
}
void in_queue(struct list_head *new_node, struct list_head *head)
{
list_add_tail(new_node, head);
}
struct list_head *out_queue(struct list_head *head)
{
if (list_empty(head)) {
return NULL;
}
struct list_head *list = head->next; /* 保存链表的最后节点 */
list_del(head->next);/* 头删法 */
INIT_LIST_HEAD(list); /* 重新初始化删除的最后节点,使其指向自身 */
return list;
}
int get_queue_size(struct list_head *head)
{
struct list_head *pos;
int size = 0;
if (head == NULL) {
return -1;
}
list_for_each(pos, head) {
size++;
}
return size;
}
bool is_empt_queue(struct list_head *head)
{
return list_empty(head);
}
public:
CWHDataQueue();
virtual ~CWHDataQueue();
//功能函数
public:
//负荷信息
VOID GetBurthenInfo(tagBurthenInfo & BurthenInfo);
//数据信息
DWORD GetDataPacketCount() { return m_dwDataPacketCount; }
public:
//插入数据:head + data
bool InsertData(WORD wIdentifier, VOID * pBuffer, WORD wDataSize);
//插入多条数据:head +data[N]
bool InsertData(WORD wIdentifier, tagDataBuffer DataBuffer[], WORD wDataCount);
public:
//删除数据
VOID RemoveData(bool bFreeMemroy);
//提取数据
bool DistillData(tagDataHead & DataHead, VOID * pBuffer, WORD wBufferSize);
private:
//调整存储
bool RectifyBuffer(DWORD dwNeedSize);
};
// queue.cpp
#include "StdAfx.h"
#include "Queue.h"
#include "DataLocker.h"
#include <iostream>
#define nop() __asm__ __volatile__ ("nop");
#define barrier() __asm__ __volatile__("": : :"memory")
#define mb() barrier()
#define rmb() mb()
#define wmb() mb()
#define read_barrier_depends() do { } while(0)
#define set_mb(var, value) do { var = value; mb(); } while (0)
#ifdef CONFIG_SMP
#define smp_mb() mb()
#define smp_rmb() rmb()
#define smp_wmb() wmb()
#define smp_read_barrier_depends() read_barrier_depends()
#else
#define smp_mb() barrier()
#define smp_rmb() barrier()
#define smp_wmb() barrier()
#define smp_read_barrier_depends() do { } while(0)
#endif
#ifdef CONFIG_SMP
#define smp_mb() mb()
#ifdef CONFIG_X86_PPRO_FENCE
#define smp_rmb() rmb()
#else /* CONFIG_X86_PPRO_FENCE */
#define smp_rmb() barrier()
#endif /* CONFIG_X86_PPRO_FENCE */
#ifdef CONFIG_X86_OOSTORE
#define smp_wmb() wmb()
#else /* CONFIG_X86_OOSTORE */
#define smp_wmb() barrier()
#endif /* CONFIG_X86_OOSTORE */
#define smp_read_barrier_depends() read_barrier_depends()
#define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
#else /* CONFIG_SMP */
#define smp_mb() barrier()
#define smp_rmb() barrier()
#define smp_wmb() barrier()
#define smp_read_barrier_depends() do { } while (0)
#define set_mb(var, value) do { var = value; barrier(); } while (0)
#endif /* CONFIG_SMP */
Queue::Queue()
{
//数据变量
m_dwDataSize = 0L;
m_dwDataPacketCount = 0L;
m_dwBufferSize = 0L;
block_manager.clear();
INIT_LIST_HEAD(&free_head_list);
queue_creat(&queue_head);
return;
}
Queue::~Queue()
{
RemoveData(true);
}
//负荷信息
VOID Queue::GetBurthenInfo(tagBurthenInfo & BurthenInfo)
{
//设置变量
BurthenInfo.dwDataSize = m_dwDataSize;
BurthenInfo.dwBufferSize = m_dwBufferSize;
BurthenInfo.dwDataPacketCount = m_dwDataPacketCount;
return;
}
// 插入数据
bool Queue::InsertData(WORD wIdentifier, VOID * pBuffer, WORD wDataSize)
{
//Spinlock DataSpinLocker(m_AtomicVar);
//aLocker ThreadLock(m_CriticalSection);
// 申请数据块
block_node * block = get_block(wDataSize);
if (block == NULL) {
return false;
}
//INIT_LIST_HEAD(&block->list);
block->header.wIdentifier = wIdentifier;
block->header.wDataSize = wDataSize;
block->size = wDataSize;
// 拷贝数据到数据快
CopyMemory(block->data, pBuffer, block->size);
// 数据块插入队列尾
in_queue(&block->list, &queue_head);
// 更新队列属性
m_dwDataPacketCount++;
m_dwDataSize += (wDataSize + sizeof(tagDataHead));
m_dwBufferSize += (wDataSize + sizeof(tagDataHead));
return true;
}
// 插入数据切片
bool Queue::InsertData(WORD wIdentifier, tagDataBuffer DataBuffer[], WORD wDataCount)
{
//Spinlock DataSpinLocker(m_AtomicVar);
//Locker ThreadLock(m_CriticalSection);
// 计算数据快大小
volatile WORD index = 0;
volatile WORD wDataSize = 0;
wDataSize = 0;
for (WORD i = 0; i < wDataCount; i++)
{
if (DataBuffer[i].wDataSize > 0)
{
wDataSize += DataBuffer[i].wDataSize;
}
}
// 申请数据块
block_node * block = get_block(wDataSize);
if (block == NULL) {
return false;
}
//INIT_LIST_HEAD(&block->list);
block->header.wIdentifier = wIdentifier;
block->header.wDataSize = wDataSize;
block->size = wDataSize;
// 拷贝数据到数据快
index = 0;
for (WORD i = 0; i < wDataCount; i++)
{
if (DataBuffer[i].wDataSize > 0)
{
CopyMemory((unsigned char *)block->data + index, DataBuffer[i].pDataBuffer, DataBuffer[i].wDataSize);
index += DataBuffer[i].wDataSize;
}
}
// 数据块插入队列尾
in_queue(&block->list, &queue_head);
// 更新队列属性
m_dwDataPacketCount++;
m_dwDataSize += (wDataSize + sizeof(tagDataHead));
m_dwBufferSize += (wDataSize + sizeof(tagDataHead));
return true;
}
// 提取数据
bool Queue::DistillData(tagDataHead & DataHead, VOID * pBuffer, WORD wBufferSize)
{
//Spinlock DataSpinLocker(m_AtomicVar);
//Locker ThreadLock(m_CriticalSection);
//缓冲区为0字节、或缓冲区包的个数为0则返回
if (m_dwDataSize == 0L) return false;
if (m_dwDataPacketCount == 0L) return false;
// 队列是否为null
if (is_empt_queue(&queue_head)) {
return false;
}
// 从队列头读取一个包
block_node * block = NULL;
struct list_head * node;
node = out_queue(&queue_head);
if (node == NULL) {
return false;
}
//block = (struct block_node *)((char *)node - offsetof(struct block_node, list));
block = (struct block_node *)node;
// 拷贝数据到参数指定缓存空间
DataHead = block->header;
CopyMemory(pBuffer, block->data, block->size);
// 更新队列属性
m_dwDataPacketCount--;
m_dwDataSize -= (block->size + sizeof(tagDataHead));
m_dwBufferSize -= (block->size + sizeof(tagDataHead));
// 查看队列
/*
std::cout << "m_dwDataPacketCount=" << m_dwDataPacketCount << std::endl;
std::cout << "消息头: block->header.wIdentifier = " << block->header.wIdentifier << ", block->header.wDataSize = " << block->header.wDataSize << std::endl;
int item_size = get_queue_size(&queue_head);
std::cout << "当前队列的数据个数为:" << item_size << std::endl;
*/
// 回收数据块
free_block(block);
return true;
}
//删除数据队列缓冲区
VOID Queue::RemoveData(bool bFreeMemroy)
{
//Spinlock DataSpinLocker(m_AtomicVar);
//Locker ThreadLock(m_CriticalSection);
m_dwDataSize = 0L;
m_dwDataPacketCount = 0L;
// 释放队列
struct block_node * block = NULL;
struct list_head * pos, *n;
list_for_each_safe(pos, n, &queue_head) {
list_del(pos);
INIT_LIST_HEAD(pos);
block = (struct block_node *)((char *)pos - offsetof(struct block_node, list));
if (block->data) {
SafeDeleteArray(block->data);
}
if (block) {
SafeDelete(block);
}
}
// 释放数据块资源池
std::map < WORD, struct list_head * >::iterator it;
for (it = block_manager.begin(); it != block_manager.end(); ) {
// 获取链表头
struct list_head * head = it->second;
// 释放链表节点(数据块)
list_for_each_safe(pos, n, head) {
list_del(pos);
INIT_LIST_HEAD(pos);
{
block = (struct block_node *)((char *)pos - offsetof(struct block_node, list));
if (block->data) {
SafeDeleteArray(block->data);
}
if (block) {
SafeDelete(block);
}
}
}
// 删除该size的资源
block_manager.erase(it++);
// 删除链表头
free(head);
}
// 删除链表头
list_for_each_safe(pos, n, &free_head_list) {
list_del(pos);
free(pos);
}
m_dwBufferSize = 0;
//queue_head
INIT_LIST_HEAD(&queue_head);
INIT_LIST_HEAD(&free_head_list);
block_manager.clear();
return;
}