-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.c
More file actions
86 lines (78 loc) · 2.65 KB
/
monitor.c
File metadata and controls
86 lines (78 loc) · 2.65 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* monitor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kacherch <kacherch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/15 14:21:10 by kacherch #+# #+# */
/* Updated: 2026/05/08 15:18:49 by kacherch ### ########.fr */
/* */
/* ************************************************************************** */
#include "coders.h"
static int check_timeout(t_monitor *monitor, int i)
{
pthread_mutex_lock(monitor->coders[i].last_compiled_mutex);
if (monitor->coders[i].nb_compile == monitor->config->nb_compile_required)
{
pthread_mutex_unlock(monitor->coders[i].last_compiled_mutex);
return (0);
}
if (get_time() > monitor->coders[i].last_compiled
+ monitor->config->time_to_burnout)
{
pthread_mutex_lock(monitor->coders[i].config->mutex_output);
printf("%ld %d burned out\n", get_time()
- monitor->config->begin_timestamp, monitor->coders[i].id);
set_exit(monitor->config);
pthread_mutex_unlock(monitor->coders[i].config->mutex_output);
pthread_mutex_unlock(monitor->coders[i].last_compiled_mutex);
return (1);
}
pthread_mutex_unlock(monitor->coders[i].last_compiled_mutex);
return (0);
}
static void *monitor_routine(void *data)
{
t_monitor *monitor;
int i;
monitor = (t_monitor *)data;
while (1 && check_exit(monitor->config) == 0
&& !total_comp_reached(monitor->config))
{
i = 0;
while (i < monitor->config->nb_coders)
{
if (check_timeout(monitor, i) == 1)
return (NULL);
i++;
}
}
return (NULL);
}
static t_monitor *init_monitor(t_coder *coders, t_config *config)
{
t_monitor *monitor;
monitor = ft_calloc(1, sizeof(t_monitor));
if (!monitor)
return (NULL);
monitor->coders = coders;
monitor->config = config;
return (monitor);
}
int launch_monitor(t_coder *coders, t_config *config)
{
t_monitor *monitor;
pthread_t *monitor_thread;
monitor_thread = ft_calloc(1, sizeof(pthread_t));
if (!monitor_thread)
return (-1);
monitor = init_monitor(coders, config);
if (!monitor)
return (-1);
pthread_create(monitor_thread, NULL, monitor_routine, monitor);
pthread_join(*monitor_thread, NULL);
free(monitor);
free(monitor_thread);
return (0);
}