aboutsummaryrefslogtreecommitdiff
path: root/philo_one/philo_one.h
blob: 7badd7103d25da279f848f2ad819e8fce25042d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   philo_one.h                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/11/24 06:11:16 by cacharle          #+#    #+#             */
/*   Updated: 2020/04/22 13:26:06 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef PHILO_ONE_H
# define PHILO_ONE_H

# include <unistd.h>
# include <sys/time.h>
# include <stdlib.h>
# include <pthread.h>
# include <stdbool.h>

typedef long int	t_time;

typedef enum
{
	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
{
	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);

/*
** forks.c
*/

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
*/

t_philo					*philos_new(int num);
void					philos_destroy(t_philo *philos, int num);
bool					philos_start(
							t_philo *philos,
							t_routine_arg *routine_args,
							int num);
void					philos_join(t_philo *philos, int num);
bool					philos_starved(t_philo *philos, int num);

/*
** routine.c
*/

void					*routine_philo(void *void_arg);
void					*routine_death(void *void_arg);

/*
** io.c
*/

void					io_eat(t_routine_arg *arg);
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