philosopher/philo/philo.h
Etienne Rey-bethbeder 29832e45c3 _
2023-04-09 22:41:29 +02:00

79 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
/* Updated: 2023/04/09 19:29:44 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_H
# define PHILO_H
# include "utils/utils.h"
# include <unistd.h>
# include <sys/time.h>
# include <pthread.h>
typedef struct s_config
{
int nbr_philo;
long time_die;
long time_eat;
long time_sleep;
long must_eat;
} t_config;
typedef struct s_same
{
t_config *config;
int *forks;
pthread_mutex_t *mutex;
int all_eat;
int death;
long long time;
} t_same;
typedef struct s_only
{
int id;
int next_id;
long long last_eat_time;
int eat;
} t_only;
typedef struct s_philo
{
t_same *same;
t_only *only;
} t_philo;
/* PARSING */
int parsing(char *argv[], t_config *conf);
/* MANAGE THREADS */
int manage_threads(t_config *config);
/* UTILS */
long long get_utime(void);
int verif_die_eating(t_philo *philo, t_only *only, t_config *config);
int verif_finish(t_philo *philo, t_only *only);
int finish(t_philo *philo, long long last_eat_time);
void message_die(t_philo *philo, char *msg);
void message(t_philo *philo, char *msg);
int init_mutex(pthread_mutex_t *mutex, int nbr);
void destroy_mutex(pthread_mutex_t *mutex, int nbr);
int free_all(pthread_t *threads, t_same *same, t_philo **philo);
int get_fork(t_philo *philo, int id_fork);
void take_fork(t_philo *philo, int id_fork);
void drop_fork(t_philo *philo, int id_fork, int next_id_fork);
void ft_sleep(long long time_to_sleep);
/* THREADS */
void *philosopher(t_philo *philo);
#endif