1 Star 0 Fork 16

LCK/Ppool

forked from 炕头哥/Ppool 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ppool_queue.c 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
炕头哥 提交于 2015-03-26 08:44 . ppool_queue_get_task更新任务数
#include "ppool_queue.h"
int ppool_queue_get_insert_pos(pool_node *head,int priority);
//查询插入的位置
pool_w *ppool_queue_init(void)
{
pool_w *head;
head=malloc(sizeof(pool_w));
if(!head)
{
ppool_errno=PE_QUEUE_NO_MEM;
return NULL;
}
head->len=0;
head->head=NULL;
return head;
}
pool_node *ppool_queue_new(ppool_work task,void *arg,int priority)
{
pool_node *node;
if(priority < 0)
{
ppool_errno=PE_PRIORITY_ERROR;
return NULL;
}
node=malloc(sizeof(pool_node));
if(node == NULL)
{
ppool_errno=PE_QUEUE_NODE_NO_MEM;
return NULL;
}
node->task=task;
node->arg=arg;
node->priority=priority;
node->next=NULL;
return node;
}
void ppool_queue_add(pool_w *head,pool_node *node)
{
int pos;
int i;
pool_node *h=head->head;
pos=ppool_queue_get_insert_pos(h,node->priority);
if(pos == -1)
{
head->head=node;
++head->len;
return;
}
else if(pos == 0)
{
node->next=h;
head->head=node;
++head->len;
return;
}
for(i=0;i < pos-1;++i)
h=h->next;
node->next=h->next;
h->next=node;
++head->len;
}
pool_node *ppool_queue_get_task(pool_w *head)
{
pool_node *task;
if(head->head == NULL)
return NULL;
task=head->head;
head->head=head->head->next;
--head->len;
return task;
}
void ppool_queue_cleanup(pool_w *head)
{
pool_node *h=head->head;
pool_node *temp;
while(h)
{
temp=h;
h=h->next;
free(temp);
}
head->len=0;
head->head=NULL;
}
void ppool_queue_destroy(pool_w *head)
{
pool_node *h=head->head;
pool_node *temp;
while(h)
{
temp=h;
h=h->next;
free(temp);
}
free(head);
}
int ppool_queue_get_insert_pos(pool_node *head,int priority)
{
int pos;
if(head == NULL)
return -1;
for(pos=0;head && priority >= head->priority;++pos)
head=head->next;
return pos;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/PirLCK/Ppool.git
git@gitee.com:PirLCK/Ppool.git
PirLCK
Ppool
Ppool
master

搜索帮助