42_Philosopher/print.c
2023-04-25 14:30:21 +00:00

67 lines
1.3 KiB
C

#include "philo.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include "./time.h"
#include "./utils/utils.h"
static void print(t_data *data, size_t id, char *str)
{
size_t time;
bool stop;
time = get_time();
pthread_mutex_lock(&data->print_mutex);
pthread_mutex_lock(&data->stop_mutex);
stop = data->stop;
pthread_mutex_unlock(&data->stop_mutex);
if (stop == false)
{
ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(id + 1, 3);
ft_putchar(' ');
ft_putstr(str);
ft_putchar('\n');
}
pthread_mutex_unlock(&data->print_mutex);
}
void print_take_a_fork(t_philo *philo)
{
print(philo->data, philo->id, "has taken a fork");
}
void print_eating(t_philo *philo)
{
print(philo->data, philo->id, "is eating");
}
void print_sleeping(t_philo *philo)
{
print(philo->data, philo->id, "is sleeping");
}
void print_thinking(t_philo *philo)
{
print(philo->data, philo->id, "is thinking");
}
void print_died(t_philo *philo)
{
t_data *data;
size_t time;
data = philo->data;
time = get_time();
pthread_mutex_lock(&data->print_mutex);
ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(philo->id + 1, 3);
ft_putchar(' ');
ft_putstr("died");
ft_putchar('\n');
pthread_mutex_unlock(&data->print_mutex);
}