diff options
Diffstat (limited to 'philo_one/src')
| -rw-r--r-- | philo_one/src/forks.c | 4 | ||||
| -rw-r--r-- | philo_one/src/main.c | 82 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 42 | ||||
| -rw-r--r-- | philo_one/src/philo_one.h | 8 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 27 |
5 files changed, 84 insertions, 79 deletions
diff --git a/philo_one/src/forks.c b/philo_one/src/forks.c index 264ffad..84bc0fd 100644 --- a/philo_one/src/forks.c +++ b/philo_one/src/forks.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */ -/* Updated: 2021/01/01 13:34:42 by charles ### ########.fr */ +/* Updated: 2021/01/10 11:29:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,8 @@ pthread_mutex_t *forks_new(long int num) void forks_destroy(pthread_mutex_t *forks, long int num) { + if (forks == NULL) + return ; while (num-- > 0) pthread_mutex_destroy(&forks[num]); free(forks); diff --git a/philo_one/src/main.c b/philo_one/src/main.c index 3dacf8f..9ac4cfc 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,43 +6,28 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/10 09:33:17 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:51:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -static int st_setup( +static int st_destroy( t_philo_conf *conf, - t_philo **philos, - pthread_mutex_t **forks) + t_philo *philos, + pthread_mutex_t *mutex_stdout, + pthread_mutex_t *mutex_meal_num_finished_counter) { - if ((*forks = forks_new(conf->philo_num)) == NULL) - return (1); - if ((*philos = philos_new(conf, *forks)) == NULL) - { - forks_destroy(*forks, conf->philo_num); - return (1); - } - if (pthread_mutex_init(&conf->mutex_stdout, NULL) != 0) - { - forks_destroy(*forks, conf->philo_num); - free(*philos); - return (1); - } - if (pthread_mutex_init(&conf->mutex_meal_num_finished_counter, NULL) != 0) - { - forks_destroy(*forks, conf->philo_num); - free(*philos); - pthread_mutex_destroy(&conf->mutex_stdout); - return (1); - } - conf->all_alive = true; - conf->meal_num_finished_counter = 0; - return (0); + philos_destroy(philos, conf->philo_num); + forks_destroy(conf->forks, conf->philo_num); + if (mutex_stdout != NULL) + pthread_mutex_destroy(mutex_stdout); + if (mutex_meal_num_finished_counter != NULL) + pthread_mutex_destroy(mutex_meal_num_finished_counter); + return (1); } -void *routine_flush(t_philo_conf *conf) +static void *st_routine_flush(t_philo_conf *conf) { while (true) { @@ -53,35 +38,50 @@ void *routine_flush(t_philo_conf *conf) } } +static int st_setup( + t_philo_conf *conf, + t_philo **philos) +{ + pthread_t thread_flush; + + if (pthread_create(&thread_flush, NULL, + (t_routine)st_routine_flush, (void*)&conf) != 0) + return (1); + pthread_detach(thread_flush); + if ((conf->forks = forks_new(conf->philo_num)) == NULL) + return (1); + if ((*philos = philos_new(conf)) == NULL) + return (st_destroy(conf, NULL, NULL, NULL)); + if (pthread_mutex_init(&conf->mutex_stdout, NULL) != 0) + return (st_destroy(conf, *philos, NULL, NULL)); + if (pthread_mutex_init(&conf->mutex_meal_num_finished_counter, NULL) != 0) + return (st_destroy(conf, *philos, &conf->mutex_stdout, NULL)); + conf->all_alive = true; + conf->meal_num_finished_counter = 0; + return (0); +} + int main(int argc, char **argv) { t_philo_conf conf; t_philo *philos; - pthread_mutex_t *forks; if (!parse_args((t_philo_args*)&conf, argc, argv)) return (1); if (conf.philo_num == 0 || conf.meal_num == 0) return (0); - if (st_setup(&conf, &philos, &forks) != 0) + if (st_setup(&conf, &philos) != 0) return (1); - pthread_t thread_flush; - pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf); - pthread_detach(thread_flush); conf.initial_time = h_time_now(); if (!philos_start(philos, conf.philo_num)) { - forks_destroy(forks, conf.philo_num); - free(philos); - return (1); + return (st_destroy(&conf, philos, + &conf.mutex_stdout, &conf.mutex_meal_num_finished_counter)); } while (!philo_finished(&conf)) usleep(500); philo_put_flush(); - philos_detach(philos, conf.philo_num); - forks_destroy(forks, conf.philo_num); - pthread_mutex_destroy(&conf.mutex_stdout); - pthread_mutex_destroy(&conf.mutex_meal_num_finished_counter); - free(philos); + st_destroy(&conf, philos, + &conf.mutex_stdout, &conf.mutex_meal_num_finished_counter); return (0); } diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 8a41fe7..1e8e691 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,13 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2021/01/10 09:49:48 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:50:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks) +t_philo *philos_new(t_philo_conf *conf) { long int i; t_philo *philos; @@ -26,11 +26,15 @@ t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks) { philos[i].id = i + 1; philos[i].conf = conf; - philos[i].fork_left = forks + i % conf->philo_num; - philos[i].fork_right = forks + (i + 1) % conf->philo_num; - pthread_mutex_init(&philos[i].mutex_start, NULL); + philos[i].fork_left = conf->forks + i % conf->philo_num; + philos[i].fork_right = conf->forks + (i + 1) % conf->philo_num; + if (pthread_mutex_init(&philos[i].mutex_start, NULL) != 0 || + pthread_mutex_init(&philos[i].mutex_eat, NULL) != 0) + { + philos_destroy(philos, i); + return (false); + } pthread_mutex_lock(&philos[i].mutex_start); - pthread_mutex_init(&philos[i].mutex_eat, NULL); } return (philos); } @@ -47,32 +51,21 @@ bool philos_start(t_philo *philos, long int num) NULL, (t_routine)routine_philo, (void*)(philos + i)) != 0) - { - while (--i >= 0) - pthread_detach(philos[i].thread); return (false); - } } i = -1; while (++i < num) - { - if (i % 2 == 0) - continue; - pthread_mutex_unlock(&philos[i].mutex_start); - } + if (i % 2 == 1) + pthread_mutex_unlock(&philos[i].mutex_start); usleep(1000); i = -1; while (++i < num) - { - if (i % 2 == 1) - continue; - pthread_mutex_unlock(&philos[i].mutex_start); - } - + if (i % 2 == 0) + pthread_mutex_unlock(&philos[i].mutex_start); return (true); } -void philos_detach(t_philo *philos, long int num) +void philos_destroy(t_philo *philos, long int num) { long int i; @@ -80,5 +73,10 @@ void philos_detach(t_philo *philos, long int num) return ; i = -1; while (++i < num) + { + pthread_mutex_destroy(&philos[i].mutex_start); + pthread_mutex_destroy(&philos[i].mutex_eat); pthread_detach(philos[i].thread); + } + free(philos); } diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index aeabc4c..2af457e 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/10 09:50:02 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:42:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ typedef struct t_time initial_time; pthread_mutex_t mutex_stdout; pthread_mutex_t mutex_meal_num_finished_counter; + pthread_mutex_t *forks; } t_philo_conf; typedef struct s_philo @@ -66,10 +67,9 @@ 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, long int num); +t_philo *philos_new(t_philo_conf *conf); bool philos_start(t_philo *philos, long int num); -void philos_detach(t_philo *philos, long int num); +void philos_destroy(t_philo *philos, long int num); /* ** routine.c diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 01c74d5..4e1f39f 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/10 09:54:30 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:52:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,20 @@ static void st_check_meal_num_finished(t_philo *arg, long int eat_counter) } } +static void st_take_both_forks(t_philo *arg) +{ + if (arg->id % 2 == 0) + { + event_take_fork(arg, arg->fork_left); + event_take_fork(arg, arg->fork_right); + } + else + { + event_take_fork(arg, arg->fork_right); + event_take_fork(arg, arg->fork_left); + } +} + void *routine_philo(t_philo *arg) { pthread_t thread_death; @@ -45,16 +59,7 @@ void *routine_philo(t_philo *arg) arg->time_last_eat = h_time_now(); while (!philo_finished(arg->conf)) { - if (arg->id % 2 == 0) - { - event_take_fork(arg, arg->fork_left); - event_take_fork(arg, arg->fork_right); - } - else - { - event_take_fork(arg, arg->fork_right); - event_take_fork(arg, arg->fork_left); - } + st_take_both_forks(arg); event_eat(arg); eat_counter++; st_check_meal_num_finished(arg, eat_counter); |
