1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2025-07-10 11:49:24 +00:00

Les premières méthodes synchronisées

This commit is contained in:
2018-06-07 22:04:30 +02:00
parent 027f2bcc15
commit 44ddb79df8
7 changed files with 113 additions and 11 deletions

View File

@ -4,7 +4,36 @@
#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;
}
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);
free(this);
}
@ -12,7 +41,16 @@ SharedData *_get_instance_SharedData(){
static SharedData * new_shared_data = NULL;
if (new_shared_data == NULL){
new_shared_data = malloc_or_die(sizeof(SharedData));
LINK(SharedData, new_shared_data, _free_);
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
);
}
return new_shared_data;
}

View File

@ -5,9 +5,23 @@
#ifndef LO41_SHAREDDATA_H
#define LO41_SHAREDDATA_H
#include <pthread.h>
#include "../Objects.h"
#include "../Building/Building.h"
#define MUTEX_NB 3
enum {mutex_1, mutex_2, mutex_3};
typedef struct o_SharedData {
PRIVATE int threads_nb;
PRIVATE pthread_t *threads;
PRIVATE pthread_mutex_t mutex_array[MUTEX_NB];
PRIVATE Building * main_building;
PUBLIC void (*set_main_building)(_THIS(SharedData), Building * building);
PUBLIC void (*wait_threads)(_THIS(SharedData));
PUBLIC int (*call_elevator)(_THIS(SharedData), int starting_floor, int destination_floor);
DESTRUCTOR(SharedData);
} SharedData;