aboutsummaryrefslogtreecommitdiff
path: root/philo_one/src
diff options
context:
space:
mode:
Diffstat (limited to 'philo_one/src')
-rw-r--r--philo_one/src/event.c35
-rw-r--r--philo_one/src/forks.c8
-rw-r--r--philo_one/src/main.c11
-rw-r--r--philo_one/src/philo.c14
-rw-r--r--philo_one/src/philo_one.h16
-rw-r--r--philo_one/src/routine.c30
6 files changed, 65 insertions, 49 deletions
diff --git a/philo_one/src/event.c b/philo_one/src/event.c
index ef3d7a5..ab8da0b 100644
--- a/philo_one/src/event.c
+++ b/philo_one/src/event.c
@@ -6,48 +6,53 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/12/30 13:39:56 by charles ### ########.fr */
+/* Updated: 2021/01/01 13:46:50 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-void event_take_fork(t_philo *arg, pthread_mutex_t *fork)
+void event_take_fork(t_philo *arg, pthread_mutex_t *fork)
{
+ if (philo_finished(arg->conf))
+ return ;
pthread_mutex_lock(fork);
pthread_mutex_lock(&arg->conf->mutex_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_FORK);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
-void event_eat(t_philo *arg)
+void event_eat(t_philo *arg)
{
+ if (philo_finished(arg->conf))
+ return ;
pthread_mutex_lock(&arg->conf->mutex_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_EAT);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
usleep(arg->conf->timeout_eat * 1000);
}
-void event_think(t_philo *arg)
+void event_think(t_philo *arg)
{
+ if (philo_finished(arg->conf))
+ return ;
pthread_mutex_lock(&arg->conf->mutex_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_THINK);
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 ;
pthread_mutex_lock(&arg->conf->mutex_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_SLEEP);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
@@ -56,10 +61,12 @@ void event_sleep(
usleep(arg->conf->timeout_sleep * 1000);
}
-void event_die(t_philo *arg)
+void event_die(t_philo *arg)
{
+ if (philo_finished(arg->conf))
+ return ;
pthread_mutex_lock(&arg->conf->mutex_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
arg->conf->all_alive = false;
philo_put(arg->id, EVENT_DIE);
diff --git a/philo_one/src/forks.c b/philo_one/src/forks.c
index fef1de0..264ffad 100644
--- a/philo_one/src/forks.c
+++ b/philo_one/src/forks.c
@@ -6,15 +6,15 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */
-/* Updated: 2020/09/30 09:45:46 by cacharle ### ########.fr */
+/* Updated: 2021/01/01 13:34:42 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-pthread_mutex_t *forks_new(int num)
+pthread_mutex_t *forks_new(long int num)
{
- int i;
+ long int i;
pthread_mutex_t *forks;
if ((forks = malloc(num * sizeof(pthread_mutex_t))) == NULL)
@@ -31,7 +31,7 @@ pthread_mutex_t *forks_new(int num)
return (forks);
}
-void forks_destroy(pthread_mutex_t *forks, int num)
+void forks_destroy(pthread_mutex_t *forks, long int num)
{
while (num-- > 0)
pthread_mutex_destroy(&forks[num]);
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index 0c4b95c..5ee4ba5 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: 2020/12/31 19:22:08 by charles ### ########.fr */
+/* Updated: 2021/01/01 13:43:46 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -60,10 +60,11 @@ int main(int argc, char **argv)
free(philos);
return (1);
}
- while (conf.all_alive)
- if (conf.meal_num != -1 && conf.meal_num_finished_counter == conf.philo_num)
- break;
- conf.all_alive = false;
+ 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/philo.c b/philo_one/src/philo.c
index 3e3d90f..1ffb15b 100644
--- a/philo_one/src/philo.c
+++ b/philo_one/src/philo.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
-/* Updated: 2020/12/30 11:58:51 by charles ### ########.fr */
+/* Updated: 2021/01/01 13:33:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,8 @@
t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks)
{
- int i;
- t_philo *philos;
+ long int i;
+ t_philo *philos;
if (conf->philo_num < 0)
return (NULL);
@@ -32,9 +32,9 @@ t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks)
return (philos);
}
-bool philos_start(t_philo *philos, int num)
+bool philos_start(t_philo *philos, long int num)
{
- int i;
+ long int i;
i = -1;
while (++i < num)
@@ -54,9 +54,9 @@ bool philos_start(t_philo *philos, int num)
return (true);
}
-void philos_detach(t_philo *philos, int num)
+void philos_detach(t_philo *philos, long int num)
{
- int i;
+ long int i;
if (philos == NULL)
return ;
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index 341c1a8..b566f80 100644
--- a/philo_one/src/philo_one.h
+++ b/philo_one/src/philo_one.h
@@ -6,13 +6,14 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2020/12/30 13:44:14 by charles ### ########.fr */
+/* Updated: 2021/01/01 13:51:29 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_ONE_H
# define PHILO_ONE_H
+# define _XOPEN_SOURCE 500
# include <unistd.h>
# include <sys/time.h>
# include <stdlib.h>
@@ -42,7 +43,7 @@ typedef struct
typedef struct s_philo
{
- int id;
+ long int id;
pthread_t thread;
t_time time_last_eat;
t_philo_conf *conf;
@@ -55,22 +56,23 @@ typedef struct s_philo
** forks.c
*/
-pthread_mutex_t *forks_new(int num);
-void forks_destroy(pthread_mutex_t *forks, int num);
+pthread_mutex_t *forks_new(long int num);
+void forks_destroy(pthread_mutex_t *forks, long int num);
/*
** philo.c
*/
t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks);
-void philos_destroy(t_philo *philos, int num);
-bool philos_start(t_philo *philos, int num);
-void philos_detach(t_philo *philos, int num);
+void philos_destroy(t_philo *philos, long int num);
+bool philos_start(t_philo *philos, long int num);
+void philos_detach(t_philo *philos, long int num);
/*
** routine.c
*/
+bool philo_finished(t_philo_conf *conf);
void *routine_philo(t_philo *arg);
void *routine_death(t_philo *arg);
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index 280f9d6..e9775cc 100644
--- a/philo_one/src/routine.c
+++ b/philo_one/src/routine.c
@@ -6,32 +6,40 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2020/12/30 13:44:25 by charles ### ########.fr */
+/* Updated: 2021/01/01 13:57:11 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-void *routine_philo(t_philo *arg)
+inline bool philo_finished(t_philo_conf *conf)
{
- pthread_t thread_death;
- long int eat_counter;
+ return (!conf->all_alive ||
+ (conf->meal_num != -1 &&
+ conf->meal_num_finished_counter == conf->philo_num));
+}
+
+void *routine_philo(t_philo *arg)
+{
+ pthread_t thread_death;
+ long int eat_counter;
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return (NULL);
arg->time_last_eat = h_time_now();
if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0)
return (NULL);
event_think(arg);
eat_counter = 0;
- while (arg->conf->all_alive)
+ while (!philo_finished(arg->conf))
{
event_take_fork(arg, arg->fork_left);
event_take_fork(arg, arg->fork_right);
arg->time_last_eat = h_time_now();
event_eat(arg);
eat_counter++;
- if (arg->conf->meal_num != -1 && eat_counter == arg->conf->meal_num)
+ 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++;
@@ -44,19 +52,17 @@ void *routine_philo(t_philo *arg)
return (NULL);
}
-void *routine_death(t_philo *arg)
+void *routine_death(t_philo *arg)
{
- t_time current;
+ t_time current;
current = h_time_now();
- while (arg->conf->all_alive &&
+ while (!philo_finished(arg->conf) &&
current - arg->time_last_eat < arg->conf->timeout_death)
{
current = h_time_now();
usleep(200);
}
- if (!arg->conf->all_alive)
- return (NULL);
event_die(arg);
return (NULL);
}