//更新定时器 void update_timer(struct timer *t,uint64_t now) { struct timer_item *item; while(NULL != (item = (struct timer_item*)minheap_min(t->_minheap))){ if(NULL == item->_ud){ minheap_popmin(t->_minheap); free(item); }else if(now >= item->_timeout){ minheap_popmin(t->_minheap); if(item->_callback(t,item,item->_ud)) free(item); }else break; } }
void check_time_out(sche_t s,uint32_t now) { coro_t co; for( ; ;) { struct heapele *ele = minheap_min(s->_minheap); if(!ele) break; co = (coro_t)((int8_t*)ele - sizeof(co->next)); if(co->timeout < now) break; minheap_popmin(s->_minheap); if(co->status != CORO_ACTIVE) { co->status = CORO_ACTIVE; LINK_LIST_PUSH_BACK(s->active_list_1,co); }else if(co->status == CORO_DIE) { coro_destroy(&co); if(--s->coro_size == 0) s->stop = 1; //printf("a coro destroy\n"); } } s->next_check_timeout = now + 200;//check every 200 ms }
int find_path(AStar_t astar,int x,int y,int x1,int y1,kn_dlist *path){ AStarNode *from = get_node(astar,x,y); AStarNode *to = get_node(astar,x1,y1); if(!from || !to || from == to || to->block) return 0; minheap_insert(astar->open_list,&from->heap); AStarNode *current_node = NULL; while(1){ struct heapele *e = minheap_popmin(astar->open_list); if(!e){ reset(astar); return 0; } current_node = (AStarNode*)((int8_t*)e-sizeof(kn_dlist_node)); if(current_node == to){ while(current_node) { kn_dlist_remove((kn_dlist_node*)current_node);//可能在close_list中,需要删除 if(current_node != from)//当前点无需加入到路径点中 kn_dlist_push_front(path,(kn_dlist_node*)current_node); AStarNode *t = current_node; current_node = current_node->parent; t->parent = NULL; t->F = t->G = t->H = 0; t->heap.index = 0; } reset(astar); return 1; } //current插入到close表 kn_dlist_push(&astar->close_list,(kn_dlist_node*)current_node); //获取current的相邻节点 kn_dlist *neighbors = get_neighbors(astar,current_node); if(neighbors) { AStarNode *n; while((n = (AStarNode*)kn_dlist_pop(neighbors))){ if(n->heap.index)//在openlist中 { float new_G = current_node->G + cost_2_neighbor(current_node,n); if(new_G < n->G) { //经过当前neighbor路径更佳,更新路径 n->G = new_G; n->F = n->G + n->H; n->parent = current_node; minheap_change(astar->open_list,&n->heap); } continue; } n->parent = current_node; n->G = current_node->G + cost_2_neighbor(current_node,n); n->H = cost_2_goal(n,to); n->F = n->G + n->H; minheap_insert(astar->open_list,&n->heap); } neighbors = NULL; } } }