préempté ?

This commit is contained in:
Aethor 2018-06-20 17:49:18 +02:00
parent 120fd7f0d8
commit df740634ad
3 changed files with 18 additions and 6 deletions

View File

@ -146,13 +146,12 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
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);
printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor,
passenger.type == VISITOR ? passenger.visitor->name : passenger.resident->name,
passenger.type == VISITOR ? passenger.visitor->id : passenger.resident->id);
for (i=0; i<ELEVATOR_NB; i++){
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 (passenger.type == VISITOR) TODO : ???
this->elevators[i]->add_passenger(this->elevators[i], passenger);
/* Il faut faire des trucs ici */
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
return i;
}
@ -167,6 +166,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 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger));
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
elevator_number = this->get_inside_elevator(this, origin, passenger);
@ -190,7 +190,9 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
printf("Le résident %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger.resident->name, origin);
else if (passenger.type == VISITOR)
printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger.visitor->name, origin);
}//todo : else, remettre en attente ?
//réarmement
this->go_to_floor(this, origin, destination, passenger);
}
}
@ -228,6 +230,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){
new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS);
new_building->residents = NEW(List);
new_building->visitors = NEW(List);
new_building->waiting_passengers = NEW(List);
for (i=0; i<ELEVATOR_NB; i++) {
elevator_name[0]++;
new_building->elevators[i] = NEW(Elevator, elevator_name);

View File

@ -19,6 +19,7 @@ typedef struct o_Building {
PRIVATE int floors;
PRIVATE List * residents;
PRIVATE List * visitors;
PRIVATE List * waiting_passengers;
PRIVATE Elevator ** elevators;
PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator;
PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator;
@ -40,6 +41,7 @@ typedef struct o_Building {
FRIENDLY(residents, SharedData)
FRIENDLY(visitors, SharedData)
FRIENDLY(elevators, SharedData)
FRIENDLY(waiting_passengers, SharedData)
Building *_init_Building(char * residents_file, char * visitors_file);

View File

@ -5,6 +5,7 @@
#include <string.h>
#include <pthread.h>
#include "Resident.h"
#include "../SharedData/SharedData.h"
GETTER(Resident, char *, name);
GETTER(Resident, int, id);
@ -12,11 +13,17 @@ GETTER(Resident, int, apartment_floor);
void * runnable_Resident(void * void_this){
Resident * this = (Resident*) void_this;
SharedData* data = GET_INSTANCE(SharedData);
Passenger passenger;
AGENT_OPTIONS
AGENT_OPTIONS;
passenger.resident = this;
passenger.type = RESIDENT;
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);
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
return NULL;
}