philosopher/philo/utils_manage_threads.c

64 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_manage_threads.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */
/* Updated: 2023/04/08 16:10:05 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int init_mutex(pthread_mutex_t *mutex, int nbr)
{
int i;
i = -1;
while (++i < nbr)
if (pthread_mutex_init(&mutex[i], NULL) != 0)
return (1);
return (0);
}
void destroy_mutex(pthread_mutex_t *mutex, int nbr)
{
int i;
i = -1;
while (++i < nbr)
pthread_mutex_destroy(&mutex[i]);
}
static void free_philo(t_philo **philo)
{
int i;
i = -1;
while (philo[++i])
{
if (philo[i]->only)
free(philo[i]->only);
if (philo[i])
free(philo[i]);
}
free(philo);
}
int free_all(pthread_t *threads, t_same *same, t_philo **philo)
{
if (same && same->mutex)
destroy_mutex(same->mutex, same->config->nbr_philo + 3);
if (threads)
free(threads);
if (same && same->mutex)
free(same->mutex);
if (same)
free(same);
if (philo)
free_philo(philo);
return (1);
}