-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharbitrator.c
More file actions
60 lines (54 loc) · 2.16 KB
/
Copy patharbitrator.c
File metadata and controls
60 lines (54 loc) · 2.16 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arbitrator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/15 12:59:39 by dporhomo #+# #+# */
/* Updated: 2026/06/15 13:01:20 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
/* Checks if a compilation is possible */
#include "codexion.h"
/* Figures out if two coders are fighting for same dongle */
/* Determines if coders are next to each other or not */
int shares_dongle(t_coder *me, int other_id)
{
int o_left;
int o_right;
o_left = other_id - 1;
o_right = other_id % me->prog->num_coders;
if (o_left == me->left_dongle || o_left == me->right_dongle
|| o_right == me->left_dongle || o_right == me->right_dongle)
return (1);
return (0);
}
/* Checks are dongles ready */
/* Finds coder in pq, gets its idx */
/* Checks if dongles ready, if I am not checking myself,
if other coder has higher priority, if coders shares dongles
if all is not, can compile is true */
int can_compile(t_coder *coder)
{
long long now;
int my_idx;
int i;
now = get_current_time();
if (now < coder->prog->dongle_ready_time[coder->left_dongle]
|| now < coder->prog->dongle_ready_time[coder->right_dongle])
return (0);
my_idx = -1;
i = -1;
while (++i < coder->prog->pq.size)
if (coder->prog->pq.heap[i].coder_id == coder->id)
my_idx = i;
if (my_idx == -1)
return (0);
i = -1;
while (++i < coder->prog->pq.size)
if (i != my_idx && pq_compare(&coder->prog->pq, i, my_idx))
if (shares_dongle(coder, coder->prog->pq.heap[i].coder_id))
return (0);
return (1);
}