#ifndef _QUEUE_H_ #include "queue.h" #endif #ifndef _TYPEDEF_H_ #include "typedef.h" #endif #ifndef _LOG_H_ #include "log.h" #endif #define QUEUE_HANDLE_FAIL 0 #define QUEUE_HANDLE_OK 1 queue_t* queue_init(int capacity) { queue_t* queue = (queue_t*)malloc(sizeof(queue_t)); if (queue == NULL) { LOG("malloc queue err ret=%08x", SYD_MEMORY_ERR); return NULL; } memset(queue, 0, sizeof(queue_t)); queue->task = (task_t*)malloc(sizeof(task_t) * capacity); if (queue->task == NULL) { LOG("malloc task err ret=%08x", SYD_MEMORY_ERR); return NULL; } memset(queue->task, 0, sizeof(task_t) * capacity); queue->head = 0; queue->tail = 0; queue->length = 0; queue->capacity = capacity; queue->status = QUEUE_HANDLE_OK; return queue; } void queue_destory(queue_t* queue) { if (queue == NULL) { return; } if (queue->task != NULL) { free(queue->task); queue->task = NULL; } queue->status = QUEUE_HANDLE_FAIL; free(queue); queue = NULL; } void queue_clear(queue_t* queue) { queue->head = 0; queue->tail = 0; queue->length = 0; } int queue_getlength(queue_t* queue) { if (queue == NULL) { LOG("queue == NULL ret=%04x", SYD_PARAM_ERR); return SYD_PARAM_ERR; } if (queue->status != QUEUE_HANDLE_OK) { LOG("queue->status != QUEUE_HANDLE_OK ret=%08x", SYD_QUEUE_HANDLE_ERR); return SYD_QUEUE_HANDLE_ERR; } return AO_GET(&queue->length); } int queue_enqueue(queue_t* queue, int* (*function)(void* arg, void *device), void *arg, void *device) { if (queue == NULL) { LOG("queue == NULL ret=%04x", SYD_PARAM_ERR); return SYD_PARAM_ERR; } if (queue->status != QUEUE_HANDLE_OK) { LOG("queue->status != QUEUE_HANDLE_OK ret=%08x", SYD_QUEUE_HANDLE_ERR); return SYD_QUEUE_HANDLE_ERR; } /*队列是否已满*/ if (AO_GET(&queue->length) == queue->capacity) { //LOG("queue is full ret=%04x", SYD_QUEUE_ISFULL); return SYD_QUEUE_ISFULL; } queue->task[queue->tail].function = function; queue->task[queue->tail].arg = arg; queue->task[queue->tail].device = device; queue->tail = (queue->tail + 1) % queue->capacity; AO_F_ADD(&queue->length, 1); return SYD_OK; } int queue_dequeue(queue_t* queue, int* (*function)(void* arg, void *device), void *arg, void *device) { if (queue == NULL) { LOG("queue == NULL ret=%04x", SYD_PARAM_ERR); return SYD_PARAM_ERR; } if (queue->status != QUEUE_HANDLE_OK) { LOG("queue->status != QUEUE_HANDLE_OK ret=%08x", SYD_QUEUE_HANDLE_ERR); return SYD_QUEUE_HANDLE_ERR; } /*队列是否为空*/ if (AO_GET(&queue->length) == 0) { //LOG("queue is empty ret=%04x", SYD_QUEUE_ISEMPTY); return SYD_QUEUE_ISEMPTY; } function = queue->task[queue->head].function; arg = queue->task[queue->head].arg; device = queue->task[queue->head].device; queue->head = (queue->head + 1) % queue->capacity; AO_F_SUB(&queue->length, 1); return SYD_OK; }