/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* utils_manage_threads.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */ /* Updated: 2023/04/10 09:53:55 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" int *init_forks(t_config *config) { int *forks; int i; forks = ft_calloc(sizeof(int), config->nbr_philo); if (!forks) return (NULL); i = 0; while (i < config->nbr_philo) forks[i++] = 1; return (forks); } 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 && same->forks) free(same->forks); if (same) free(same); if (philo) free_philo(philo); return (1); }