#include "shrink.h" /************************************************************************ 线程池(threadpool) *************************************************************************/ struct TP_node tpnodes[TH_NUM]; #ifdef _UCOS OS_STK TaskTHStk[TH_NUM][TaskStkLengh]; #endif /************************************************************************ ** Function name: TP_create ** Descriptions: 创建线程池 ** input parameters: minn:最小线程数,还没应用,保留 ** maxn:最大线程数,还没应用,保留 ** mini:最小空闲线程数,还没应用,保留 ** maxi:最大空闲线程数,还没应用,保留 ** output parameters: ptp:线程池指针 *************************************************************************/ void TP_create(struct TP_threadpool *ptp, int minn,int maxn,int mini,int maxi) { int i; ptp->minnum = minn; ptp->maxnum = maxn; ptp->minidle = mini; ptp->maxidle = maxi; ptp->sem = sem_create(1); TP_initlist(&ptp->busy); /* 初始化busy线程队列 */ TP_initlist(&ptp->idle); /* 初始化idle线程队列 */ for(i=0; iidle,&tpnodes[i]); } } /************************************************************************ ** Function name: TP_myworking ** Descriptions: 空闲线程运行用户函数 ** input parameters: fun:用户函数指针 ** args:函数参数 ** output parameters: ptp:线程池指针 *************************************************************************/ void TP_myworking(struct TP_threadpool *ptp, TP_Work_Fun fun, void * args) { struct TP_node *pnode; if (TP_getidlethread(ptp,&pnode)) /* 获得空闲线程 */ { pnode->fun = fun; /* 设置任务函数 */ pnode->args = args; /* 设置传入参数 */ sem_post(pnode->sem); /* 发送信号量,让工作线程工作 */ } else { #ifdef _DEBUG printf("nomore woking thread!\n"); #endif } } /************************************************************************ ** Function name: TP_getidlethread ** Descriptions: 获得空闲线程节点 ** output parameters: ptp:线程池指针 ** pnode:指向节点指针的指针 ** Returned value: 0 失败;1 成功 *************************************************************************/ int TP_getidlethread(struct TP_threadpool *ptp, struct TP_node **ppnode) { if (ptp->idle.num == 0) /* 没有空闲线程 */ { return 0; } else { *ppnode = ptp->idle.head; /* 获得空闲线程 */ TP_listdel(&ptp->idle,*ppnode); return 1; } } /************************************************************************ ** Function name: TP_initlist ** Descriptions: 初始化节点链表 ** output parameters: nodelist:节点链表指针 *************************************************************************/ void TP_initlist(struct TP_nodelist * nodelist) { nodelist->head = nodelist->tail = 0; nodelist->num = 0; } /************************************************************************ ** Function name: TP_initnode ** Descriptions: 初始化节点 ** output parameters: node:节点指针 ** ptp:线程池指针 *************************************************************************/ void TP_initnode(struct TP_node * node, struct TP_threadpool * ptp) { node->fun = 0; node->args = 0; node->prev = node->next = 0; node->pool = ptp; node->sem = sem_create(0); } /************************************************************************ ** Function name: TP_listadd ** Descriptions: 把节点加到节点链表里 ** output parameters: nodelist:节点链表指针 ** node:节点指针 *************************************************************************/ void TP_listadd(struct TP_nodelist * pnodelist, struct TP_node * pnode) { pnode->next = pnodelist->head; pnodelist->head = pnode; pnode->prev = 0; if (pnode->next) /* 判断是否是第一个加入的 */ { pnode->next->prev = pnode; } else { pnodelist->tail = pnode; } pnodelist->num ++; } /************************************************************************ ** Function name: TP_listdel ** Descriptions: 把节点从节点链表里删除 ** output parameters: nodelist:节点链表指针 ** node:节点指针 *************************************************************************/ void TP_listdel(struct TP_nodelist * pnodelist, struct TP_node * pnode) { if (pnode->next) { pnode->next->prev = pnode->prev; } else { pnodelist->tail = pnode->prev; } if (pnode->prev) { pnode->prev->next = pnode->next; } else { pnodelist->head = pnode->next; } pnodelist->num --; } /************************************************************************ ** Function name: TP_working ** Descriptions: 用户任务接口 ** output parameters: args:传入参数 *************************************************************************/ #ifdef _WIN32 DWORD WINAPI TP_working(void* args) { struct TP_node *pnode = (struct TP_node *)args; for (;;) { sem_wait(pnode->sem, INFINITE); pnode->fun(pnode->args); pnode->fun = 0; TP_listadd(&pnode->pool->idle,pnode); } } #elif _UCOS void TP_working(void* pdata) { struct TP_node *pnode = (struct TP_node *)pdata; uint8 err; for (;;) { sem_wait(pnode->sem,0,&err); pnode->fun(pnode->args); pnode->fun = 0; TP_listadd(&pnode->pool->idle,pnode); } } #endif