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

177 lines
5.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"
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);
2018-06-10 23:58:52 +00:00
}
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;
2018-06-22 05:13:13 +00:00
this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB + 1;
this->threads = malloc_or_die(sizeof(pthread_t*) * this->threads_nb);
2018-06-22 01:18:44 +00:00
for (i=0; i < this->threads_nb; i++)
this->threads[i] = malloc_or_die(sizeof(pthread_t));
2018-06-10 23:58:52 +00:00
2018-06-21 23:58:11 +00:00
pthread_mutex_lock(&this->mutex_active_passengers);
pthread_mutex_lock(&this->mutex_threads);
2018-06-22 01:59:36 +00:00
2018-06-22 03:49:15 +00:00
2018-06-11 00:31:03 +00:00
/* starts threading elevators */
2018-06-22 05:13:13 +00:00
for (i=0; i<ELEVATOR_NB; i++) {
2018-06-11 00:31:03 +00:00
this->start_thread(this, this->main_building->elevators[i]->runnable,
this->main_building->elevators[i], i);
this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i);
}
2018-06-11 00:31:03 +00:00
2018-06-22 05:13:13 +00:00
/* starting the elevator breaker */
this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, i);
this->elevator_breaker->set_thread_number(this->elevator_breaker, i);
i++;
/* starts threading residents */
current = residents->get_head(residents);
2018-06-10 23:58:52 +00:00
while (current != NULL){
2018-06-22 05:13:13 +00:00
this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i);
((Resident*)current->get_data(current))->set_thread_number((Resident*)current->get_data(current), i);
2018-06-10 23:58:52 +00:00
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-22 05:13:13 +00:00
/* starts threading visitors */
current = visitors->get_head(visitors);
2018-06-10 23:58:52 +00:00
while (current != NULL){
2018-06-22 05:13:13 +00:00
this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i);
((Visitor*)current->get_data(current))->set_thread_number((Visitor*)current->get_data(current), i);
2018-06-10 23:58:52 +00:00
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-22 03:49:15 +00:00
pthread_mutex_unlock(&this->mutex_threads);
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++) {
if (this->threads[i] != NULL)
pthread_join(*this->threads[i], NULL);
}
2018-06-10 23:58:52 +00:00
}
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++) {
if (this->threads[i] != NULL) {
2018-06-22 01:16:30 +00:00
printf("Quitting thread %d\n", (*(int*) this->threads[i]));
pthread_kill(*this->threads[i], SIGUSR1);
2018-06-22 01:18:44 +00:00
free(this->threads[i]);
}
2018-06-15 17:14:30 +00:00
}
free(this->threads);
}
if (this->main_building != NULL)
DELETE(this->main_building);
2018-06-22 01:59:36 +00:00
DELETE(this->elevator_breaker);
2018-06-21 23:58:11 +00:00
pthread_mutex_unlock(&this->mutex_active_passengers);
pthread_mutex_destroy(&this->mutex_active_passengers);
2018-06-22 01:16:30 +00:00
pthread_mutex_unlock(&this->mutex_threads);
pthread_mutex_destroy(&this->mutex_threads);
2018-06-21 23:58:11 +00:00
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);
}
void unregister_thread_SharedData(THIS(SharedData), int thread_number){
pthread_mutex_lock(&this->mutex_threads);
2018-06-22 01:18:44 +00:00
free(this->threads[thread_number]);
this->threads[thread_number] = NULL;
pthread_mutex_unlock(&this->mutex_threads);
}
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;
2018-06-22 01:59:36 +00:00
new_shared_data->elevator_breaker = NEW(ElevatorBreaker);
2018-06-21 23:58:11 +00:00
pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL);
pthread_mutex_init(&new_shared_data->mutex_threads, 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,
unregister_thread
);
2018-06-06 11:50:11 +00:00
}
return new_shared_data;
}