Example #1
0
int swTimer_add(swTimer *timer, int ms)
{
	swTimer_node *node = sw_malloc(sizeof(swTimer_node));
	if (node == NULL)
	{
		swWarn("malloc failed.");
		return SW_ERR;
	}

	bzero(node, sizeof(swTimer_node));
	node->lasttime = swTimer_get_ms();
	node->interval = ms;

	if (ms < timer->interval)
	{
		int new_interval = swoole_common_divisor(ms, timer->interval);
		timer->interval = new_interval;
		if (SwooleG.use_timerfd)
		{
			swTimer_timerfd_set(timer, new_interval);
		}
		else
		{
			swTimer_signal_set(timer, new_interval);
		}
	}
	swHashMap_add_int(timer->list, ms, node);
	timer->num++;
	return SW_OK;
}
Example #2
0
int swTimer_select(swTimer *timer, swServer *serv)
{
	void *tmp = NULL;
	uint64_t key;
	swTimer_node *timer_node;

	time_t now_ms = swTimer_get_ms();
	if (now_ms < 0)
	{
		return SW_ERR;
	}

	while (1)
	{
		tmp = swHashMap_foreach_int(&timer->list, &key, (void **)&timer_node, tmp);
		//值为空
		if (timer_node == NULL)
		{
			break;
		}
		//swWarn("Timer=%ld|lasttime=%ld|now=%ld", key, timer_node->lasttime, now_ms);
		if (timer_node->lasttime < now_ms - timer_node->interval)
		{
			serv->onTimer(serv, timer_node->interval);
			timer_node->lasttime += timer_node->interval;
		}
		//遍历结束
		if (tmp == NULL)
		{
			break;
		}
	}
	return SW_OK;
}
Example #3
0
int swTimer_add(swTimer *timer, int ms)
{
	swTimer_node *node = sw_malloc(sizeof(swTimer_node));
	if (node == NULL)
	{
		swWarn("swTimer_add malloc fail");
		return SW_ERR;
	}
	bzero(node, sizeof(swTimer_node));
	node->lasttime = swTimer_get_ms();
	node->interval = ms;

	if(ms < timer->interval)
	{
		int new_interval = swoole_common_divisor(ms, timer->interval);
		timer->interval = new_interval;
#if defined(HAVE_TIMERFD) && SW_WORKER_IPC_MODE == 1
		swTimer_timerfd_set(timer, new_interval);
#else
		swTimer_signal_set(timer, new_interval);
#endif
	}
	swHashMap_add_int(&timer->list, ms, node);
	timer->num++;
	return SW_OK;
}
Example #4
0
int swTimer_add(swTimer *timer, int ms)
{
	swTimer_node *node = sw_malloc(sizeof(swTimer_node));
	if (node == NULL)
	{
		swWarn("swTimer_add malloc fail");
		return SW_ERR;
	}
	bzero(node, sizeof(swTimer_node));
	node->lasttime = swTimer_get_ms();
	node->interval = ms;
	swHashMap_add_int(&timer->list, ms, node);
	timer->num++;
	return SW_OK;
}
Example #5
0
int swTimer_select(swTimer *timer)
{
	uint64_t key;
	swTimer_node *timer_node;

	int64_t now_ms = swTimer_get_ms();
	if (now_ms < 0)
	{
		return SW_ERR;
	}

	if (timer->onTimer == NULL)
	{
		swWarn("timer->onTimer is NULL");
		return SW_ERR;
	}

	do
	{
	    //swWarn("timer foreach start\n----------------------------------------------");
	    timer_node = swHashMap_each_int(timer->list, &key);

	    //hashmap empty
		if (timer_node == NULL)
		{
			break;
		}
		//swWarn("Timer=%ld|lasttime=%ld|now=%ld", key, timer_node->lasttime, now_ms);
		if (timer_node->lasttime < now_ms - timer_node->interval)
		{
			timer->onTimer(timer, timer_node->interval);
			timer_node->lasttime += timer_node->interval;
		}
	} while(timer_node);
	return SW_OK;
}