/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* utils_threads.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */ /* Updated: 2023/04/01 12:08:51 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_die(t_philo *philo, char *msg) { char send[1024]; int i; i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); send[i++] = ' '; i += ft_lltos(send + i, philo->only->id + 1); send[i++] = ' '; while (*msg) send[i++] = *msg++; write(1, send, i); } void message(t_philo *philo, char *msg) { char send[1024]; int i; i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); send[i++] = ' '; i += ft_lltos(send + i, philo->only->id + 1); send[i++] = ' '; while (*msg) send[i++] = *msg++; if (!finish(philo, philo->only->last_eat_time)) 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); }