aboutsummaryrefslogtreecommitdiff
path: root/philo_one/src/philo_one.h
blob: 2ebe708eafd2b708d59f52f096061acbf3d75da4 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   philo_one.h                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/11/24 06:11:16 by cacharle          #+#    #+#             */
/*   Updated: 2021/01/04 10:38:44 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef PHILO_ONE_H
# define PHILO_ONE_H

# define _XOPEN_SOURCE 500
# include <unistd.h>
# include <sys/time.h>
# include <stdlib.h>
# include <pthread.h>
# include <stdbool.h>
# include <limits.h>
# include <stdarg.h>

# include "common.h"

/*
** t_philo_args with more fields at the end
*/

typedef struct
{
	long int		philo_num;
	t_time			timeout_death;
	t_time			timeout_eat;
	t_time			timeout_sleep;
	long int		meal_num;
	bool			all_alive;
	long int		meal_num_finished_counter;
	t_time			initial_time;
	pthread_mutex_t	mutex_stdout;
	pthread_mutex_t	mutex_meal_num_finished_counter;
}					t_philo_conf;

typedef struct		s_philo
{
	long int		id;
	pthread_t		thread;
	t_time			time_last_eat;
	t_philo_conf	*conf;
	pthread_mutex_t	*fork_left;
	pthread_mutex_t	*fork_right;
	pthread_mutex_t	*mutex_stdout;
	pthread_mutex_t	mutex_start;
}					t_philo;

/*
** forks.c
*/

pthread_mutex_t		*forks_new(long int num);
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);
bool				philos_start(t_philo *philos, long int num);
void				philos_detach(t_philo *philos, long int num);

/*
** routine.c
*/

bool				philo_finished(t_philo_conf *conf);
void				*routine_philo(t_philo *arg);
void				*routine_death(t_philo *arg);

/*
** io.c
*/

void				event_take_fork(t_philo *arg);
void				event_eat(t_philo *arg);
void				event_think(t_philo *arg);
void				event_sleep(
						t_philo *arg,
						pthread_mutex_t *fork_right,
						pthread_mutex_t *fork_left);
void				event_die(t_philo *arg);

#endif