meilleur organisation code structures et variables
This commit is contained in:
42
srcs/init.c
42
srcs/init.c
@@ -1,6 +1,6 @@
|
||||
#include "philo.h"
|
||||
|
||||
t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i)
|
||||
t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
||||
{
|
||||
t_philo *new;
|
||||
|
||||
@@ -8,16 +8,18 @@ t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i)
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new->params = params;
|
||||
new->global = global;
|
||||
new->p_nbr = i + 1;
|
||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||
return (NULL);
|
||||
new->m_print = m_print;
|
||||
new->t_last_meal_s = 0;
|
||||
new->t_last_meal_u = 0;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
// looping chained list
|
||||
t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
|
||||
t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_philo *tmp;
|
||||
@@ -28,7 +30,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
|
||||
philo = NULL;
|
||||
while (i < params->n_phi)
|
||||
{
|
||||
tmp = lst_add_philo(params, m_print, i);
|
||||
tmp = lst_add_philo(params, global, i);
|
||||
if (philo)
|
||||
philo->next = tmp;
|
||||
else
|
||||
@@ -47,37 +49,53 @@ t_params *init_params(int ac, char **av)
|
||||
if (ac == 5 || ac == 6)
|
||||
{
|
||||
params = malloc(sizeof(t_params));
|
||||
if (!params)
|
||||
return (NULL);
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
if (ac == 6)
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
params->dead = 0;
|
||||
params->t_start_s = 0;
|
||||
params->t_start_u = 0;
|
||||
}
|
||||
else
|
||||
return (NULL);
|
||||
return (params);
|
||||
}
|
||||
|
||||
t_global *init_global(void)
|
||||
{
|
||||
t_global *global;
|
||||
|
||||
global = malloc(sizeof(t_global));
|
||||
if (!global)
|
||||
return (NULL);
|
||||
global->dead = 0;
|
||||
global->t_start_s = 0;
|
||||
global->t_start_u = 0;
|
||||
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||
return (NULL);
|
||||
if (pthread_mutex_init(&(global->m_dead), NULL) != 0)
|
||||
return (NULL);
|
||||
return (global);
|
||||
}
|
||||
|
||||
t_philo *init(int ac, char **av, pthread_t **id)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_params *params;
|
||||
t_mtx *m_print;
|
||||
t_global *global;
|
||||
|
||||
params = init_params(ac, av);
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
global = init_global();
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||
if (*id == NULL)
|
||||
return (NULL);
|
||||
m_print = malloc(sizeof(t_mtx));
|
||||
if (pthread_mutex_init(m_print, NULL) != 0)
|
||||
return (NULL);
|
||||
philo = init_chain_philo(params, m_print);
|
||||
philo = init_chain_philo(params, global);
|
||||
if (philo == NULL)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
|
||||
Reference in New Issue
Block a user