tgrekov-philosophers
HIVE philosophers July 2024
Loading...
Searching...
No Matches
breakfast.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* breakfast.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/07/22 04:55:23 by tgrekov #+# #+# */
9/* Updated: 2024/08/06 17:03:07 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include "../utils/utils.h"
21#include "philo.h"
22
30static int eat(t_thread *thread, int fork2)
31{
32 int res;
33
34 pthread_mutex_lock(&thread->global->forks[thread->i]);
35 if (stop(thread)
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]);
41 res = 1;
42 if (!status(thread, "has taken a fork", 0) && !stop(thread))
43 {
44 thread->last_meal = timestamp();
45 res = status(thread, "is eating", 0);
46 if (!res)
47 {
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);
52 }
53 }
54 pthread_mutex_unlock(&thread->global->forks[thread->i]);
55 pthread_mutex_unlock(&thread->global->forks[fork2]);
56 return (res || thread->err);
57}
58
65void *breakfast(void *arg)
66{
67 t_thread *thread;
68
69 thread = (t_thread *) arg;
70 thread->last_meal = 0;
71 thread->times_ate = 0;
72 thread->err = 0;
73 if (thread->global->opt.eat_n)
74 pthread_mutex_lock(&thread->full);
75 if (status(thread, "is thinking", 0))
76 return (0);
77 if (thread->i % 2
78 && philo_sleep(thread->global->opt.tt_eat, thread))
79 return (0);
80 while (!stop(thread)
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);
85 if (thread->err)
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);
90 return (0);
91}
static int eat(t_thread *thread, int fork2)
Take forks, eat, release.
Definition breakfast.c:30
void * breakfast(void *arg)
Ponder, eat, sleep, repeat.
Definition breakfast.c:65
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.
Definition philo_sleep.c:33
int stop(t_thread *thread)
Determine if this thread's philo should die, or if the thread has reported an error.
Definition stop.c:30
int status(t_thread *thread, char *str, int ignore_end)
Thread safe philosopher state logging.
Definition status.c:46
unsigned long timestamp(void)
Millisecond timestamp since this function was first called.
Definition timestamp.c:27