115 lines
3.1 KiB
C
115 lines
3.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* philo_routine.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
|
|
/* Updated: 2023/06/01 14:23:53 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <unistd.h>
|
|
#include "./time.h"
|
|
#include "./philo.h"
|
|
#include "./print.h"
|
|
#include "data.h"
|
|
|
|
bool check(t_data *data)
|
|
{
|
|
bool stop;
|
|
|
|
if (data->nb_meals != -1 && get_min_meal(data) == (size_t) data->nb_meals)
|
|
{
|
|
pthread_mutex_lock(&data->stop_mutex);
|
|
data->stop = 1;
|
|
pthread_mutex_unlock(&data->stop_mutex);
|
|
}
|
|
pthread_mutex_lock(&data->stop_mutex);
|
|
stop = data->stop;
|
|
pthread_mutex_unlock(&data->stop_mutex);
|
|
return (stop);
|
|
}
|
|
|
|
int philo_take_forks(t_philo *philo, t_data *data)
|
|
{
|
|
pthread_mutex_lock(&data->forks[philo->id]);
|
|
print_take_a_fork(philo);
|
|
if (check(data))
|
|
{
|
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
return (1);
|
|
}
|
|
while ((philo->id + 1) % data->nb_philos == philo->id)
|
|
{
|
|
if (check(data))
|
|
{
|
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
return (1);
|
|
}
|
|
}
|
|
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
|
print_take_a_fork(philo);
|
|
if (check(data))
|
|
{
|
|
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
return (1);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
bool philo_eat(t_philo *philo, t_data *data)
|
|
{
|
|
if (philo_take_forks(philo, data))
|
|
return (1);
|
|
print_eating(philo);
|
|
pthread_mutex_lock(&philo->last_eat_mutex);
|
|
philo->last_eat = get_time();
|
|
pthread_mutex_unlock(&philo->last_eat_mutex);
|
|
usleep(get_time_eat(philo, data) * 1000);
|
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
|
if (check(data))
|
|
return (1);
|
|
pthread_mutex_lock(&philo->nb_meal_mutex);
|
|
philo->nb_meal++;
|
|
pthread_mutex_unlock(&philo->nb_meal_mutex);
|
|
return (0);
|
|
}
|
|
|
|
int philo_sleep(t_data *data, t_philo *philo)
|
|
{
|
|
print_sleeping(philo);
|
|
usleep(get_time_sleep(philo, data) * 1000);
|
|
return (0);
|
|
}
|
|
|
|
void *philo_routine(void *arg)
|
|
{
|
|
t_philo *philo;
|
|
t_data *data;
|
|
|
|
philo = arg;
|
|
data = philo->data;
|
|
print_thinking(philo);
|
|
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
|
|
while (true)
|
|
{
|
|
if (check(data)
|
|
|| philo_eat(philo, data)
|
|
|| check(data)
|
|
|| philo_sleep(data, philo)
|
|
|| check(data))
|
|
{
|
|
pthread_mutex_lock(&philo->stop_mutex);
|
|
philo->stop = 1;
|
|
pthread_mutex_unlock(&philo->stop_mutex);
|
|
return (NULL);
|
|
}
|
|
print_thinking(philo);
|
|
}
|
|
return (NULL);
|
|
}
|