-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.c
More file actions
78 lines (68 loc) · 2.18 KB
/
Copy pathmonitor.c
File metadata and controls
78 lines (68 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* monitor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/08 18:55:00 by dporhomo #+# #+# */
/* Updated: 2026/06/14 22:39:04 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
/* Checks burnsout, required compile counts, end sleeping threads*/
#include "codexion.h"
static int check_all_full(t_program *prog, t_coder *coders)
{
int i;
i = -1;
while (++i < prog->num_coders)
if (read_compiles_done(&coders[i]) < prog->req_compiles)
return (0);
return (1);
}
static void trigger_end(t_program *prog)
{
int i;
set_sim_finished(prog);
pthread_mutex_lock(&prog->arbitrator);
i = -1;
while (++i < prog->num_coders)
pthread_cond_broadcast(&prog->coder_cond[i]);
pthread_mutex_unlock(&prog->arbitrator);
}
static int check_burnout(t_program *prog, t_coder *coders)
{
int i;
i = -1;
while (++i < prog->num_coders && read_sim_active(prog))
{
if (get_current_time() - read_last_compile(&coders[i])
>= prog->t_burnout)
{
pthread_mutex_lock(&prog->print_lock);
if (read_sim_active(prog))
printf("%lld %d burned out\n",
get_current_time() - prog->start_time, coders[i].id);
trigger_end(prog);
pthread_mutex_unlock(&prog->print_lock);
return (1);
}
}
return (0);
}
void *monitor_routine(void *arg)
{
t_coder *coders;
t_program *prog;
coders = (t_coder *)arg;
prog = coders[0].prog;
while (read_sim_active(prog))
{
if (check_burnout(prog, coders))
return (NULL);
if (check_all_full(prog, coders))
trigger_end(prog);
ft_usleep(1);
}
return (NULL);
}