Début de synchronisation stylée

This commit is contained in:
Antoine Bartuccio 2018-06-10 04:00:01 +02:00
parent dd297cee99
commit beca5698b4
Signed by: klmp200
GPG Key ID: E7245548C53F904B
5 changed files with 109 additions and 7 deletions

View File

@ -96,16 +96,63 @@ void _free__Building(THIS(Building)){
this->residents->clear_custom(this->residents, free_resident);
this->visitors->clear_custom(this->visitors, free_visitor);
pthread_mutex_unlock(this->mutex_get_inside_elevator);
pthread_mutex_destroy(this->mutex_get_inside_elevator);
pthread_mutex_unlock(this->mutex_get_outside_elevator);
pthread_mutex_destroy(this->mutex_get_outside_elevator);
DELETE(this->residents);
DELETE(this->visitors);
DELETE(this->box);
for (i=0; i<ELEVATOR_NB; i++)
DELETE(this->elevators[i]);
for (i=0; i<FLOORS; i++)
free(this->condition_floors[i]);
free(this->condition_floors);
free(this->mutex_get_inside_elevator);
free(this->mutex_get_outside_elevator);
free(this->elevators);
free(this);
}
int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type){
int i;
/* Make assumption that a waiting elevator is not full */
for (i=0; i<ELEVATOR_NB; i++){
if (this->elevators[i]->get_floor(this->elevators[i]) == current_floor &&
this->elevators[i]->get_state(this->elevators[i]) == waiting){
/* Il faut faire des trucs ici */
return i;
}
}
return -1;
}
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type){
int elevator_number;
if (origin < 0 || origin >= FLOORS) CRASH("You are trying to start from a non existing floor\n");
if (destination < 0 || origin >= FLOORS) CRASH("You are trying to reach a non existing floor\n");
pthread_cond_wait(this->condition_floors[origin], this->mutex_get_inside_elevator);
elevator_number = this->get_inside_elevator(this, origin, passenger, type);
if (type == resident)
printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.resident->name,
this->elevators[elevator_number]->name, origin);
else if (type == visitor)
printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.visitor->name,
this->elevators[elevator_number]->name, origin);
pthread_cond_wait(this->condition_floors[destination], this->mutex_get_outside_elevator);
if (type == resident)
printf("Le résident %s sors de l'ascenseur %s à l'étage %d\n", passenger.resident->name,
this->elevators[elevator_number]->name, destination);
else if (type == visitor)
printf("Le visiteur %s sors de l'ascenseur %s à l'étage %d\n", passenger.visitor->name,
this->elevators[elevator_number]->name, destination);
}
Building *_init_Building(char * residents_file, char * visitors_file){
Building * new_building = malloc_or_die(sizeof(Building));
char elevator_name[] = "@";
@ -113,6 +160,9 @@ Building *_init_Building(char * residents_file, char * visitors_file){
new_building->floors = FLOORS;
new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB);
new_building->mutex_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
new_building->mutex_get_outside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS);
new_building->residents = NEW(List);
new_building->visitors = NEW(List);
new_building->box = NEW(CommunicationBox);
@ -120,12 +170,18 @@ Building *_init_Building(char * residents_file, char * visitors_file){
elevator_name[0]++;
new_building->elevators[i] = NEW(Elevator, elevator_name);
}
for (i=0; i<FLOORS; i++){
new_building->condition_floors[i] = malloc_or_die((sizeof(pthread_cond_t)));
pthread_cond_init(new_building->condition_floors[i], NULL);
}
LINK_ALL(Building, new_building,
parse_residents,
parse_visitors
parse_visitors,
get_inside_elevator,
go_to_floor
)
if (residents_file != NULL)

View File

@ -12,16 +12,31 @@
#include "../List/List.h"
#include "../Elevator/Elevator.h"
#include "../CommunicationBox/CommunicationBox.h"
#include "../Resident/Resident.h"
#include "../Visitor/Visitor.h"
typedef enum {resident, visitor} PASSENGER_TYPE;
typedef union u_Passenger {
Resident * resident;
Visitor * visitor;
} Passenger;
typedef struct o_Building {
int floors;
List * residents;
List * visitors;
CommunicationBox * box;
Elevator ** elevators;
PRIVATE int floors;
PRIVATE List * residents;
PRIVATE List * visitors;
PRIVATE CommunicationBox * box;
PRIVATE Elevator ** elevators;
PRIVATE pthread_mutex_t * mutex_get_inside_elevator;
PRIVATE pthread_mutex_t * mutex_get_outside_elevator;
PRIVATE pthread_cond_t ** condition_floors;
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
PRIVATE void (*parse_visitors)(_THIS(Building), char * file);
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type);
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type);
DESTRUCTOR(Building);
} Building;

View File

@ -7,6 +7,8 @@
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, floor)
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, floor)
void _free__Elevator(THIS(Elevator)){
DELETE(this->passenger_ids);
@ -19,6 +21,9 @@ void _free__Elevator(THIS(Elevator)){
pthread_mutex_unlock(&this->mutex_state);
pthread_mutex_destroy(&this->mutex_state);
pthread_mutex_unlock(&this->mutex_floor);
pthread_mutex_destroy(&this->mutex_floor);
free(this);
}
@ -52,6 +57,7 @@ Elevator *_init_Elevator(char * name){
new_elevator->passenger_ids = NEW(List);
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
pthread_mutex_init(&new_elevator->mutex_state, NULL);
pthread_mutex_init(&new_elevator->mutex_floor, NULL);
LINK_ALL(Elevator, new_elevator,
runnable,
@ -59,6 +65,8 @@ Elevator *_init_Elevator(char * name){
can_get_more_passengers,
get_state,
set_state,
get_floor,
set_floor,
repair
);

View File

@ -17,16 +17,20 @@ typedef struct o_Elevator {
PRIVATE ELEVATOR_STATE state;
PRIVATE List * passenger_ids;
PRIVATE char * name;
PRIVATE int floor;
PRIVATE pthread_mutex_t mutex_passenger;
PRIVATE pthread_mutex_t mutex_state;
PRIVATE pthread_mutex_t mutex_floor;
PUBLIC void * (*runnable)(void * void_this);
SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var);
SYNCHRONIZE PRIVATE int (*set_floor)(_THIS(Elevator));
SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*can_get_more_passengers)(_THIS(Elevator));
DESTRUCTOR(Elevator);

21
main.c
View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "Objects.h"
#include "List/List.h"
@ -8,6 +9,15 @@
#include "Visitor/Visitor.h"
#include "Resident/Resident.h"
pthread_cond_t cond;
void * test_sub(void *bite){
usleep(2000000);
printf("Je relache le programme\n");
pthread_cond_signal(&cond);
return bite;
}
void clean_exit(int error_code){
printf("Signal %d received, exiting app\n", error_code);
DELETE(GET_INSTANCE(SharedData));
@ -20,11 +30,21 @@ int main() {
Resident * sli = NEW(Resident, 1, "Sli", 2);
char text1[30] = "Patate";
char text2[30] = "Patator";
pthread_mutex_t mutex;
pthread_t thread;
GET_INSTANCE(SharedData);
signal(SIGINT, clean_exit);
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("La taille est de %d\n", l->get_size(l));
pthread_create(&thread, 0, test_sub, NULL);
printf("FREEEZZZE\n");
pthread_cond_wait(&cond, &mutex);
pthread_cond_destroy(&cond);
l->insert_tail(l, text1, sizeof(char) * 30);
l->insert_head(l, text2, sizeof(char) * 30);
l->insert_head(l, "Bite", sizeof(char) * 30);
@ -46,7 +66,6 @@ int main() {
printf("%s est le second resident\n", ((Resident *) main_building->residents->get_element_data(main_building->residents, 1))->name);
for (int i=0; i < ELEVATOR_NB; i++)
printf("Ascenseur %s\n", main_building->elevators[i]->name);
GET_INSTANCE(SharedData)->main_building = main_building;
DELETE(main_building);
printf("Hello, World!\n");