Aethor 4 years ago
parent
commit
7f160f69f8
  1. 2
      Building/Building.c
  2. 57
      Elevator/Elevator.c
  3. 4
      ElevatorBreaker/ElevatorBreaker.c
  4. 10
      SharedData/SharedData.c

2
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);

57
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)){
building->signal_elevator_at_floor(building, this->get_floor(this));
}
temp_element = temp_element->next;
}
pthread_mutex_unlock(&this->mutex_passengers);
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)){
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;
}
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));
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(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;
}

4
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;
}
}

10
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; i<ELEVATOR_NB; i++) {
this->start_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);
}

Loading…
Cancel
Save