-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.c
112 lines (107 loc) · 2.7 KB
/
handlers.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handlers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stigkas <stigkas@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 17:42:48 by stigkas #+# #+# */
/* Updated: 2024/04/16 09:42:04 by stigkas ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/philo.h"
int mtx_error(int mtx_err, t_mtx_action act)
{
if (mtx_err != 0 && act == INIT)
{
printf("The values specified by attr are not valid\n");
return (0);
}
else if (mtx_err != 0 && act == DESTROY)
{
printf("Mutex could not be destroyed\n");
return (0);
}
else if (mtx_err != 0 && act == LOCK)
{
printf("Mutex could not be locked\n");
return (0);
}
else if (mtx_err != 0 && act == UNLOCK)
{
printf("Mutex could not be unlocked\n");
return (0);
}
return (1);
}
int mtx_handler(pthread_mutex_t *mtx, t_mtx_action act)
{
if (act == INIT)
{
if (!mtx_error(pthread_mutex_init(mtx, NULL), INIT))
return (0);
}
else if (act == DESTROY)
{
if (!mtx_error(pthread_mutex_destroy(mtx), DESTROY))
return (0);
}
else if (act == LOCK)
{
if (!mtx_error(pthread_mutex_lock(mtx), LOCK))
return (0);
}
else if (act == UNLOCK)
{
if (pthread_mutex_unlock(mtx) != 0)
return (0);
}
else
return (0);
return (1);
}
int th_error(int thread_err, t_thread_action act)
{
if (thread_err != 0 && act == CREATE)
{
printf("Thread could not be created\n");
return (0);
}
else if (thread_err != 0 && act == JOIN)
{
printf("Thread could not be joined\n");
return (0);
}
else if (thread_err != 0 && act == DETACH)
{
printf("Thread could not be detached\n");
return (0);
}
return (1);
}
int thread_handler(pthread_t *thread, void *(*routine)(void *),
void *data, t_thread_action act)
{
if (act == CREATE)
{
if (!th_error(pthread_create(thread, NULL, routine, data), CREATE))
return (0);
}
else if (act == JOIN)
{
if (!th_error(pthread_join(*thread, NULL), JOIN))
return (0);
}
else if (act == DETACH)
{
if (!th_error(pthread_detach(*thread), DETACH))
return (0);
}
else
{
printf("Wrong thread action"
"Use CREATE, JOIN or DETACH\n");
return (0);
}
return (1);
}