-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_3_routine_utils.c
87 lines (81 loc) · 2.75 KB
/
3_3_routine_utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 3_3_routine_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anhigo-s <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/28 16:56:58 by anhigo-s #+# #+# */
/* Updated: 2022/03/29 13:32:20 by anhigo-s ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
void fork_lock(t_thinker *list)
{
if (list->data->philo_alive == true)
{
pthread_mutex_lock(&list->fork);
pthread_mutex_lock(&list->data->printer_mutex);
if (list->data->philo_alive == true)
printf("%zu\t%s %d\n", \
(timenow() - list->time_start), FORK, list->index);
pthread_mutex_unlock(&list->data->printer_mutex);
pthread_mutex_lock(&list->prev->fork);
pthread_mutex_lock(&list->data->printer_mutex);
if (list->data->philo_alive == true)
printf("%zu\t%s %d\n", \
(timenow() - list->time_start), FORK, list->index);
pthread_mutex_unlock(&list->data->printer_mutex);
return ;
}
return ;
}
void lunchtime(t_thinker *list)
{
if (list->data->philo_alive == true)
{
pthread_mutex_lock(&list->eat_mutex);
list->last_meal = timenow();
list->nbr_snacks++;
pthread_mutex_unlock(&list->eat_mutex);
pthread_mutex_lock(&list->data->printer_mutex);
if (list->data->philo_alive == true)
printf("%zu\t%s %d\n", \
(timenow() - list->time_start), EAT, list->index);
pthread_mutex_unlock(&list->data->printer_mutex);
waiting(list->data->args.time_eat);
return ;
}
return ;
}
void fork_unlock(t_thinker *list)
{
pthread_mutex_unlock(&list->fork);
pthread_mutex_unlock(&list->prev->fork);
return ;
}
void naptime(t_thinker *list)
{
if (list->data->philo_alive == true)
{
pthread_mutex_lock(&list->data->printer_mutex);
if (list->data->philo_alive == true)
printf("%zu\t%s %d\n", \
(timenow() - list->time_start), SLEEP, list->index);
pthread_mutex_unlock(&list->data->printer_mutex);
waiting(list->data->args.time_sleep);
return ;
}
return ;
}
void thinker(t_thinker *list)
{
if (list->data->philo_alive == true)
{
pthread_mutex_lock(&list->data->printer_mutex);
printf("%zu\t%s %d\n", \
(timenow() - list->time_start), THINK, list->index);
pthread_mutex_unlock(&list->data->printer_mutex);
}
return ;
}