42_Philosopher/philo/print.c

57 lines
1.7 KiB
C
Raw Normal View History

2023-04-27 08:05:14 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
2023-05-17 07:25:34 -04:00
/* Updated: 2023/05/17 13:22:19 by cchauvet ### ########.fr */
2023-04-27 08:05:14 -04:00
/* */
/* ************************************************************************** */
2023-04-13 09:00:39 -04:00
#include "philo.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>
2023-04-25 10:30:21 -04:00
#include <stdbool.h>
2023-04-13 09:00:39 -04:00
#include <stddef.h>
#include "./time.h"
2023-04-25 10:30:21 -04:00
#include "./utils/utils.h"
2023-04-13 09:00:39 -04:00
2023-04-19 07:53:50 -04:00
static void print(t_data *data, size_t id, char *str)
2023-04-13 09:00:39 -04:00
{
size_t time;
2023-04-25 10:30:21 -04:00
bool stop;
2023-04-13 09:00:39 -04:00
2023-04-25 10:30:21 -04:00
pthread_mutex_lock(&data->print_mutex);
2023-04-19 07:53:50 -04:00
pthread_mutex_lock(&data->stop_mutex);
2023-04-25 10:30:21 -04:00
stop = data->stop;
pthread_mutex_unlock(&data->stop_mutex);
if (stop == false)
2023-04-19 07:53:50 -04:00
{
2023-04-25 11:01:54 -04:00
time = get_time();
2023-05-17 07:25:34 -04:00
printf("%zu %zu %s\n", time, id, str);
2023-04-19 07:53:50 -04:00
}
pthread_mutex_unlock(&data->print_mutex);
2023-04-13 09:00:39 -04:00
}
void print_take_a_fork(t_philo *philo)
{
2023-04-19 07:53:50 -04:00
print(philo->data, philo->id, "has taken a fork");
2023-04-13 09:00:39 -04:00
}
void print_eating(t_philo *philo)
{
2023-04-19 07:53:50 -04:00
print(philo->data, philo->id, "is eating");
2023-04-13 09:00:39 -04:00
}
void print_sleeping(t_philo *philo)
{
2023-04-19 07:53:50 -04:00
print(philo->data, philo->id, "is sleeping");
2023-04-13 09:00:39 -04:00
}
void print_thinking(t_philo *philo)
{
2023-04-19 07:53:50 -04:00
print(philo->data, philo->id, "is thinking");
2023-04-13 09:00:39 -04:00
}