philo.h
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   philo.h                                            :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/04/18 18:13:11 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/06/01 22:56:37 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #ifndef PHILO_H
14  # define PHILO_H
15  
16  # include <pthread.h>
17  # include <stdio.h>
18  # include <stdlib.h>
19  # include <string.h>
20  # include <sys/time.h>
21  # include <unistd.h>
22  
23  # define TRUE 1
24  # define FALSE 0
25  # define USAGE1 "Usage: ./philo number_of_philosophers time_to_die time_to_eat"
26  # define USAGE2 " time_to_sleep [number_of_times_each_philosopher_must_eat]\n"
27  
28  typedef struct s_args
29  {
30  	int	philo_num;
31  	int	philo_time_die;
32  	int	philo_time_eat;
33  	int	philo_time_sleep;
34  	int	philo_must_eat;
35  }	t_args;
36  
37  typedef struct s_share
38  {
39  	t_args			args;
40  	int				*forks;
41  	pthread_mutex_t	*fork_locks;
42  	pthread_mutex_t	share_lock;
43  	int				stop;
44  	long long		philo_start_time;
45  }	t_share;
46  
47  typedef struct s_philo
48  {
49  	t_share			*share;
50  	int				*forks[2];
51  	pthread_mutex_t	*fork_lock[2];
52  	pthread_t		philo_thread;
53  	int				philo_id;
54  	int				philo_count_eat;
55  	long long		philo_time_last_eat;
56  	int				error;
57  }	t_philo;
58  
59  void		philo_print(t_philo *philo, char *str);
60  int			philo_sleep(long long wait_time, t_philo *philo);
61  void		philo_dead(t_philo *philo);
62  long long	get_current_time(t_philo *philo);
63  
64  int			clear_mutex_array(pthread_mutex_t *mutex_array, int index);
65  int			clear_all_mutex(t_share *share);
66  int			clear_and_detach_all_thread(t_philo *philos, t_share *share);
67  
68  void		init_struct_args(t_args *args, char **argv);
69  void		init_struct_share(t_share *share, t_args args);
70  void		init_struct_philo(t_philo **philos, t_share *share, t_args args);
71  int			init_all_mutex(t_share *share, int philo_num);
72  int			init_all_malloc(t_share *share, t_philo **philos, int philo_num);
73  
74  void		lock(pthread_mutex_t *mutex, t_philo *philo);
75  void		unlock(pthread_mutex_t *mutex, t_philo *philo);
76  int			atoi_only_unsigned(char *n);
77  int			valid_input(char **argv);
78  
79  int			simulate(t_philo *philos, t_share *share);
80  
81  #endif