aboutsummaryrefslogtreecommitdiff
path: root/philo_one/src
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-02 10:51:53 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-02 10:51:53 +0100
commit280dace8bf97242151faf8297af31de67f77085c (patch)
tree7fa68ce3deb4d9236d13de5c05201fcb491b6887 /philo_one/src
parenta73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb (diff)
downloadphilosophers-280dace8bf97242151faf8297af31de67f77085c.tar.gz
philosophers-280dace8bf97242151faf8297af31de67f77085c.tar.bz2
philosophers-280dace8bf97242151faf8297af31de67f77085c.zip
Norming philo_one
Diffstat (limited to 'philo_one/src')
-rw-r--r--philo_one/src/event.c7
-rw-r--r--philo_one/src/main.c5
-rw-r--r--philo_one/src/routine.c21
3 files changed, 19 insertions, 14 deletions
diff --git a/philo_one/src/event.c b/philo_one/src/event.c
index ab8da0b..2a0c302 100644
--- a/philo_one/src/event.c
+++ b/philo_one/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2021/01/01 13:46:50 by charles ### ########.fr */
+/* Updated: 2021/01/02 10:49:08 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,7 +47,10 @@ void event_think(t_philo *arg)
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
-void event_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left)
+void event_sleep(
+ t_philo *arg,
+ pthread_mutex_t *fork_right,
+ pthread_mutex_t *fork_left)
{
if (philo_finished(arg->conf))
return ;
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index 5ee4ba5..0c406b0 100644
--- a/philo_one/src/main.c
+++ b/philo_one/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2021/01/01 13:43:46 by charles ### ########.fr */
+/* Updated: 2021/01/02 10:49:26 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -62,9 +62,6 @@ int main(int argc, char **argv)
}
while (!philo_finished(&conf))
;
- /* if (conf.meal_num != -1 && conf.meal_num_finished_counter == conf.philo_num) */
- /* break; */
- /* conf.all_alive = false; */
philos_detach(philos, conf.philo_num);
forks_destroy(forks, conf.philo_num);
pthread_mutex_destroy(&conf.mutex_stdout);
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index d8fa3a0..6ee37f1 100644
--- a/philo_one/src/routine.c
+++ b/philo_one/src/routine.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2021/01/01 14:24:05 by charles ### ########.fr */
+/* Updated: 2021/01/02 10:51:36 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,6 +19,17 @@ inline bool philo_finished(t_philo_conf *conf)
conf->meal_num_finished_counter == conf->philo_num));
}
+static void st_check_meal_num_finished(t_philo *arg, long int eat_counter)
+{
+ if (!philo_finished(arg->conf) && arg->conf->meal_num != -1 &&
+ eat_counter == arg->conf->meal_num)
+ {
+ pthread_mutex_lock(&arg->conf->mutex_meal_num_finished_counter);
+ arg->conf->meal_num_finished_counter++;
+ pthread_mutex_unlock(&arg->conf->mutex_meal_num_finished_counter);
+ }
+}
+
void *routine_philo(t_philo *arg)
{
pthread_t thread_death;
@@ -38,13 +49,7 @@ void *routine_philo(t_philo *arg)
event_eat(arg);
arg->time_last_eat = h_time_now();
eat_counter++;
- if (!philo_finished(arg->conf) && arg->conf->meal_num != -1 &&
- eat_counter == arg->conf->meal_num)
- {
- pthread_mutex_lock(&arg->conf->mutex_meal_num_finished_counter);
- arg->conf->meal_num_finished_counter++;
- pthread_mutex_unlock(&arg->conf->mutex_meal_num_finished_counter);
- }
+ st_check_meal_num_finished(arg, eat_counter);
event_sleep(arg, arg->fork_right, arg->fork_left);
event_think(arg);
}