-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle.c
64 lines (59 loc) · 1.9 KB
/
lifecycle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lifecycle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ykoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/15 05:46:37 by ykoh #+# #+# */
/* Updated: 2021/03/15 05:46:38 by ykoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
static int dead(t_philo *p)
{
pthread_mutex_unlock(&(p->left->mutex));
pthread_mutex_unlock(&(p->right->mutex));
return (DEAD);
}
static int eating(t_philo *p)
{
pthread_mutex_lock(&(p->left->mutex));
p->left->status = 1;
if (print_status(p, TAKING) == DEAD)
return (dead(p));
pthread_mutex_lock(&(p->right->mutex));
p->right->status = 1;
if (print_status(p, TAKING) == DEAD)
return (dead(p));
p->eat_time = get_time();
if (print_status(p, EATING) == DEAD)
return (dead(p));
p->cnt++;
ft_msleep(g_table.time_to_eat);
p->left->status = 0;
p->right->status = 0;
pthread_mutex_unlock(&(p->right->mutex));
pthread_mutex_unlock(&(p->left->mutex));
return (0);
}
void *lifecycle(void *arg)
{
t_philo *p;
p = arg;
if (p->index % 2)
ft_msleep(DELAY);
while (1)
{
if (g_table.end)
return (0);
if (eating(p) == DEAD)
return (0);
if (print_status(p, SLEEPING) == DEAD)
return (0);
ft_msleep(g_table.time_to_sleep);
if (print_status(p, THINKING) == DEAD)
return (0);
}
return (0);
}