47 lines
1.6 KiB
C
47 lines
1.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* sleep.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */
|
|
/* Updated: 2024/08/08 18:06:32 by adjoly ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "philo.h"
|
|
#define SLEEP_SLICE_MS 5
|
|
|
|
uint32_t get_sleep_time(t_philo *philo)
|
|
{
|
|
if (philo->state == EAT)
|
|
return (philo->data.eat_time + get_current_time());
|
|
else if (philo->state == SLEEP)
|
|
return (philo->data.sleep_time + get_current_time());
|
|
else if (philo->state == DEAD)
|
|
return (philo->data.die_time + get_current_time());
|
|
return (0);
|
|
}
|
|
|
|
bool sleep_phil(t_philo *philo)
|
|
{
|
|
struct timeval t1;
|
|
uint32_t sleep_time;
|
|
|
|
sleep_time = get_sleep_time(philo);
|
|
while (get_current_time() < sleep_time)
|
|
{
|
|
if (get_death(false, true, philo))
|
|
return (true);
|
|
gettimeofday(&t1, NULL);
|
|
if (get_time_in_ms(philo->eat, t1) > philo->data.die_time)
|
|
{
|
|
get_death(true, false, philo);
|
|
return (print_death(philo));
|
|
}
|
|
usleep(SLEEP_SLICE_MS * 1000);
|
|
}
|
|
return (false);
|
|
}
|