From 36bda7d5c035e9bb9eaed5deec22a763f13df35d Mon Sep 17 00:00:00 2001 From: Adam Joly Date: Mon, 12 Aug 2024 21:06:38 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?= =?UTF-8?q?=20error=20in=20helgrind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/eat.c | 10 ++++++---- philo/monitor.c | 6 +++++- philo/philo_routine.c | 2 +- philo/usleep.c | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/philo/eat.c b/philo/eat.c index 8d31c31..1392c9b 100644 --- a/philo/eat.c +++ b/philo/eat.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 17:29:51 by adjoly #+# #+# */ -/* Updated: 2024/08/12 20:50:55 by adjoly ### ########.fr */ +/* Updated: 2024/08/12 21:04:57 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,19 +23,21 @@ void take_fork(t_fork *fork, int id) bool philo_eat(t_philo *philo) { philo->state = FORK_TAKEN; - take_fork(&philo->fork, philo->id); if (get_death(philo, RETURN)) return (true); + take_fork(&philo->fork, philo->id); log_philo(philo); if (&(philo->fork.left) == philo->fork.right) return (print_death(philo)); + if (get_death(philo, RETURN)) + return (true); take_fork(&philo->fork, philo->id + 1); -// if (get_death(philo, RETURN)) -// return (true); log_philo(philo); philo->state = EAT; log_philo(philo); + pthread_mutex_lock(philo->check); gettimeofday(&philo->eat, NULL); + pthread_mutex_unlock(philo->check); if (sleep_phil(philo) == true) return (true); pthread_mutex_unlock(&philo->fork.left); diff --git a/philo/monitor.c b/philo/monitor.c index a8249b4..bf886ad 100644 --- a/philo/monitor.c +++ b/philo/monitor.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/12 12:13:35 by adjoly #+# #+# */ -/* Updated: 2024/08/12 20:50:17 by adjoly ### ########.fr */ +/* Updated: 2024/08/12 21:00:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,13 +22,17 @@ bool philo_check(t_philo *philo, t_pdata data) eaten = 0; while (i < data.philo_nbr) { + gettimeofday(&t1, NULL); + pthread_mutex_lock(philo->check); if (get_time_in_ms(philo[i].eat, t1) > data.die_time) { + pthread_mutex_unlock(philo->check); pthread_mutex_unlock(&philo[i].fork.left); pthread_mutex_unlock(philo[i].fork.right); return (print_death(&philo[i])); } + pthread_mutex_unlock(philo->check); if (philo[i].meal_left == 0) eaten++; if (get_death(philo, RETURN)) diff --git a/philo/philo_routine.c b/philo/philo_routine.c index ae26acf..fec7ee1 100644 --- a/philo/philo_routine.c +++ b/philo/philo_routine.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ -/* Updated: 2024/08/12 20:27:14 by adjoly ### ########.fr */ +/* Updated: 2024/08/12 21:03:47 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo/usleep.c b/philo/usleep.c index a270c49..79d8564 100644 --- a/philo/usleep.c +++ b/philo/usleep.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ -/* Updated: 2024/08/12 20:47:05 by adjoly ### ########.fr */ +/* Updated: 2024/08/12 21:05:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */