队列定义
struct event_base 结构里面有个事件队列,类型如下
struct event_list eventqueue;
struct event_list的定义通过如下面的宏来定义:
TAILQ_HEAD (event_list, event);
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
}
所以struct event_list实际的定义如下:
struct event_list
{
struct event tqh_first; / first element */
struct event *tqh_last; / addr of last next element */
}
队列节点
队列节点定义如下:
#define TAILQ_ENTRY(type)
struct {
struct type tqe_next; / next element */
struct type *tqe_prev; / address of previous next element */
}
即:
struct {
struct event tqe_next; / next element */
struct event *tqe_prev; / address of previous next element */
}
队列初始化
#define TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first }
或:
#define TAILQ_INIT(head) do { \
(head)->tqh_first = NULL; \
(head)->tqh_last = &(head)->tqh_first; \
} while (0)
使用方法:
TAILQ_INIT(&base->eventqueue);
插入、删除队列节点
TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
(elm)->field.tqe_next = NULL; \
(elm)->field.tqe_prev = (head)->tqh_last; \
*(head)->tqh_last = (elm); \
(head)->tqh_last = &(elm)->field.tqe_next; \
} while (0)
#define TAILQ_REMOVE(head, elm, field) do { \
if (((elm)->field.tqe_next) != NULL) \
(elm)->field.tqe_next->field.tqe_prev = \
(elm)->field.tqe_prev; \
else \
(head)->tqh_last = (elm)->field.tqe_prev; \
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
} while (0)