mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-11 20:29:24 +00:00
Ça rentre
This commit is contained in:
@ -109,10 +109,14 @@ 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_get_inside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_get_inside_elevator);
|
||||
pthread_mutex_unlock(this->mutex_get_outside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_get_outside_elevator);
|
||||
pthread_mutex_unlock(this->mutex_cond_get_inside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_cond_get_inside_elevator);
|
||||
|
||||
pthread_mutex_unlock(this->mutex_cond_get_outside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_cond_get_outside_elevator);
|
||||
|
||||
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_func_get_inside_elevator);
|
||||
|
||||
DELETE(this->residents);
|
||||
DELETE(this->visitors);
|
||||
@ -121,8 +125,9 @@ void _free__Building(THIS(Building)){
|
||||
for (i=0; i<FLOORS; i++)
|
||||
free(this->condition_floors[i]);
|
||||
free(this->condition_floors);
|
||||
free(this->mutex_get_inside_elevator);
|
||||
free(this->mutex_get_outside_elevator);
|
||||
free(this->mutex_cond_get_inside_elevator);
|
||||
free(this->mutex_cond_get_outside_elevator);
|
||||
free(this->mutex_func_get_inside_elevator);
|
||||
free(this->elevators);
|
||||
|
||||
free(this);
|
||||
@ -131,16 +136,19 @@ void _free__Building(THIS(Building)){
|
||||
int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type){
|
||||
int i;
|
||||
/* Make assumption that a waiting elevator is not full */
|
||||
pthread_mutex_lock(this->mutex_func_get_inside_elevator);
|
||||
printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor, passenger.visitor->name, passenger.visitor->id);
|
||||
for (i=0; i<ELEVATOR_NB; i++){
|
||||
if (this->elevators[i]->get_floor(this->elevators[i]) == current_floor &&
|
||||
this->elevators[i]->get_state(this->elevators[i]) == WAITING){
|
||||
if (this->elevators[i]->can_get_inside(this->elevators[i], current_floor)){
|
||||
/* pour faire taire le compilateur le temps que je revienne sur cette fonction */
|
||||
if (type == VISITOR)
|
||||
this->elevators[i]->add_passenger(this->elevators[i], passenger.visitor->get_id(passenger.visitor));
|
||||
/* Il faut faire des trucs ici */
|
||||
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -150,7 +158,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
|
||||
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");}
|
||||
|
||||
pthread_cond_wait(this->condition_floors[origin], this->mutex_get_inside_elevator);
|
||||
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
|
||||
elevator_number = this->get_inside_elevator(this, origin, passenger, type);
|
||||
if (type == RESIDENT)
|
||||
printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.resident->name,
|
||||
@ -159,7 +167,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
|
||||
printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.visitor->name,
|
||||
this->elevators[elevator_number]->name, origin);
|
||||
|
||||
pthread_cond_wait(this->condition_floors[destination], this->mutex_get_outside_elevator);
|
||||
pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator);
|
||||
if (type == RESIDENT)
|
||||
printf("Le résident %s sors de l'ascenseur %s à l'étage %d\n", passenger.resident->name,
|
||||
this->elevators[elevator_number]->name, destination);
|
||||
@ -179,6 +187,10 @@ int use_call_box_Building(THIS(Building), char * resident_name){
|
||||
return -1;
|
||||
}
|
||||
|
||||
void signal_elevator_at_floor_Building(THIS(Building), int floor){
|
||||
pthread_cond_signal(this->condition_floors[floor]);
|
||||
}
|
||||
|
||||
Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
Building * new_building = malloc_or_die(sizeof(Building));
|
||||
char elevator_name[] = "@";
|
||||
@ -186,8 +198,15 @@ Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
|
||||
new_building->floors = FLOORS;
|
||||
new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB);
|
||||
new_building->mutex_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
new_building->mutex_get_outside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
|
||||
new_building->mutex_cond_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
new_building->mutex_cond_get_outside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
new_building->mutex_func_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
|
||||
pthread_mutex_init(new_building->mutex_cond_get_inside_elevator, NULL);
|
||||
pthread_mutex_init(new_building->mutex_cond_get_outside_elevator, NULL);
|
||||
pthread_mutex_init(new_building->mutex_func_get_inside_elevator, NULL);
|
||||
|
||||
new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS);
|
||||
new_building->residents = NEW(List);
|
||||
new_building->visitors = NEW(List);
|
||||
@ -207,7 +226,8 @@ Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
parse_visitors,
|
||||
get_inside_elevator,
|
||||
use_call_box,
|
||||
go_to_floor
|
||||
go_to_floor,
|
||||
signal_elevator_at_floor
|
||||
)
|
||||
|
||||
if (residents_file != NULL)
|
||||
|
@ -26,8 +26,9 @@ typedef struct o_Building {
|
||||
PRIVATE List * residents;
|
||||
PRIVATE List * visitors;
|
||||
PRIVATE Elevator ** elevators;
|
||||
PRIVATE pthread_mutex_t * mutex_get_inside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_get_outside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_func_get_inside_elevator;
|
||||
PRIVATE pthread_cond_t ** condition_floors;
|
||||
|
||||
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
|
||||
@ -35,6 +36,7 @@ typedef struct o_Building {
|
||||
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type);
|
||||
|
||||
PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name);
|
||||
PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor);
|
||||
|
||||
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type);
|
||||
|
||||
|
Reference in New Issue
Block a user