1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2025-07-22 09:29:57 +00:00
This commit is contained in:
Aethor
2018-06-22 05:49:15 +02:00
parent 3334f77d22
commit 7f160f69f8
4 changed files with 44 additions and 35 deletions

View File

@@ -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;i<building->waiting_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;i<building->waiting_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;
}