aboutsummaryrefslogtreecommitdiff
path: root/philo_one/philo_one.h
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-22 13:43:00 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-22 13:43:00 +0200
commit3c2c3a07396c001f51eac8d0fd0fc84e83eb127b (patch)
tree7c646f128d97784fba9fd76cf8bb758a9b965c76 /philo_one/philo_one.h
parent2c5abe421b7a1b92081e38f6b1f04d407fcba834 (diff)
downloadphilosophers-3c2c3a07396c001f51eac8d0fd0fc84e83eb127b.tar.gz
philosophers-3c2c3a07396c001f51eac8d0fd0fc84e83eb127b.tar.bz2
philosophers-3c2c3a07396c001f51eac8d0fd0fc84e83eb127b.zip
philo_one refactoring without common lib
Diffstat (limited to 'philo_one/philo_one.h')
-rw-r--r--philo_one/philo_one.h98
1 files changed, 72 insertions, 26 deletions
diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h
index 498cd49..7badd71 100644
--- a/philo_one/philo_one.h
+++ b/philo_one/philo_one.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2020/02/15 01:25:40 by cacharle ### ########.fr */
+/* Updated: 2020/04/22 13:26:06 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,36 +17,60 @@
# include <sys/time.h>
# include <stdlib.h>
# include <pthread.h>
-# include "common.h"
+# include <stdbool.h>
-typedef pthread_mutex_t t_fork;
+typedef long int t_time;
-typedef struct s_philo
+typedef enum
{
- int id;
- t_bool alive;
- t_time time_last_eat;
- t_philo_state state;
- pthread_t thread;
-} t_philo;
-
-typedef struct s_routine_arg
+ EVENT_FORK,
+ EVENT_EAT,
+ EVENT_SLEEP,
+ EVENT_THINK,
+ EVENT_DIE
+} t_philo_event;
+
+typedef struct
+{
+ int philo_num;
+ t_time timeout_death;
+ t_time timeout_eat;
+ t_time timeout_sleep;
+ int meal_num;
+ bool all_alive;
+ pthread_mutex_t mutex_stdout;
+ pthread_mutex_t mutex_all_alive;
+} t_philo_conf;
+
+typedef struct s_philo
{
- t_philo_args *args;
- t_philo *philo;
- t_fork *fork_left;
- t_fork *fork_right;
- pthread_mutex_t *mutex_stdout;
-} t_routine_arg;
+ int id;
+ bool alive;
+ t_time time_last_eat;
+ pthread_t thread;
+} t_philo;
+
+typedef struct s_routine_arg
+{
+ t_philo_conf *conf;
+ t_philo *philo;
+ pthread_mutex_t *fork_left;
+ pthread_mutex_t *fork_right;
+ pthread_mutex_t *mutex_stdout;
+} t_routine_arg;
+
+typedef void (*t_philo_routine)(void *arg);
/*
-** fork.c
+** forks.c
*/
-t_fork *forks_new(int num);
-void forks_destroy(t_fork *forks, int num);
-t_routine_arg *forks_dispatch(t_philo *philos,
- t_fork *forks, t_philo_args *args);
+pthread_mutex_t *forks_new(int num);
+void forks_destroy(pthread_mutex_t *forks, int num);
+t_routine_arg *forks_dispatch(
+ t_philo *philos,
+ pthread_mutex_t *forks,
+ t_philo_conf *conf);
/*
** philo.c
@@ -54,10 +78,12 @@ t_routine_arg *forks_dispatch(t_philo *philos,
t_philo *philos_new(int num);
void philos_destroy(t_philo *philos, int num);
-t_bool philos_start(t_philo *philos,
- t_routine_arg *routine_args, int num);
+bool philos_start(
+ t_philo *philos,
+ t_routine_arg *routine_args,
+ int num);
void philos_join(t_philo *philos, int num);
-t_bool philos_starved(t_philo *philos, int num);
+bool philos_starved(t_philo *philos, int num);
/*
** routine.c
@@ -75,4 +101,24 @@ void io_think(t_routine_arg *arg);
void io_sleep(t_routine_arg *arg);
void io_die(t_routine_arg *arg);
+/*
+** common.c
+*/
+
+bool parse_args(
+ t_philo_conf *philo_args,
+ int argc,
+ char **argv);
+
+/*
+** helper.c
+*/
+
+long int h_atoi_strict_unsigned(char *s);
+void h_putnbr(unsigned long num);
+void h_putchar(char c);
+void h_putstr(char *s);
+void *h_calloc(int count, int size);
+t_time h_time_now(void);
+
#endif