92 lines
2.8 KiB
C
92 lines
2.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* philo.h :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/21 15:38:19 by lfirmin #+# #+# */
|
|
/* Updated: 2024/11/21 15:38:19 by lfirmin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#ifndef PHILO_H
|
|
# define PHILO_H
|
|
|
|
# include <stdlib.h>
|
|
# include <stdio.h>
|
|
# include <limits.h>
|
|
# include <pthread.h>
|
|
# include <unistd.h>
|
|
# include <sys/time.h>
|
|
|
|
# define USAGE_MESS "Usage ► ./philo <number_of_philosophers> <time_to_die> \
|
|
<time_to_eat> <time_to_sleep> [number_of_times_each_philosopher_must_eat]"
|
|
|
|
typedef struct s_philo
|
|
{
|
|
long long id;
|
|
int l_fork;
|
|
int r_fork;
|
|
long long last_meal;
|
|
int m_eaten;
|
|
pthread_t thread;
|
|
pthread_mutex_t meal_lock;
|
|
struct s_data *data;
|
|
} t_philo;
|
|
|
|
typedef struct s_data
|
|
{
|
|
long long nb_philo;
|
|
long long t_die;
|
|
long long t_eat;
|
|
long long t_sleep;
|
|
long long must_eat;
|
|
long long start_time;
|
|
pthread_mutex_t *forks;
|
|
pthread_mutex_t print;
|
|
pthread_mutex_t dead_mutex;
|
|
int dead_flag;
|
|
t_philo *philos;
|
|
int threads_active;
|
|
pthread_mutex_t threads_count_mutex;
|
|
pthread_cond_t threads_done_cond;
|
|
} t_data;
|
|
|
|
//utils//
|
|
long long ft_atoll(const char *str);
|
|
t_data *init_data(void);
|
|
void free_data(t_data *data);
|
|
long long get_time(void);
|
|
void *death_check(void *arg);
|
|
int create_threads(t_data *data);
|
|
int check_if_dead(t_philo *philo);
|
|
int check_if_dead_data(t_data *data);
|
|
int print_message(t_philo *philo, char *message);
|
|
|
|
//init//
|
|
int init_forks(t_data *data);
|
|
int init_philos(t_data *data);
|
|
int init_philo_forks(t_data *data);
|
|
|
|
//parsing//
|
|
void ft_put_and_check_2(t_data *data, char **av, int *c);
|
|
int ft_put_and_check(t_data *data, char **av);
|
|
int validate_number(char **av, int i);
|
|
int ft_check_av(char **av);
|
|
int parsing(int ac, char **av, t_data *data);
|
|
|
|
//routine//
|
|
int thinking(t_philo *philo);
|
|
int sleeping(t_philo *philo);
|
|
int eating(t_philo *philo);
|
|
void philo_routine_init(t_philo *philo);
|
|
void *philo_routine(void *arg);
|
|
|
|
//routine_utils//
|
|
int eating_1_philo(t_philo *philo);
|
|
int take_fork_and_eat(t_philo *philo);
|
|
int take_fork_and_eat_alt(t_philo *philo);
|
|
int safe_sleep(t_philo *philo, long time);
|
|
int take_fork(t_philo *philo);
|
|
int ft_should_continue(t_philo *philo);
|
|
#endif |