diff --git a/Building/Building.c b/Building/Building.c index 4d9a2da..eefa27c 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -122,8 +122,10 @@ void _free__Building(THIS(Building)){ DELETE(this->visitors); for (i=0; ielevators[i]); - for (i=0; icondition_floors[i]); free(this->condition_floors[i]); + } free(this->condition_floors); free(this->mutex_cond_get_inside_elevator); free(this->mutex_cond_get_outside_elevator); @@ -156,7 +158,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger int elevator_number; if (origin < 0 || origin >= FLOORS) {CRASH("You are trying to start from a non existing floor\n");} - if (destination < 0 || origin >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");} + if (destination < 0 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");} pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator); elevator_number = this->get_inside_elevator(this, origin, passenger, type); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index bb2c555..08aa61c 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -71,6 +71,8 @@ void *runnable_Elevator(void * void_this){ Elevator * this = (Elevator*) void_this; SharedData * data = GET_INSTANCE(SharedData); + AGENT_OPTIONS + printf("Je suis l'ascenseur %s\n", this->name); for (;;) data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this)); @@ -79,7 +81,6 @@ void *runnable_Elevator(void * void_this){ Elevator *_init_Elevator(char * name){ Elevator * new_elevator = malloc_or_die(sizeof(Elevator)); - new_elevator->state = WAITING; new_elevator->name = strdup(name); new_elevator->passenger_ids = NEW(List); pthread_mutex_init(&new_elevator->mutex_passenger, NULL); @@ -98,5 +99,8 @@ Elevator *_init_Elevator(char * name){ repair ); + new_elevator->set_floor(new_elevator, 0); + new_elevator->set_state(new_elevator, WAITING); + return new_elevator; } \ No newline at end of file diff --git a/Objects.c b/Objects.c index 3bb8b90..1a7cfde 100644 --- a/Objects.c +++ b/Objects.c @@ -11,3 +11,8 @@ void *malloc_or_die(size_t size){ } return ptr; } + +void agent_exit(int error_code){ + printf("Agent receiving code %d, exiting\n", error_code); + pthread_exit(NULL); +} diff --git a/Objects.h b/Objects.h index f9ca676..d3e362d 100644 --- a/Objects.h +++ b/Objects.h @@ -8,6 +8,9 @@ #include #include #include +#include + +void agent_exit(int error_code); #define NEW(type, ...) _init_##type(__VA_ARGS__) #define GET_INSTANCE(type, ...) _get_instance_##type(__VA_ARGS__) @@ -15,7 +18,15 @@ #define DESTRUCTOR(type) void (*_free_)(struct o_##type *this) #define THIS(type) type *this #define _THIS(type) struct o_##type *this -#define CRASH(message) perror(message); raise(SIGINT) +#define CRASH(message) perror(message); raise(SIGINT); pthread_exit(NULL) + +#define AGENT_OPTIONS sigset_t set; \ + sigemptyset(&set); \ + sigaddset(&set, SIGINT); \ + signal(SIGUSR1, agent_exit); \ + pthread_sigmask(SIG_BLOCK, &set, NULL); \ + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \ + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); #define SYNCHRONIZE #define PRIVATE diff --git a/Resident/Resident.c b/Resident/Resident.c index c455960..cccf30c 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -3,6 +3,7 @@ // #include +#include #include "Resident.h" GETTER(Resident, char *, name); @@ -11,6 +12,9 @@ GETTER(Resident, int, apartment_floor); void * runnable_Resident(void * void_this){ Resident * this = (Resident*) void_this; + + AGENT_OPTIONS + printf("Je suis le resident %s et je suis a l'etage %d en direction de l'etage %d\n", this->name, this->apartment_floor, this->destination); return NULL; diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index 6b911fa..79ec5fa 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -81,8 +81,10 @@ int use_call_box_SharedData(THIS(SharedData), char * resident_name){ void _free__SharedData(THIS(SharedData)){ int i; if (this->threads != NULL){ - for (i=0; ithreads_nb; i++) - pthread_cancel(this->threads[i]); + for (i=0; ithreads_nb; i++) { + printf("Quitting thread %d\n", (int) this->threads[i]); + pthread_kill(this->threads[i], SIGUSR1); + } free(this->threads); } if (this->main_building != NULL) diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 56752c4..971e302 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -13,6 +13,9 @@ void * runnable_Visitor(void * void_this){ Visitor *this = (Visitor*) void_this; SharedData * data = GET_INSTANCE(SharedData); Passenger passenger; + + AGENT_OPTIONS + passenger.visitor = this; printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name);