示例#1
0
static inline int _wait(kn_msgque_reader_t reader,int ms){
	kn_msgque*  msgque = reader->msgque;	
	if(ms > 0){
		uint32_t timeout = kn_systemms() + (uint32_t)ms;
		kn_dlist_push(&msgque->waits,(kn_dlist_node*)reader);
		while(kn_list_size(&msgque->shareque) == 0 && !msgque->closing){
			uint32_t now = kn_systemms();
			uint32_t sleepms = timeout > now ? timeout - now : 0;
			if(!sleepms) 
				break;
			if(0 != kn_condition_timedwait(reader->cond,msgque->mtx,(int32_t)sleepms)){
				break;
			}
		}
		kn_dlist_remove((kn_dlist_node*)reader);
	}else if(ms < 0){
		//无限等待
		kn_dlist_push(&msgque->waits,(kn_dlist_node*)reader);
		while(kn_list_size(&msgque->shareque) == 0 && !msgque->closing){
			if(0 != kn_condition_wait(reader->cond,msgque->mtx))
				break;
		}
		kn_dlist_remove((kn_dlist_node*)reader);
		kn_list_swap(&reader->readbuff,&msgque->shareque);
	}else{
		//不等待
		kn_list_swap(&reader->readbuff,&msgque->shareque);
	}
	return kn_list_size(&reader->readbuff);	
}
示例#2
0
void kn_curl_easy_cleanup(kn_CURL_t curl){
	//printf("kn_curl_easy_cleanup\n");
	kn_dlist_remove((kn_dlist_node*)curl);
    	curl_multi_remove_handle(kn_CURLM_get(curl->c_handle), curl->curl);
    	curl_easy_cleanup(curl->curl);	
	free(curl);
}
示例#3
0
文件: astar.c 项目: DiaosiDev/Survive
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;
		}	
	}	
}
示例#4
0
int32_t  kn_epoll_unregister(kn_proactor_t p ,kn_fd_t s){
	kn_epoll *ep = (kn_epoll*)p;
	struct epoll_event ev = {0};
	int32_t ret;
	if(!s->proactor) return -1;	
	TEMP_FAILURE_RETRY(ret = epoll_ctl(ep->epfd,EPOLL_CTL_DEL,s->fd,&ev));
	//((kn_datasocket*)s)->flag = 0;
    kn_dlist_remove((kn_dlist_node*)s);
	s->proactor = NULL;
	if(0 == ret) --ep->eventsize;
	return ret;
}
示例#5
0
CURLMcode kn_CURLM_add(kn_CURLM_t cm,kn_CURL_t curl,void (*cb)(kn_CURL_t,CURLMsg *message,void*),void*ud){
	if(0 != kn_dlist_push(&cm->curls,(kn_dlist_node*)curl))
		return CURLM_ADDED_ALREADY;
	curl->cb = cb;
	curl->ud = ud;
	curl->c_handle = cm;
	CURLMcode code = curl_multi_add_handle(cm->c_handle,curl->curl);
	if(code != CURLM_OK) kn_dlist_remove((kn_dlist_node*)curl);
	else{
		curl_easy_setopt(curl->curl,CURLOPT_PRIVATE,curl);
		if(!cm->timer) cm->timer = kn_reg_timer(cm->e,1,timer_callback,cm);
	}
	return code;
}