#include "philo.h" // TODO : compare time with time before dying void go_sleep(t_philo *philo, int time) { (void)philo; usleep(time * 1000); } int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret) { if (mutex_1) pthread_mutex_unlock(mutex_1); if (mutex_2) pthread_mutex_unlock(mutex_2); return (ret); } int take_forks(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); if (philo->global->dead != 0) return (ret_unlock(&(philo->m_fork), NULL, 1)); print_message(philo, "has taken a fork"); pthread_mutex_lock(&(philo->next->m_fork)); if (philo->global->dead != 0) return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1)); print_message(philo, "has taken a fork"); update_time(philo); print_message(philo, "is eating"); go_sleep(philo, philo->params->t_eat); pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->m_fork)); return (0); } void *philo_exec(void *arg) { t_philo *philo; philo = (t_philo*)arg; init_time(philo); if (philo->p_nbr % 2 == 0) usleep(10 * 1000); while (1) { if (take_forks(philo) != 0) break ; print_message(philo, "is sleeping"); go_sleep(philo, philo->params->t_slp); print_message(philo, "is thinking"); } return (NULL); }