1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2025-07-11 04:09: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

@ -2,16 +2,41 @@
// Created by Antoine Bartuccio on 05/06/2018.
//
#include <string.h>
#include "Elevator.h"
void _free__Elevator(THIS(Elevator)){
DELETE(this->passenger_ids);
if (this->name != NULL)
free(this->name);
pthread_mutex_unlock(&this->passenger_mutex);
pthread_mutex_destroy(&this->passenger_mutex);
free(this);
}
Elevator *_init_Elevator(){
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
int get_number_of_passengers_Elevator(THIS(Elevator)){
int num;
pthread_mutex_lock(&this->passenger_mutex);
num = this->passenger_ids->get_size(this->passenger_ids);
pthread_mutex_lock(&this->passenger_mutex);
return num;
}
LINK(Elevator, new_elevator, _free_);
int can_get_more_passengers_Elevator(_THIS(Elevator)){
return (this->get_number_of_passengers(this) < MAX_ELEVATOR_CAPACITY);
}
Elevator *_init_Elevator(char * name){
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
new_elevator->state = waiting;
new_elevator->name = strdup(name);
new_elevator->passenger_ids = NEW(List);
pthread_mutex_init(&new_elevator->passenger_mutex, NULL);
LINK_ALL(Elevator, new_elevator,
get_number_of_passengers,
can_get_more_passengers
);
return new_elevator;
}

View File

@ -5,13 +5,27 @@
#ifndef LO41_ELEVATOR_H
#define LO41_ELEVATOR_H
#include <pthread.h>
#include "../Objects.h"
#include "../List/List.h"
#define MAX_ELEVATOR_CAPACITY 10
typedef enum {running, waiting, sleeping} ELEVATOR_STATE;
typedef struct o_Elevator {
PRIVATE ELEVATOR_STATE state;
PRIVATE List * passenger_ids;
PRIVATE char * name;
PRIVATE pthread_mutex_t passenger_mutex;
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*can_get_more_passengers(_THIS(Elevator)));
DESTRUCTOR(Elevator);
} Elevator;
Elevator *_init_Elevator();
FRIENDLY(state, Building)
Elevator *_init_Elevator(char* name);
#endif //LO41_ELEVATOR_H