예제 #1
0
파일: alarm.c 프로젝트: Jack19881218/cavan
static int cavan_alarm_thread_handler(struct cavan_thread *thread, void *data)
{
	struct cavan_alarm_thread *alarm_thread = data;
	struct double_link_node *node;

	pthread_mutex_lock(&alarm_thread->lock);

	node = double_link_get_first_node(&alarm_thread->link);
	if (node == NULL)
	{
		alarm(0);
		cavan_thread_suspend(thread);
	}
	else
	{
		struct cavan_alarm_node *alarm_node = double_link_get_container(&alarm_thread->link, node);
		time_t curr_time;

		curr_time = time(NULL);
		if (curr_time < 0)
		{
			pr_error_info("get current time");
			pthread_mutex_unlock(&thread->lock);
			return curr_time;
		}

		cavan_show_date2(curr_time, "curr_time = ");
		cavan_show_date2(alarm_node->time, "alarm_time = ");

		if (curr_time < alarm_node->time)
		{
			alarm(alarm_node->time - curr_time);
			cavan_thread_suspend(thread);
		}
		else
		{
			double_link_remove(&alarm_thread->link, node);
			alarm_node->handler(alarm_node, alarm_thread, alarm_node->private_data);

			if (alarm_node->repeat)
			{
				alarm_node->time += alarm_node->repeat;
				cavan_alarm_insert_node_base(alarm_thread, alarm_node);
			}
			else if (alarm_node->destroy)
			{
				alarm_node->destroy(alarm_node, alarm_node->private_data);
			}
		}
	}

	pthread_mutex_unlock(&alarm_thread->lock);

	return 0;
}
예제 #2
0
파일: pool.c 프로젝트: FuangCao/cavan
void *cavan_data_pool_alloc(struct cavan_data_pool *pool)
{
	void *data;
	struct double_link_node *node;
	struct cavan_data_pool_node *pool_node;

	node = double_link_pop(&pool->link);
	if (node) {
		return double_link_get_container(&pool->link, node);
	}

	data = malloc(pool->node_size);
	if (data == NULL) {
		pr_error_info("malloc");
		return NULL;
	}

	pool_node = cavan_data_pool_to_node(pool, data);
	pool_node->destroy = cavan_data_pool_node_destroy2;
	double_link_node_init(&pool_node->node);

	return data;
}
예제 #3
0
파일: alarm.c 프로젝트: Jack19881218/cavan
static bool cavan_alarm_insert_matcher(struct double_link *link, struct double_link_node *node, void *data)
{
	struct cavan_alarm_node *alarm_node = double_link_get_container(link, node);

	return alarm_node->time > *(time_t *) data;
}