61 lines
1.6 KiB
C
61 lines
1.6 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/06 13:57:40 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "philo.h"
|
|
|
|
void init_mutex(pthread_mutex_t *mutex, int nbr)
|
|
{
|
|
int i;
|
|
|
|
i = -1;
|
|
while (++i < nbr)
|
|
pthread_mutex_init(&mutex[i], NULL);
|
|
}
|
|
|
|
void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
|
{
|
|
int i;
|
|
|
|
i = -1;
|
|
while (++i < nbr)
|
|
pthread_mutex_destroy(&mutex[i]);
|
|
}
|
|
|
|
void free_all(pthread_t *threads, t_same *same, t_philo **philo)
|
|
{
|
|
int i;
|
|
|
|
destroy_mutex(same->mutex, same->config->nbr_philo + 2);
|
|
if (threads)
|
|
free(threads);
|
|
if (same && same->mutex)
|
|
free(same->mutex);
|
|
if (same && same->mutex)
|
|
free(same->death);
|
|
if (same && same->mutex)
|
|
free(same->all_eat);
|
|
if (same)
|
|
free(same);
|
|
if (philo)
|
|
{
|
|
i = -1;
|
|
while (philo[++i])
|
|
{
|
|
if (philo[i]->only)
|
|
free(philo[i]->only);
|
|
if (philo[i])
|
|
free(philo[i]);
|
|
}
|
|
free(philo);
|
|
}
|
|
}
|