-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodexion.h
More file actions
115 lines (102 loc) · 3.52 KB
/
Copy pathcodexion.h
File metadata and controls
115 lines (102 loc) · 3.52 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
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
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* codexion.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/02 16:06:14 by dporhomo #+# #+# */
/* Updated: 2026/06/15 13:15:24 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CODEXION_H
# define CODEXION_H
# include <unistd.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <sys/time.h>
# include <pthread.h>
# include <limits.h> /*llong_max, heap removal*/
/* Scheduler*/
typedef struct s_request
{
int coder_id;
long long req_time; /*time of request used in fifo*/
long long deadline; /*time to_burn_out used in edf*/
} t_request;
/* Queue*/
typedef struct s_pqueue
{
int size; /*dynamic value, num of coders waiting in pq*/
int capacity; /*max allocated memory for array; safety buffer*/
int is_edf;
t_request *heap; /*actual array of t_requests based on capacity*/
} t_pqueue;
/* Program*/
typedef struct s_program
{
int num_coders;
int t_burnout;
int t_compile;
int t_debug;
int t_refactor;
int req_compiles;
int cooldown;
int is_edf; /* 0 for FIFO, 1 for EDF*/
long long start_time;
int sim_active;
long long *dongle_ready_time;
pthread_mutex_t *dongles;
pthread_mutex_t print_lock;
pthread_mutex_t arbitrator;
pthread_mutex_t state_lock;
pthread_cond_t *coder_cond;
t_pqueue pq;
pthread_t monitor_thread;
} t_program;
/* Coders*/
typedef struct s_coder
{
int id;
int left_dongle;
int right_dongle;
int compiles_done;
long long last_compile;
pthread_t thread_id;
t_program *prog;
} t_coder;
/* Parser*/
int parse_input(int argc, char **argv, t_program *prog);
/* Utils*/
int pq_init(t_pqueue *pq, int capacity, int is_edf);
void pq_free(t_pqueue *pq);
void pq_swap(t_pqueue *pq, int i, int j);
int pq_compare(t_pqueue *pq, int i, int j);
int pq_push(t_pqueue *pq, t_request req);
int pq_remove_coder(t_pqueue *pq, int coder_id);
/* Time Mgt*/
long long get_current_time(void);
void ft_usleep(long long time_in_ms);
struct timespec ms_to_timespec(long long target_time_ms);
/* Threading and Lifecycle*/
int init_mutexes_and_coders(t_program *prog, t_coder **coders);
void clean_mutexes_and_coders(t_program *prog, t_coder *coders);
void print_status(t_coder *coder, char *status);
void *coder_routine(void *arg);
/* Operating and monitoring dongles*/
void wait_for_dongles(t_coder *coder);
void lock_dongles(t_coder *coder);
int shares_dongle(t_coder *me, int other_id);
int can_compile(t_coder *coder);
int grab_dongles(t_coder *coder);
void drop_dongles(t_coder *coder);
void *monitor_routine(void *arg);
/* State Lock Utils */
int read_sim_active(t_program *prog);
void set_sim_finished(t_program *prog);
long long read_last_compile(t_coder *coder);
void update_last_compile(t_coder *coder);
int read_compiles_done(t_coder *coder);
void increment_compiles_done(t_coder *coder);
#endif