From 7f160f69f8d02bfb2214ba4df6e07d7c48b20888 Mon Sep 17 00:00:00 2001 From: Aethor Date: Fri, 22 Jun 2018 05:49:15 +0200 Subject: [PATCH] ~ --- Building/Building.c | 2 +- Elevator/Elevator.c | 63 +++++++++++++++++-------------- ElevatorBreaker/ElevatorBreaker.c | 4 +- SharedData/SharedData.c | 10 +++-- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 239006c..0827815 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -243,7 +243,7 @@ void signal_elevator_at_floor_Building(THIS(Building), int floor){ void ask_elevator_reparation_Building(THIS(Building), Elevator* elevator){ printf("Technicien : l'ascenseur %s attend pour se faire réparer\n", elevator->name); pthread_mutex_lock(&this->mutex_repair); - usleep(10000); + usleep(500000); elevator->repair(elevator); pthread_mutex_unlock(&this->mutex_repair); printf("Technicien : l'ascenseur %s est maintenant réparé\n", elevator->name); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index b202dcf..53e6f21 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -106,7 +106,7 @@ int can_get_inside_Elevator(THIS(Elevator), int floor){ pthread_mutex_lock(&this->mutex_floor); permission = (this->passengers->get_size(this->passengers) < MAX_ELEVATOR_CAPACITY && - /*this->state == WAITING &&*/ this->floor == floor); + this->state == RUNNING && this->floor == floor); //if(this->floor != floor) //printf("DEBUG : Cause de la préemption : l'ascenseur %s n'est pas là !\n", this->name); @@ -140,37 +140,44 @@ void *runnable_Elevator(void * void_this){ printf("Ascenseur %s : Initialisation...\n", this->name); while (data->is_active_passengers_left(data)){ usleep(250000); - if(this->target_floor == this->get_floor(this)){ + if(this->get_state(this) == RUNNING){ + if(this->target_floor == this->get_floor(this)){ - pthread_mutex_lock(&this->mutex_passengers);//on débarque les passagers - Element* temp_element = this->passengers->head; - while(temp_element != NULL){ - Passenger* temp_passenger = ((Passenger*) temp_element->data); - if(temp_passenger->get_destination(temp_passenger) == this->get_floor(this)){ + pthread_mutex_lock(&this->mutex_passengers);//on débarque les passagers + Element* temp_element = this->passengers->head; + while(temp_element != NULL){ + Passenger* temp_passenger = ((Passenger*) temp_element->data); + if(temp_passenger->get_destination(temp_passenger) == this->get_floor(this)){ + building->signal_elevator_at_floor(building, this->get_floor(this)); + } + temp_element = temp_element->next; + } + pthread_mutex_unlock(&this->mutex_passengers); + + for(i=0;iwaiting_floors[this->get_floor(this)];i++){ //on embarque les passagers building->signal_elevator_at_floor(building, this->get_floor(this)); } - temp_element = temp_element->next; + + next_passenger_stop = this->get_next_passenger_stop(this); + next_call = building->get_next_call(building, this->get_floor(this)); + + if(next_passenger_stop != -1){ + this->target_floor = next_passenger_stop; + }else if(next_call != -1){ + this->target_floor = next_call; + } + } - pthread_mutex_unlock(&this->mutex_passengers); - - for(i=0;iwaiting_floors[this->get_floor(this)];i++){ //on embarque les passagers - building->signal_elevator_at_floor(building, this->get_floor(this)); - } - - next_passenger_stop = this->get_next_passenger_stop(this); - next_call = building->get_next_call(building, this->get_floor(this)); - - if(next_passenger_stop != -1){ - this->target_floor = next_passenger_stop; - }else if(next_call != -1){ - this->target_floor = next_call; - } - - } - if(this->get_floor(this) != this->target_floor){ - this->set_floor(this, this->target_floor); - printf("Ascenseur %s : Je suis en route vers l'étage %d\n", this->name, this->target_floor); + if(this->get_floor(this) != this->target_floor){ + this->set_floor(this, this->target_floor); + printf("Ascenseur %s : Je suis en route vers l'étage %d\n", this->name, this->target_floor); + } + }else if(this->get_state(this) == BROKEN){ + building->ask_elevator_reparation(building, this); + }else{ + //CRASH("ERROR : Undefined behaviour\n"); } + } data->unregister_thread(data, this->thread_number); @@ -205,7 +212,7 @@ Elevator *_init_Elevator(char * name){ ); new_elevator->set_floor(new_elevator, 0); - new_elevator->set_state(new_elevator, WAITING); + new_elevator->set_state(new_elevator, RUNNING); return new_elevator; } diff --git a/ElevatorBreaker/ElevatorBreaker.c b/ElevatorBreaker/ElevatorBreaker.c index 97013c0..c0f3cd4 100644 --- a/ElevatorBreaker/ElevatorBreaker.c +++ b/ElevatorBreaker/ElevatorBreaker.c @@ -20,7 +20,7 @@ void * runnable_ElevatorBreaker(void * void_this){ AGENT_OPTIONS - while (data->is_active_passengers_left(data)){ + while (data->is_active_passengers_left(data)){ usleep(900000); // One chance out of two to break something if ((rand() % 101) > 50) { @@ -44,4 +44,4 @@ ElevatorBreaker *_init_ElevatorBreaker(){ ) return new_elevator_breaker; -} \ No newline at end of file +} diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index ee90945..0e64211 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -47,10 +47,7 @@ void start_all_threads_SharedData(THIS(SharedData)){ pthread_mutex_lock(&this->mutex_active_passengers); pthread_mutex_lock(&this->mutex_threads); - /* starting the elevator breaker */ - this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0); - this->elevator_breaker->set_thread_number(this->elevator_breaker, 0); - + /* starts threading elevators */ for (i=1; istart_thread(this, this->main_building->elevators[i]->runnable, @@ -77,6 +74,11 @@ void start_all_threads_SharedData(THIS(SharedData)){ this->active_passengers++; i++; } + + /* starting the elevator breaker */ + this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0); + this->elevator_breaker->set_thread_number(this->elevator_breaker, 0); + pthread_mutex_unlock(&this->mutex_threads); pthread_mutex_unlock(&this->mutex_active_passengers); }