int32_t kn_epoll_loop(kn_proactor_t p,int32_t ms) { uint64_t sleep_ms; uint64_t timeout = kn_systemms64() + (uint64_t)ms; uint64_t current_tick; int32_t nfds; int32_t i; uint64_t l_now; kn_dlist* actived; kn_fd_t s; kn_epoll* ep = (kn_epoll*)p; if(ms < 0) ms = 0; do{ if(!kn_dlist_empty(&p->connecting)){ l_now = kn_systemms64(); kn_dlist_check_remove(&p->connecting,check_connect_timeout,(void*)&l_now); } actived = kn_proactor_activelist(p); if(!kn_dlist_empty(actived)){ p->actived_index = (p->actived_index+1)%2; while((s = (kn_fd_t)kn_dlist_pop(actived)) != NULL){ if(s->process(s)) kn_procator_putin_active(p,s); } } current_tick = kn_systemms64(); actived = kn_proactor_activelist(p); if(kn_dlist_empty(actived)) sleep_ms = timeout > current_tick ? timeout - current_tick:0; else sleep_ms = 0; nfds = _epoll_wait(ep->epfd,ep->events,ep->eventsize,(uint32_t)sleep_ms); if(nfds < 0) return -1; for(i=0; i < nfds ; ++i) { s = (kn_fd_t)ep->events[i].data.ptr; s->on_active(s,ep->events[i].events); } current_tick = kn_systemms64(); }while(timeout > current_tick); return 0; }
static inline kn_dlist* get_neighbors(AStar_t astar,AStarNode *node) { clear_neighbors(astar); int32_t i = 0; for( ; i < 8; ++i) { int x = node->x + direction[i][0]; int y = node->y + direction[i][1]; AStarNode *tmp = get_node(astar,x,y); if(tmp){ if(tmp->list_node.pre || tmp->list_node.next) continue;//在close表中,不处理 if(!tmp->block) kn_dlist_push(&astar->neighbors,(kn_dlist_node*)tmp); } } if(kn_dlist_empty(&astar->neighbors)) return NULL; else return &astar->neighbors; }
static inline void clear_neighbors(AStar_t astar){ while(!kn_dlist_empty(&astar->neighbors)) kn_dlist_pop(&astar->neighbors); }