mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-12-23 00:01:11 +00:00
Merge branch 'master' into 'bro'
# Conflicts: # Elevator/Elevator.c
This commit is contained in:
commit
a8107a97b1
@ -135,7 +135,6 @@ 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_cond_get_inside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_cond_get_inside_elevator);
|
||||
|
||||
|
@ -133,7 +133,8 @@ void *runnable_Elevator(void * void_this){
|
||||
AGENT_OPTIONS
|
||||
|
||||
printf("Ascenseur %s : Initialisation...\n", this->name);
|
||||
for (;;){
|
||||
while (data->is_active_passengers_left(data)){
|
||||
// for (;;){
|
||||
usleep(250000);
|
||||
if(this->target_floor == this->get_floor(this)){
|
||||
|
||||
|
@ -12,8 +12,8 @@
|
||||
typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE;
|
||||
|
||||
typedef struct o_Passenger {
|
||||
PASSENGER_TYPE type;
|
||||
union {
|
||||
PUBLIC PASSENGER_TYPE type;
|
||||
PUBLIC union {
|
||||
Resident * resident;
|
||||
Visitor * visitor;
|
||||
};
|
||||
|
@ -29,6 +29,7 @@ void * runnable_Resident(void * void_this){
|
||||
printf("Résident %s : Je reste chez moi\n", this->name);
|
||||
else
|
||||
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
|
||||
data->decrement_active_passengers(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -41,6 +41,7 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB;
|
||||
this->threads = malloc_or_die(sizeof(pthread_t) * this->threads_nb);
|
||||
|
||||
pthread_mutex_lock(&this->mutex_active_passengers);
|
||||
/* starts threading elevators */
|
||||
for (i=0; i<ELEVATOR_NB; i++)
|
||||
this->start_thread(this, this->main_building->elevators[i]->runnable,
|
||||
@ -51,6 +52,7 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
while (current != NULL){
|
||||
this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i);
|
||||
current = current->get_next(current);
|
||||
this->active_passengers++;
|
||||
i++;
|
||||
}
|
||||
|
||||
@ -59,8 +61,10 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
while (current != NULL){
|
||||
this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i);
|
||||
current = current->get_next(current);
|
||||
this->active_passengers++;
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_unlock(&this->mutex_active_passengers);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -87,9 +91,26 @@ void _free__SharedData(THIS(SharedData)){
|
||||
if (this->main_building != NULL)
|
||||
DELETE(this->main_building);
|
||||
|
||||
pthread_mutex_unlock(&this->mutex_active_passengers);
|
||||
pthread_mutex_destroy(&this->mutex_active_passengers);
|
||||
|
||||
free(this);
|
||||
}
|
||||
|
||||
int is_active_passengers_left_SharedData(THIS(SharedData)){
|
||||
int response;
|
||||
pthread_mutex_lock(&this->mutex_active_passengers);
|
||||
response = (this->active_passengers > 0);
|
||||
pthread_mutex_unlock(&this->mutex_active_passengers);
|
||||
return response;
|
||||
}
|
||||
|
||||
void decrement_active_passengers_SharedData(THIS(SharedData)){
|
||||
pthread_mutex_lock(&this->mutex_active_passengers);
|
||||
this->active_passengers--;
|
||||
pthread_mutex_unlock(&this->mutex_active_passengers);
|
||||
}
|
||||
|
||||
SharedData *_get_instance_SharedData(){
|
||||
static SharedData * new_shared_data = NULL;
|
||||
if (new_shared_data == NULL){
|
||||
@ -98,6 +119,9 @@ SharedData *_get_instance_SharedData(){
|
||||
new_shared_data->threads = NULL;
|
||||
new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */
|
||||
new_shared_data->threads_nb = 0;
|
||||
new_shared_data->active_passengers = 0;
|
||||
|
||||
pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL);
|
||||
|
||||
LINK_ALL(SharedData, new_shared_data,
|
||||
start_thread,
|
||||
@ -106,7 +130,9 @@ SharedData *_get_instance_SharedData(){
|
||||
set_main_building,
|
||||
get_main_building,
|
||||
use_call_box,
|
||||
call_elevator
|
||||
call_elevator,
|
||||
decrement_active_passengers,
|
||||
is_active_passengers_left
|
||||
);
|
||||
}
|
||||
return new_shared_data;
|
||||
|
@ -11,8 +11,10 @@
|
||||
|
||||
typedef struct o_SharedData {
|
||||
PRIVATE int threads_nb;
|
||||
PRIVATE int active_passengers;
|
||||
PRIVATE pthread_t *threads;
|
||||
PRIVATE Building * main_building;
|
||||
PRIVATE pthread_mutex_t mutex_active_passengers;
|
||||
|
||||
PRIVATE void (*start_thread)(_THIS(SharedData), void * (*thread)(void *), void * data, int thread_number);
|
||||
|
||||
@ -22,6 +24,8 @@ typedef struct o_SharedData {
|
||||
PUBLIC Building * (*get_main_building)(_THIS(SharedData));
|
||||
PUBLIC int (*call_elevator)(_THIS(SharedData), int starting_floor, int destination_floor);
|
||||
PUBLIC int (*use_call_box)(_THIS(SharedData), char * resident_name);
|
||||
SYNCHRONIZE PUBLIC void (*decrement_active_passengers)(_THIS(SharedData));
|
||||
SYNCHRONIZE PUBLIC int (*is_active_passengers_left)(_THIS(SharedData));
|
||||
|
||||
DESTRUCTOR(SharedData);
|
||||
} SharedData;
|
||||
|
@ -23,6 +23,7 @@ void * runnable_Visitor(void * void_this){
|
||||
printf("Visiteur %s : Je souhaite rendre visite a %s\n", this->name, this->contact_name);
|
||||
printf("Visiteur %s : J'apelle à l'interphone\nVisiteur %s : J'apprends que %s habite à l'étage %d\n", this->name, this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name)));
|
||||
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
|
||||
data->decrement_active_passengers(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user