1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-15 05:13:22 +00:00
LO41/SharedData/SharedData.c

140 lines
4.2 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"
2018-06-07 22:49:20 +00:00
GETTER(SharedData, Building *, main_building)
2018-06-11 08:52:58 +00:00
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 */
2018-06-18 13:20:19 +00:00
if (this->main_building == NULL) {
CRASH("No building attached to shared data, you cannot call an elevator\n");
2018-06-18 13:20:19 +00:00
}
if (starting_floor < 0) {
CRASH("You cannot start from a floor lower than 0\n");
2018-06-18 13:20:19 +00:00
}
if (destination_floor > FLOORS) {
CRASH("You are trying to reach a floor higher than the highest floor\n");
2018-06-18 13:20:19 +00:00
}
return 0;
}
2018-06-10 23:58:52 +00:00
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){
2018-06-15 09:50:38 +00:00
if (this->main_building == NULL) {
2018-06-10 23:58:52 +00:00
CRASH("No building attached to SharedData and thus no thread can be created\n");
2018-06-15 09:50:38 +00:00
}
2018-06-10 23:58:52 +00:00
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);
2018-06-21 23:58:11 +00:00
pthread_mutex_lock(&this->mutex_active_passengers);
2018-06-11 00:31:03 +00:00
/* 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);
2018-06-10 23:58:52 +00:00
/* 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);
2018-06-21 23:58:11 +00:00
this->active_passengers++;
2018-06-10 23:58:52 +00:00
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);
2018-06-21 23:58:11 +00:00
this->active_passengers++;
2018-06-10 23:58:52 +00:00
i++;
}
2018-06-21 23:58:11 +00:00
pthread_mutex_unlock(&this->mutex_active_passengers);
2018-06-10 23:58:52 +00:00
}
}
}
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);
}
2018-06-06 11:50:11 +00:00
void _free__SharedData(THIS(SharedData)){
int i;
if (this->threads != NULL){
2018-06-15 17:14:30 +00:00
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);
2018-06-21 23:58:11 +00:00
pthread_mutex_unlock(&this->mutex_active_passengers);
pthread_mutex_destroy(&this->mutex_active_passengers);
2018-06-06 11:50:11 +00:00
free(this);
}
2018-06-21 23:58:11 +00:00
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);
}
2018-06-06 11:50:11 +00:00
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;
2018-06-11 08:52:58 +00:00
new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */
new_shared_data->threads_nb = 0;
2018-06-21 23:58:11 +00:00
new_shared_data->active_passengers = 0;
pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL);
LINK_ALL(SharedData, new_shared_data,
2018-06-10 23:58:52 +00:00
start_thread,
start_all_threads,
wait_all_threads,
set_main_building,
2018-06-07 22:49:20 +00:00
get_main_building,
use_call_box,
2018-06-21 23:58:11 +00:00
call_elevator,
decrement_active_passengers,
is_active_passengers_left
);
2018-06-06 11:50:11 +00:00
}
return new_shared_data;
}