Esempio n. 1
0
static void	*f_threadpool_routine(void *arg)
{
	t_threadpool_data	*data;
	t_threadpool		*v_this;
	t_threadpool_task	*task;
	bool				run;

	run = true;
	v_this = (t_threadpool *)arg;
	while (run == true)
	{
		if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true)
		{
			run = data->v_run;
			task = D_QUEUE(pop)(&data->v_tasks);
			D_LOCK(release)(&v_this->v_data, (void **)&data);
			if (run == true && task != NULL)
			{
				task->f_funct(task->v_data);
				uf_free_s((void **)&task);
			}
		}
		usleep(5000);
	}
	return (NULL);
}
Esempio n. 2
0
void		f_queue_destroy(t_queue *v_this)
{
	D_QUEUE(clear)(v_this);
	v_this->v_size = 0;
	v_this->v_head = NULL;
	v_this->v_tail = NULL;
	v_this->f_destroy = NULL;
}
Esempio n. 3
0
bool		f_threadpool_init(t_threadpool *v_this, size_t nb_thread)
{
	t_threadpool_data	*data;

	v_this->pv_data.v_run = false;
	if ((v_this->v_id = uf_malloc_s(nb_thread, sizeof(*v_this->v_id))) == NULL)
		return (M_ERROR(false, "Bad alloc"));
	D_QUEUE(init)(&v_this->pv_data.v_tasks, free);
	if (D_LOCK(init)(&v_this->v_data, &v_this->pv_data,
					e_lock_default) == false)
	{
		uf_free_s((void **)&v_this->v_id);
		return (M_ERROR(false, "Couldn't initialize lock"));
	}
	if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true)
		return (D_THREADPOOL(create)(v_this, data, nb_thread));
	D_LOCK(destroy)(&v_this->v_data);
	uf_free_s((void **)&v_this->v_id);
	return (M_ERROR(false, "An error has occured"));
}
Esempio n. 4
0
void		f_threadpool_destroy(t_threadpool *v_this)
{
	size_t				i;
	t_threadpool_data	*data;

	i = 0;
	if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true)
	{
		data->v_run = false;
		D_LOCK(release)(&v_this->v_data, (void **)&data);
	}
	while (i < v_this->v_nb_thread)
	{
		if (pthread_join(v_this->v_id[i], NULL) != 0)
			M_INFOS("pthread_join error");
		i = i + 1;
	}
	D_LOCK(destroy)(&v_this->v_data);
	D_QUEUE(destroy)(&v_this->pv_data.v_tasks);
	uf_free_s((void **)&v_this->v_id);
}
Esempio n. 5
0
bool		f_threadpool_add_task(t_threadpool *v_this,
									t_threadpool_task *task)
{
	t_threadpool_task	*add;
	t_threadpool_data	*data;

	if ((add = uf_malloc_s(1, sizeof(*add))) == NULL)
		return (M_ERROR(false, "Bad alloc"));
	uf_memcpy(add, task, sizeof(*add));
	if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true)
	{
		if (D_QUEUE(push)(&data->v_tasks, add) == false)
		{
			uf_free_s((void **)&add);
			D_LOCK(release)(&v_this->v_data, (void **)&data);
			return (M_ERROR(false, "Couldn't add tasks"));
		}
		D_LOCK(release)(&v_this->v_data, (void **)&data);
		return (true);
	}
	uf_free_s((void **)&add);
	return (false);
}
Esempio n. 6
0
void	f_queue_destroy(t_queue *v_this)
{
    D_QUEUE(clear)(v_this);
    v_this->f_destroy = NULL;
}