intra to gitea
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* routine.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/22 14:47:26 by lfirmin #+# #+# */
|
||||
/* Updated: 2025/04/17 14:27:04 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/philo.h"
|
||||
|
||||
int eating(t_philo *philo)
|
||||
{
|
||||
if (!philo)
|
||||
return (1);
|
||||
if (check_if_dead(philo))
|
||||
return (1);
|
||||
if (take_fork_and_eat(philo))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int sleeping(t_philo *philo)
|
||||
{
|
||||
if (check_if_dead(philo))
|
||||
return (1);
|
||||
print_message(philo, "is sleeping");
|
||||
if (safe_sleep(philo, philo->data->t_sleep * 1000))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int thinking(t_philo *philo)
|
||||
{
|
||||
if (print_message(philo, "is thinking"))
|
||||
return (1);
|
||||
if (philo->data->nb_philo % 2 == 0)
|
||||
{
|
||||
if (philo->data->t_eat > philo->data->t_sleep)
|
||||
usleep((philo->data->t_eat - philo->data->t_sleep) * 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (2 * philo->data->t_eat > philo->data->t_sleep)
|
||||
usleep((2 * philo->data->t_eat - philo->data->t_sleep) * 1000);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void philo_routine_init(t_philo *philo)
|
||||
{
|
||||
if (philo->id % 2 == 0)
|
||||
{
|
||||
if (philo->data->nb_philo > 100)
|
||||
usleep(500);
|
||||
else
|
||||
usleep(1000);
|
||||
}
|
||||
pthread_mutex_lock(&philo->meal_lock);
|
||||
philo->last_meal = get_time();
|
||||
pthread_mutex_unlock(&philo->meal_lock);
|
||||
}
|
||||
|
||||
void *philo_routine(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
int should_continue;
|
||||
|
||||
philo = (t_philo *)arg;
|
||||
if (philo->data->nb_philo == 1)
|
||||
return (eating_1_philo(philo), NULL);
|
||||
if (philo->id % 2 == 0)
|
||||
usleep(philo->data->t_eat * 500);
|
||||
while (1)
|
||||
{
|
||||
pthread_mutex_lock(&philo->data->dead_mutex);
|
||||
should_continue = ft_should_continue(philo);
|
||||
pthread_mutex_unlock(&philo->data->dead_mutex);
|
||||
if (!should_continue)
|
||||
break ;
|
||||
if (eating(philo) == 1)
|
||||
break ;
|
||||
if (sleeping(philo) == 1)
|
||||
break ;
|
||||
if (thinking(philo) == 1)
|
||||
break ;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user