1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-15 21:33:22 +00:00
LO41/SharedData/SharedData.c
2018-06-18 15:20:19 +02:00

114 lines
3.3 KiB
C

//
// Created by Antoine Bartuccio on 06/06/2018.
//
#include "SharedData.h"
GETTER(SharedData, Building *, main_building)
SETTER(SharedData, Building *, main_building)
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;
}
void start_thread_SharedData(THIS(SharedData), void * (*thread)(void *), void * data, int thread_number){
pthread_create(&this->threads[thread_number], 0, thread, data);
}
void start_all_threads_SharedData(THIS(SharedData)){
int i = 0;
Element * current = NULL;
List * residents = NULL;
List * visitors = NULL;
if (this->threads == NULL){
if (this->main_building == NULL) {
CRASH("No building attached to SharedData and thus no thread can be created\n");
}
else {
residents = this->main_building->residents;
visitors = this->main_building->visitors;
this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB;
this->threads = malloc_or_die(sizeof(pthread_t) * this->threads_nb);
/* starts threading elevators */
for (i=0; i<ELEVATOR_NB; i++)
this->start_thread(this, this->main_building->elevators[i]->runnable,
this->main_building->elevators[i], i);
/* starts threading visitors */
current = visitors->get_head(visitors);
while (current != NULL){
this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i);
current = current->get_next(current);
i++;
}
/* starts threading residents */
current = residents->get_head(residents);
while (current != NULL){
this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i);
current = current->get_next(current);
i++;
}
}
}
}
void wait_all_threads_SharedData(THIS(SharedData)){
int i;
for (i=0; i<this->threads_nb; i++)
pthread_join(this->threads[i], NULL);
}
int use_call_box_SharedData(THIS(SharedData), char * resident_name){
return this->main_building->use_call_box(this->main_building, resident_name);
}
void _free__SharedData(THIS(SharedData)){
int i;
if (this->threads != NULL){
for (i=0; i<this->threads_nb; i++) {
printf("Quitting thread %d\n", (int) this->threads[i]);
pthread_kill(this->threads[i], SIGUSR1);
}
free(this->threads);
}
if (this->main_building != NULL)
DELETE(this->main_building);
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 before */
new_shared_data->threads_nb = 0;
LINK_ALL(SharedData, new_shared_data,
start_thread,
start_all_threads,
wait_all_threads,
set_main_building,
get_main_building,
use_call_box,
call_elevator
);
}
return new_shared_data;
}