1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2025-07-12 20:59:24 +00:00

Début de synchronisation stylée

This commit is contained in:
2018-06-10 04:00:01 +02:00
parent dd297cee99
commit beca5698b4
5 changed files with 109 additions and 7 deletions

View File

@ -96,16 +96,63 @@ 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);
DELETE(this->residents);
DELETE(this->visitors);
DELETE(this->box);
for (i=0; i<ELEVATOR_NB; i++)
DELETE(this->elevators[i]);
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->elevators);
free(this);
}
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 */
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){
/* Il faut faire des trucs ici */
return i;
}
}
return -1;
}
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type){
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");
pthread_cond_wait(this->condition_floors[origin], this->mutex_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,
this->elevators[elevator_number]->name, origin);
else if (type == visitor)
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);
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);
else if (type == visitor)
printf("Le visiteur %s sors de l'ascenseur %s à l'étage %d\n", passenger.visitor->name,
this->elevators[elevator_number]->name, destination);
}
Building *_init_Building(char * residents_file, char * visitors_file){
Building * new_building = malloc_or_die(sizeof(Building));
char elevator_name[] = "@";
@ -113,6 +160,9 @@ 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->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS);
new_building->residents = NEW(List);
new_building->visitors = NEW(List);
new_building->box = NEW(CommunicationBox);
@ -120,12 +170,18 @@ Building *_init_Building(char * residents_file, char * visitors_file){
elevator_name[0]++;
new_building->elevators[i] = NEW(Elevator, elevator_name);
}
for (i=0; i<FLOORS; i++){
new_building->condition_floors[i] = malloc_or_die((sizeof(pthread_cond_t)));
pthread_cond_init(new_building->condition_floors[i], NULL);
}
LINK_ALL(Building, new_building,
parse_residents,
parse_visitors
parse_visitors,
get_inside_elevator,
go_to_floor
)
if (residents_file != NULL)

View File

@ -12,16 +12,31 @@
#include "../List/List.h"
#include "../Elevator/Elevator.h"
#include "../CommunicationBox/CommunicationBox.h"
#include "../Resident/Resident.h"
#include "../Visitor/Visitor.h"
typedef enum {resident, visitor} PASSENGER_TYPE;
typedef union u_Passenger {
Resident * resident;
Visitor * visitor;
} Passenger;
typedef struct o_Building {
int floors;
List * residents;
List * visitors;
CommunicationBox * box;
Elevator ** elevators;
PRIVATE int floors;
PRIVATE List * residents;
PRIVATE List * visitors;
PRIVATE CommunicationBox * box;
PRIVATE Elevator ** elevators;
PRIVATE pthread_mutex_t * mutex_get_inside_elevator;
PRIVATE pthread_mutex_t * mutex_get_outside_elevator;
PRIVATE pthread_cond_t ** condition_floors;
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
PRIVATE void (*parse_visitors)(_THIS(Building), char * file);
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type);
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type);
DESTRUCTOR(Building);
} Building;