2023-04-13 09:00:39 -04:00
|
|
|
#include "philo.h"
|
|
|
|
#include <bits/pthreadtypes.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "./time.h"
|
2023-04-19 07:53:50 -04:00
|
|
|
#include "struct.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
|
|
|
{
|
|
|
|
static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
size_t time;
|
|
|
|
|
2023-04-19 07:53:50 -04:00
|
|
|
pthread_mutex_lock(&data->stop_mutex);
|
|
|
|
if (data->stop)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&data->stop_mutex);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&data->stop_mutex);
|
2023-04-13 09:00:39 -04:00
|
|
|
time = get_time();
|
|
|
|
pthread_mutex_lock(&print_mutex);
|
2023-04-19 07:53:50 -04:00
|
|
|
printf("%6zu %3zu %s\n", time, id, str);
|
2023-04-13 09:00:39 -04:00
|
|
|
pthread_mutex_unlock(&print_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void print_died(t_philo *philo)
|
|
|
|
{
|
2023-04-19 07:53:50 -04:00
|
|
|
print(philo->data, philo->id, "is died");
|
2023-04-13 09:00:39 -04:00
|
|
|
}
|