diff --git a/display.c b/display.c index 319df2e181c7ce4b5f511d68e101f88149c48cc6..d765e23249e442116f81d9e5916850c6000fe61e 100644 --- a/display.c +++ b/display.c @@ -56,7 +56,7 @@ void *display_func(void *param){ // printf("\ndisplay_func"); machine* mas = (machine *) (param); - while(1){ + while(mas->wheels[0].end){ SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect); diff --git a/game.c b/game.c index 98a085817c9e6b6a6be62ee9fb469729dd7a860a..448c3f0a27d3143f703341af85ad43f081a0b5a7 100644 --- a/game.c +++ b/game.c @@ -2,10 +2,13 @@ #include "game.h" -void new_machine(machine* mas, int nb_wheels,int player, int machine){ + +void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end){ for (int i = 0; i < nb_wheels; i++) { - mas->wheels[i] = create_wheel(96,0); + mas->player_wallet = player; mas->machine_wallet = machine; + sem_init(&mas->semaphore,0,0); + mas->wheels[i] = create_wheel(96,0,&mas->semaphore,end); } } diff --git a/game.h b/game.h index 17f4ed92cc1b570acc7a8260b05b470161531213..1ae9a0c97ce237b4cf819d311bdb7e9741e277c9 100644 --- a/game.h +++ b/game.h @@ -7,12 +7,14 @@ #include "wheel.h" + typedef struct machine_a_sous{ wheel wheels[WHEEL_NB]; int player_wallet; int machine_wallet; + sem_t semaphore; } machine; -void new_machine(machine* mas, int nb_wheels,int player, int machine); +void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end); #endif diff --git a/one_armed_bandit.c b/one_armed_bandit.c index 1df2646208da3c1999dec89444c73e1c10d5e86b..a66b782597663b628c29e4ab3e93baa0aa059fe5 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -1,21 +1,18 @@ -/** -* One Armed Bandit -* 24.05.21 -* Authors : Sitbon benjamin, Beck Léonard -**/ +// Template of one-armed bandit lab +// KB main, thread handling + -// Keyboard thread and game initialization #include <stdio.h> -#include <stdbool.h> + #include <assert.h> #include <unistd.h> #include <pthread.h> -#include <semaphore.h> #include "utilities.h" #include "display.h" -#include "threads.h" +#include "game.h" #include "one_armed_bandit.h" +#include "threads.h" @@ -35,21 +32,22 @@ int main() { int wheel_turn = 4; bool paid = false; + bool quit=false; pthread_t threads[4]; machine mas; - new_machine(&mas, 3,10,30); - + new_machine(&mas, 3,10,30,&quit); + for(int i = 0; i<3; i++){ run_thread(&threads[i],wheel_func,&mas.wheels[i]); } - + run_thread(&threads[3],display_func,&mas); - + SDL_Event event; - bool quit=false; + do { SDL_WaitEvent(&event); // passive waiting on an event switch (event.type) @@ -67,9 +65,6 @@ int main() switch(event.key.keysym.sym) { case SDLK_ESCAPE: - for(int i =0; i<4; i++){ - pthread_kill(threads[i]); - } quit=true; break; case SDLK_SPACE: @@ -100,6 +95,7 @@ int main() wheel_turn =0; for(int i = 0; i<WHEEL_NB; i++){ mas.wheels[i].speed = WHEEL_NB-i; + sem_post(&mas.semaphore); } } break; diff --git a/threads.c b/threads.c index 68f86c4f8ea2bee45b112f2a489874583b735726..2b0ad45305270983858045e0dba949b4f83b333f 100644 --- a/threads.c +++ b/threads.c @@ -1,7 +1,5 @@ // Small library to ease the use of pthread -// TODO: MALLOOOOC - #define __GNU_SOURCE #include <stdio.h> @@ -40,7 +38,8 @@ int run_thread_and_wait (void* (*fn )(void*), void *arg){ } int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt){ - pthread_t *t = malloc(t_cnt*sizeof(pthread_t)); + pthread_t* t = malloc(t_cnt*sizeof(pthread_t)); + if (!t) return -1; for(int i = 0; i<t_cnt; i++){ int err = run_thread(&t[i], fn, (*arg) + size_of_arg_type*i); @@ -58,6 +57,7 @@ int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt){ pthread_t *t = malloc(t_cnt*sizeof(pthread_t)); + if (!t) return -1; for(int i = 0; i<t_cnt; i++){ int err = run_thread(&t[i], fn, arg); @@ -71,4 +71,4 @@ int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt) free(t); return 0; -} +} \ No newline at end of file diff --git a/wheel.c b/wheel.c index e995d2d4d317dd99898f0f572963b3288c363f18..4b41e0a8388ca248d42f049c4342bea612e39dc6 100644 --- a/wheel.c +++ b/wheel.c @@ -4,33 +4,40 @@ #include "wheel.h" -wheel create_wheel(int pixel,int speed){ +wheel create_wheel(int pixel,int speed,sem_t* sem,bool* ending){ wheel ret; ret.current_px = pixel; ret.speed = speed; - pthread_mutex_init(&ret.lock,NULL); + ret.semaphore = sem; + ret.end = ending; + pthread_mutex_init(&ret.lock,NULL); return ret; } void *wheel_func(void *param){ // printf("\nwheel_func"); - wheel* id = (wheel *) (param); + wheel* this = (wheel *) (param); - while(1){ - wheel_spin_1_tick(id); - wait_until(wheel_delay_ms(*id)); + while(this->end){ + wheel_spin_1_tick(this); + wait_until(wheel_delay_ms(*this)); } } + int wheel_spin_1_tick(wheel* this){ if (this->speed) { pthread_mutex_lock(&this->lock); this->current_px = (this->current_px + 2) % 896 ; pthread_mutex_unlock(&this->lock); } + else{ + sem_wait(this->semaphore); + } } + int wheel_delay_ms(wheel this){ if (this.speed) { return this.speed * 2; diff --git a/wheel.h b/wheel.h index 467bf2ed15a2c5ffaa84e6adbd332887a1124588..2e5bd2a95bda987d8810195118e7d2ed5092e40c 100644 --- a/wheel.h +++ b/wheel.h @@ -2,30 +2,38 @@ #define WHEEL_H #include <pthread.h> +#include <stdbool.h> +#include <semaphore.h> typedef struct roue{ + // speed is either 1 (the fastest), 2, 3 (the slowest) or 0 (stopped) int speed; - // speed is either 1 (the fastest), 2, 3 (the slowest) or 0 (stopped) + // stores the position of the wheel, used to determine what item is visible int current_px; - // stores the position of the wheel, used to determine what item is visible + // used to synchronize cache with memory so display is always up to date pthread_mutex_t lock; - // used to synchronize cache with memory so display is always up to date + + sem_t* semaphore; + + bool* end; + } wheel; -wheel create_wheel(int pixel,int speed); +wheel create_wheel(int pixel,int speed,sem_t* sem,bool* ending); +//function run by each wheel thread, turns the wheel void *wheel_func(void *param); - // function run by each wheel thread, turns the wheel - + +// spins the wheel int wheel_spin_1_tick(wheel* this); - // spins the wheel - + +// calculates the delay based on the speed int wheel_delay_ms(wheel this); - // calculates the delay based on the speed - + +// stops the wheel on the right pixel to have a centered item int stop_wheel(wheel* this); - // stops the wheel on the right pixel to have a centered item + #endif