/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* routine_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lfirmin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/22 14:47:26 by lfirmin #+# #+# */ /* Updated: 2025/03/17 03:59:51 by lfirmin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/philo.h" void unlock_forks(t_philo *philo) { pthread_mutex_unlock(&philo->data->forks[philo->r_fork]); pthread_mutex_unlock(&philo->data->forks[philo->l_fork]); } int take_fork_and_eat(t_philo *philo) { long long current_time; int sleep_result; if (check_if_dead(philo)) return (1); if (take_fork(philo)) return (1); if (print_message(philo, "has taken a fork")) { unlock_forks(philo); return (1); } current_time = get_time(); pthread_mutex_lock(&philo->meal_lock); philo->last_meal = current_time; pthread_mutex_unlock(&philo->meal_lock); if (print_message(philo, "is eating")) return (unlock_forks(philo), 1); pthread_mutex_lock(&philo->meal_lock); philo->m_eaten++; pthread_mutex_unlock(&philo->meal_lock); sleep_result = safe_sleep(philo, philo->data->t_eat * 1000); return (unlock_forks(philo), sleep_result); } int ft_should_continue(t_philo *philo) { if (philo->data->dead_flag) return (0); else if (philo->data->must_eat != -1) { if (philo->m_eaten >= philo->data->must_eat) return (0); else return (1); } else return (1); }