20#include "../utils/utils.h"
34 pthread_mutex_lock(&thread->global->forks[thread->i]);
36 ||
status(thread,
"has taken a fork", 0)
37 || (fork2 == thread->i
38 &&
philo_sleep(thread->global->opt.tt_die, thread)))
39 return (pthread_mutex_unlock(&thread->global->forks[thread->i]) + 1);
40 pthread_mutex_lock(&thread->global->forks[fork2]);
42 if (!
status(thread,
"has taken a fork", 0) && !
stop(thread))
45 res =
status(thread,
"is eating", 0);
48 res =
philo_sleep(thread->global->opt.tt_eat, thread);
49 if (++thread->times_ate == thread->global->opt.eat_n
50 && thread->global->opt.eat_n)
51 pthread_mutex_unlock(&thread->full);
54 pthread_mutex_unlock(&thread->global->forks[thread->i]);
55 pthread_mutex_unlock(&thread->global->forks[fork2]);
56 return (res || thread->err);
70 thread->last_meal = 0;
71 thread->times_ate = 0;
73 if (thread->global->opt.eat_n)
74 pthread_mutex_lock(&thread->full);
75 if (
status(thread,
"is thinking", 0))
81 && !
eat(thread, wrap_ix(thread->i + 1, thread->global->opt.n))
82 && !
status(thread,
"is sleeping", 0)
83 && !
philo_sleep(thread->global->opt.tt_sleep, thread))
84 status(thread,
"is thinking", 0);
86 set_end(thread->global);
87 if (thread->global->opt.eat_n
88 && thread->times_ate < thread->global->opt.eat_n)
89 pthread_mutex_unlock(&thread->full);
static int eat(t_thread *thread, int fork2)
Take forks, eat, release.
void * breakfast(void *arg)
Ponder, eat, sleep, repeat.
int philo_sleep(unsigned long ms, t_thread *thread)
Sleep in 200 microsecond intervals until ms millis have elapsed, or stop has returned non-zero.
int stop(t_thread *thread)
Determine if this thread's philo should die, or if the thread has reported an error.
int status(t_thread *thread, char *str, int ignore_end)
Thread safe philosopher state logging.
unsigned long timestamp(void)
Millisecond timestamp since this function was first called.