60 lines
1.9 KiB
C
60 lines
1.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* utils_threads.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */
|
|
/* Updated: 2023/03/30 18:10:31 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "philo.h"
|
|
|
|
long long get_utime(void)
|
|
{
|
|
struct timeval time;
|
|
|
|
gettimeofday(&time, NULL);
|
|
return (time.tv_sec * 1000000 + time.tv_usec);
|
|
}
|
|
|
|
void message(long long time, int id, char *msg)
|
|
{
|
|
char send[1024];
|
|
int i;
|
|
|
|
i = ft_lltos(send, (get_utime() - time) / 1000);
|
|
send[i++] = ' ';
|
|
i += ft_lltos(send + i, id + 1);
|
|
send[i++] = ' ';
|
|
while (*msg)
|
|
send[i++] = *msg++;
|
|
write(1, send, i);
|
|
}
|
|
|
|
int verif_finish(t_philo *philo, t_only *only)
|
|
{
|
|
if (finish(philo, only->last_eat_time))
|
|
{
|
|
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
|
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
|
return (1);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int verif_die_eating(t_philo *philo, t_only *only, t_config *config)
|
|
{
|
|
if (get_utime() - only->last_eat_time + config->time_eat
|
|
>= config->time_die)
|
|
{
|
|
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
|
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
|
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
|
return (0);
|
|
}
|
|
return (1);
|
|
}
|