diff --git a/Building/Building.c b/Building/Building.c index 7e98307..d0bb8b0 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -135,7 +135,6 @@ void _free__Building(THIS(Building)){ this->residents->clear_custom(this->residents, free_resident); this->visitors->clear_custom(this->visitors, free_visitor); - pthread_mutex_unlock(this->mutex_cond_get_inside_elevator); pthread_mutex_destroy(this->mutex_cond_get_inside_elevator); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 4d6f9be..a3fe828 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -135,7 +135,8 @@ void *runnable_Elevator(void * void_this){ AGENT_OPTIONS printf("Initialisation de l'ascenseur %s\n", this->name); - for (;;){ + while (data->is_active_passengers_left(data)){ +// for (;;){ usleep(250000); if(this->target_floor == this->get_floor(this)){ diff --git a/Passenger/Passenger.h b/Passenger/Passenger.h index b29f0d0..6c2c59c 100644 --- a/Passenger/Passenger.h +++ b/Passenger/Passenger.h @@ -12,8 +12,8 @@ typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE; typedef struct o_Passenger { - PASSENGER_TYPE type; - union { + PUBLIC PASSENGER_TYPE type; + PUBLIC union { Resident * resident; Visitor * visitor; }; diff --git a/Resident/Resident.c b/Resident/Resident.c index 4b8d2d7..ffa4598 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -29,6 +29,7 @@ void * runnable_Resident(void * void_this){ printf("RĂ©sident %s : je reste chez moi\n", this->name); else data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); + data->decrement_active_passengers(data); return NULL; } diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index 4451fb2..9ab0a8e 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -41,6 +41,7 @@ void start_all_threads_SharedData(THIS(SharedData)){ this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB; this->threads = malloc_or_die(sizeof(pthread_t) * this->threads_nb); + pthread_mutex_lock(&this->mutex_active_passengers); /* starts threading elevators */ for (i=0; istart_thread(this, this->main_building->elevators[i]->runnable, @@ -51,6 +52,7 @@ void start_all_threads_SharedData(THIS(SharedData)){ while (current != NULL){ this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i); current = current->get_next(current); + this->active_passengers++; i++; } @@ -59,8 +61,10 @@ void start_all_threads_SharedData(THIS(SharedData)){ while (current != NULL){ this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i); current = current->get_next(current); + this->active_passengers++; i++; } + pthread_mutex_unlock(&this->mutex_active_passengers); } } } @@ -87,9 +91,26 @@ void _free__SharedData(THIS(SharedData)){ if (this->main_building != NULL) DELETE(this->main_building); + pthread_mutex_unlock(&this->mutex_active_passengers); + pthread_mutex_destroy(&this->mutex_active_passengers); + free(this); } +int is_active_passengers_left_SharedData(THIS(SharedData)){ + int response; + pthread_mutex_lock(&this->mutex_active_passengers); + response = (this->active_passengers > 0); + pthread_mutex_unlock(&this->mutex_active_passengers); + return response; +} + +void decrement_active_passengers_SharedData(THIS(SharedData)){ + pthread_mutex_lock(&this->mutex_active_passengers); + this->active_passengers--; + pthread_mutex_unlock(&this->mutex_active_passengers); +} + SharedData *_get_instance_SharedData(){ static SharedData * new_shared_data = NULL; if (new_shared_data == NULL){ @@ -98,6 +119,9 @@ SharedData *_get_instance_SharedData(){ new_shared_data->threads = NULL; new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */ new_shared_data->threads_nb = 0; + new_shared_data->active_passengers = 0; + + pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL); LINK_ALL(SharedData, new_shared_data, start_thread, @@ -106,7 +130,9 @@ SharedData *_get_instance_SharedData(){ set_main_building, get_main_building, use_call_box, - call_elevator + call_elevator, + decrement_active_passengers, + is_active_passengers_left ); } return new_shared_data; diff --git a/SharedData/SharedData.h b/SharedData/SharedData.h index 037b44f..18de379 100644 --- a/SharedData/SharedData.h +++ b/SharedData/SharedData.h @@ -11,8 +11,10 @@ typedef struct o_SharedData { PRIVATE int threads_nb; + PRIVATE int active_passengers; PRIVATE pthread_t *threads; PRIVATE Building * main_building; + PRIVATE pthread_mutex_t mutex_active_passengers; PRIVATE void (*start_thread)(_THIS(SharedData), void * (*thread)(void *), void * data, int thread_number); @@ -22,6 +24,8 @@ typedef struct o_SharedData { PUBLIC Building * (*get_main_building)(_THIS(SharedData)); PUBLIC int (*call_elevator)(_THIS(SharedData), int starting_floor, int destination_floor); PUBLIC int (*use_call_box)(_THIS(SharedData), char * resident_name); + SYNCHRONIZE PUBLIC void (*decrement_active_passengers)(_THIS(SharedData)); + SYNCHRONIZE PUBLIC int (*is_active_passengers_left)(_THIS(SharedData)); DESTRUCTOR(SharedData); } SharedData; diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index e59f4dc..0e0ba87 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -23,6 +23,7 @@ void * runnable_Visitor(void * void_this){ printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name); printf("Bip, %s appel a l'interphone\n%s habite a l'etage %d\n", this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name))); data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); + data->decrement_active_passengers(data); return NULL; }