42_Philosopher/philo/philo_routine.c

115 lines
3.1 KiB
C
Raw Normal View History

2023-04-27 08:05:14 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
2023-05-25 13:25:47 -04:00
/* Updated: 2023/05/25 19:22:55 by cchauvet ### ########.fr */
2023-04-27 08:05:14 -04:00
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "./time.h"
#include "./philo.h"
#include "./print.h"
#include "data.h"
2023-05-25 11:31:02 -04:00
bool check(t_data *data)
2023-04-27 08:05:14 -04:00
{
bool stop;
2023-05-25 11:31:02 -04:00
if (data->nb_meals != -1 && get_min_meal(data) == (size_t) data->nb_meals)
2023-04-27 08:05:14 -04:00
{
2023-05-25 11:31:02 -04:00
pthread_mutex_lock(&data->stop_mutex);
data->stop = 1;
pthread_mutex_unlock(&data->stop_mutex);
2023-04-27 08:05:14 -04:00
}
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);
2023-05-25 11:31:02 -04:00
if (check(data))
2023-04-27 08:05:14 -04:00
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
while ((philo->id + 1) % data->nb_philos == philo->id)
{
2023-05-25 11:31:02 -04:00
if (check(data))
2023-04-27 08:05:14 -04:00
{
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);
2023-05-25 11:31:02 -04:00
if (check(data))
2023-04-27 08:05:14 -04:00
{
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);
usleep(get_time_eat(philo, data) * 1000);
2023-04-27 08:05:14 -04:00
pthread_mutex_unlock(&data->forks[philo->id]);
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
2023-05-25 11:31:02 -04:00
if (check(data))
2023-04-27 08:05:14 -04:00
return (1);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex);
2023-05-25 13:25:47 -04:00
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
2023-04-27 08:05:14 -04:00
return (0);
}
2023-05-24 10:19:21 -04:00
int philo_sleep(t_data *data, t_philo *philo)
2023-04-27 08:05:14 -04:00
{
print_sleeping(philo);
usleep(get_time_sleep(philo, data) * 1000);
2023-05-24 10:19:21 -04:00
return (0);
2023-04-27 08:05:14 -04:00
}
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);
2023-04-27 08:05:14 -04:00
while (true)
{
2023-05-25 11:31:02 -04:00
if (check(data)
2023-05-25 09:24:35 -04:00
|| philo_eat(philo, data)
2023-05-25 11:31:02 -04:00
|| check(data)
2023-05-25 09:24:35 -04:00
|| philo_sleep(data, philo)
2023-05-25 11:31:02 -04:00
|| check(data))
2023-05-24 10:19:21 -04:00
{
2023-05-25 09:24:35 -04:00
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
2023-04-27 08:05:14 -04:00
return (NULL);
2023-05-24 10:19:21 -04:00
}
2023-04-27 08:05:14 -04:00
print_thinking(philo);
}
return (NULL);
}