/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* philo_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud@student.42nice.fr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/18 19:10:41 by narnaud@stude #+# #+# */ /* Updated: 2022/05/19 00:50:56 by narnaud@stude ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" int is_running(t_room *room) { int ret; pthread_mutex_lock(room->lock); ret = room->running; pthread_mutex_unlock(room->lock); return (ret); } int safe_print(t_philo *philo, char *str) { int ret; ret = is_running(philo->room); if (!ret) return (0); pthread_mutex_lock(philo->room->lock); printf(str, room_clock(), philo->id); pthread_mutex_unlock(philo->room->lock); return (ret); } int safe_wait(t_room *room, int start, int duration) { int ret; ret = is_running(room); while (ret && (room_clock() < (start + room->param[duration]))) { ret = is_running(room); usleep(50); } return (ret); } int room_clock(void) { struct timeval time; int ret; static long int start_time = 0; gettimeofday(&time, NULL); if (!start_time) start_time = time.tv_sec * 1000 + time.tv_usec / 1000; ret = (int)(time.tv_sec * 1000 + time.tv_usec / 1000 - start_time); return (ret); } int mini_atoi(char *nbr) { int ret; ret = 0; while (*nbr >= '0' && *nbr <= '9') { ret = (*nbr - '0') + (10 * ret); nbr++; } return (ret); }