-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
84 lines (78 loc) · 2.58 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyandriy <lyandriy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/16 18:52:29 by lyandriy #+# #+# */
/* Updated: 2023/10/27 20:10:33 by lyandriy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int check_return(int retur)
{
if (retur)
{
printf("error\n");
return (0);
}
return (1);
}
void ft_destroy(t_philo *philo)
{
int i;
i = 0;
if (pthread_mutex_destroy(&philo->common_structure.mutex))
printf("error\n");
if (pthread_mutex_destroy(&philo->common_structure.mutex_print))
printf("error\n");
if (pthread_mutex_destroy(&philo->common_structure.eat))
printf("error\n");
while (i < philo->num_ph)
pthread_mutex_destroy(&philo->needle[i++]->mutex);
i = 0;
if (philo->needle)
{
while (i < philo->count_philo)
{
if (philo->needle[i])
free(philo->needle[i]);
i++;
}
free(philo->needle);
}
}
void check_thred(t_philo *philo)
{
while (1)
{
pthread_mutex_lock(&philo->common_structure.mutex);
if (philo->common_structure.death_sign == 1)
{
pthread_mutex_lock(&philo->common_structure.mutex_print);
if (!philo->common_structure.print_sign)
philo->common_structure.print_sign = 1;
printf(DO, philo->common_structure.time_die,
(philo->common_structure.numb_philo + 1), "died");
pthread_mutex_unlock(&philo->common_structure.mutex_print);
pthread_mutex_unlock(&philo->common_structure.mutex);
break ;
}
pthread_mutex_lock(&philo->common_structure.eat);
if (philo->common_structure.philosopher_eat == philo->num_ph)
{
pthread_mutex_unlock(&philo->common_structure.eat);
pthread_mutex_unlock(&philo->common_structure.mutex);
break ;
}
pthread_mutex_unlock(&philo->common_structure.eat);
pthread_mutex_unlock(&philo->common_structure.mutex);
}
}
void can_start(t_philo *philo)
{
pthread_mutex_lock(&philo->common_structure.mutex);
philo->common_structure.thred_sign = 1;
pthread_mutex_unlock(&philo->common_structure.mutex);
}