diff --git a/Makefile b/Makefile index d79873d..913c7fc 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ OBJS = $(SRCS:.c=.o) CC = cc # CFLAGS = -Wall -Wextra -Werror -pthread -g CFLAGS = -Wall -Wextra -Werror -pthread -g -fsanitize=thread +ARGS = 2 800 200 100 10 3 0 edf all: $(NAME) @@ -27,13 +28,13 @@ $(NAME): $(OBJS) # scheduler test: $(NAME) - ./$(NAME) 2 800 200 100 10 3 0 edf + ./$(NAME) $(ARGS) test-val: $(NAME) - valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) 2 800 200 100 10 5 0 edf + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) $(ARGS) test-hel: $(NAME) - valgrind --tool=helgrind ./$(NAME) 2 800 200 100 10 5 0 edf + valgrind --tool=helgrind ./$(NAME) $(ARGS) clean: rm -f $(OBJS) diff --git a/coders/codexion.h b/coders/codexion.h index c5d189a..9d29c1a 100644 --- a/coders/codexion.h +++ b/coders/codexion.h @@ -49,7 +49,7 @@ typedef struct s_coder long last_compile; int compiles_done; t_state *state; - pthread_mutex_t last_mutex; + pthread_mutex_t info_mutex; } t_coder; typedef struct s_state diff --git a/coders/main.c b/coders/main.c index 6816034..5502b84 100644 --- a/coders/main.c +++ b/coders/main.c @@ -64,14 +64,14 @@ void compile_op(t_coder *coder, t_state *state) pthread_mutex_lock(&second->lock); second->last_used = now(); print("%ld %d has taken a dongle\n", state, coder->idx); - pthread_mutex_lock(&coder->last_mutex); + pthread_mutex_lock(&coder->info_mutex); coder->last_compile = now(); - pthread_mutex_unlock(&coder->last_mutex); + coder->compiles_done += 1; + pthread_mutex_unlock(&coder->info_mutex); print("%ld %d is compiling\n", state, coder->idx); usleep(state->args.time_to_compile * 1000L); pthread_mutex_unlock(&first->lock); pthread_mutex_unlock(&second->lock); - coder->compiles_done += 1; } void debug_op(t_coder *coder, t_state *state) @@ -116,17 +116,20 @@ void *monitor(void *param) t_state *state; int i; long last; + int compiles_done; state = param; - while (!is_over(state)) + compiles_done = 0; + while (compiles_done < state->args.number_of_compiles_required + && !is_over(state)) { usleep(1000); i = -1; while (++i < state->args.number_of_coders) { - pthread_mutex_lock(&state->coders[i].last_mutex); + pthread_mutex_lock(&state->coders[i].info_mutex); last = state->coders[i].last_compile; - pthread_mutex_unlock(&state->coders[i].last_mutex); + pthread_mutex_unlock(&state->coders[i].info_mutex); if (now() - last >= state->args.time_to_burnout) { pthread_mutex_lock(&state->over_mutex); @@ -136,6 +139,9 @@ void *monitor(void *param) return (NULL); } } + pthread_mutex_lock(&state->coders[i - 1].info_mutex); + compiles_done = state->coders[i - 1].compiles_done; + pthread_mutex_unlock(&state->coders[i - 1].info_mutex); } return (NULL); } @@ -199,7 +205,7 @@ t_coder *init_coders(t_args *args, t_state *state) coders[i].right_dongle = &state->dongles[(i + 1) % args->number_of_coders]; coders[i].state = state; - pthread_mutex_init(&coders[i].last_mutex, NULL); + pthread_mutex_init(&coders[i].info_mutex, NULL); } return (coders); }