-
Notifications
You must be signed in to change notification settings - Fork 1
/
philos_exit.c
87 lines (77 loc) · 1.88 KB
/
philos_exit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philos_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmooney <kmooney@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 12:31:42 by kmooney #+# #+# */
/* Updated: 2023/08/16 15:50:25 by kmooney ### ########.fr */
/* */
/* ************************************************************************** */
#include "philos.h"
void ft_free_all(t_data *data)
{
//pthread_mutex_unlock(data->dead_mutex);
ft_destroy_forks(data);
if (data->philo)
ft_free_philo(data);
if (data->forks)
ft_free_forks(data);
free (data);
exit (0);
}
void ft_destroy_forks(t_data *data)
{
int i;
char c;
i = 0;
if (data->forks)
{
while (i < data->num_philo)
{
if (pthread_mutex_destroy(&(data->forks[i]->mutex)) != 0)
write(2, "Mutex Destroy fail\n", 19);
i++;
}
}
return ;
}
void ft_join_philos(t_data *data)
{
int i;
i = 0;
if (data->philo)
{
while(i < data->num_philo && data->philo[i])
{
if (pthread_join(*(data)->philo[i]->thread, NULL) != 0)
write(2, "pthread join fail", 17);
i++;
}
}
ft_free_all (data);
return ;
}
void ft_free_forks(t_data *data)
{
int i;
i= 0;
while (i < data->num_philo)
{
free (data->forks[i]);
i++;
}
free (data->forks);
}
void ft_free_philo(t_data *data)
{
int i;
i= 0;
while (i < data->num_philo)
{
free (data->philo[i]);
i++;
}
free (data->philo);
}