1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-12-22 07:41:10 +00:00

Ça rentre

This commit is contained in:
Antoine Bartuccio 2018-06-15 15:38:26 +02:00
parent ab17d4fa0e
commit 415ef4807c
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 60 additions and 21 deletions

View File

@ -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)

View File

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

View File

@ -4,6 +4,7 @@
#include <string.h>
#include "Elevator.h"
#include "../SharedData/SharedData.h"
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
@ -30,8 +31,8 @@ void _free__Elevator(THIS(Elevator)){
void add_passenger_Elevator(THIS(Elevator), int passenger_id){
pthread_mutex_lock(&this->mutex_passenger);
this->passenger_ids->insert_tail(this->passenger_ids, ((void *)&passenger_id), sizeof(int));
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers\n",
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids));
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n",
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids), this->name);
if (this->passenger_ids->get_size(this->passenger_ids) >= MAX_ELEVATOR_CAPACITY)
this->set_state(this, SLEEPING);
pthread_mutex_unlock(&this->mutex_passenger);
@ -45,8 +46,20 @@ int get_number_of_passengers_Elevator(THIS(Elevator)){
return num;
}
int can_get_more_passengers_Elevator(THIS(Elevator)){
return (this->get_number_of_passengers(this) < MAX_ELEVATOR_CAPACITY);
int can_get_inside_Elevator(THIS(Elevator), int floor){
int permission;
pthread_mutex_lock(&this->mutex_passenger);
pthread_mutex_lock(&this->mutex_state);
pthread_mutex_lock(&this->mutex_floor);
permission = (this->passenger_ids->get_size(this->passenger_ids) < MAX_ELEVATOR_CAPACITY &&
this->state == WAITING && this->floor == floor);
pthread_mutex_unlock(&this->mutex_floor);
pthread_mutex_unlock(&this->mutex_state);
pthread_mutex_unlock(&this->mutex_passenger);
return permission;
}
void repair_Elevator(THIS(Elevator)){
@ -56,7 +69,11 @@ void repair_Elevator(THIS(Elevator)){
void *runnable_Elevator(void * void_this){
/* This is where the thread logic will be implemented */
Elevator * this = (Elevator*) void_this;
SharedData * data = GET_INSTANCE(SharedData);
printf("Je suis l'ascenseur %s\n", this->name);
for (;;)
data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
return NULL;
}
@ -72,7 +89,7 @@ Elevator *_init_Elevator(char * name){
LINK_ALL(Elevator, new_elevator,
runnable,
get_number_of_passengers,
can_get_more_passengers,
can_get_inside,
add_passenger,
get_state,
set_state,

View File

@ -33,7 +33,7 @@ typedef struct o_Elevator {
SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), int passenger_id);
SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*can_get_more_passengers)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*can_get_inside)(_THIS(Elevator), int floor);
DESTRUCTOR(Elevator);
} Elevator;