1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-25 10:34:17 +00:00
LO41/SharedData/SharedData.c

57 lines
1.5 KiB
C
Raw Normal View History

2018-06-06 11:50:11 +00:00
//
// Created by Antoine Bartuccio on 06/06/2018.
//
#include "SharedData.h"
SETTER(SharedData, Building *, main_building)
void wait_threads_SharedData(THIS(SharedData)){
int i;
for (i=0; i<this->threads_nb; i++)
pthread_join(this->threads[i], NULL);
}
int call_elevator_SharedData(THIS(SharedData), int starting_floor, int destination_floor){
/* Make the thread wait for an elevator available and return the id of the elevator available */
if (this->main_building == NULL)
CRASH("No building attached to shared data, you cannot call an elevator\n");
if (starting_floor < 0)
CRASH("You cannot start from a floor lower than 0\n");
if (destination_floor > FLOORS)
CRASH("You are trying to reach a floor higher than the highest floor\n");
return 0;
}
2018-06-06 11:50:11 +00:00
void _free__SharedData(THIS(SharedData)){
int i;
if (this->threads != NULL){
for (i=0; i<this->threads_nb; i++)
pthread_cancel(this->threads[i]);
free(this->threads);
}
if (this->main_building != NULL)
DELETE(this->main_building);
2018-06-06 11:50:11 +00:00
free(this);
}
SharedData *_get_instance_SharedData(){
static SharedData * new_shared_data = NULL;
if (new_shared_data == NULL){
new_shared_data = malloc_or_die(sizeof(SharedData));
new_shared_data->threads = NULL;
new_shared_data->main_building = NULL; /* Should be set to NULL if freed */
new_shared_data->threads_nb = 0;
LINK_ALL(SharedData, new_shared_data,
wait_threads,
set_main_building,
call_elevator
);
2018-06-06 11:50:11 +00:00
}
return new_shared_data;
}