mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-26 02:54:18 +00:00
66 lines
1.7 KiB
C
66 lines
1.7 KiB
C
//
|
|
// Created by Antoine Bartuccio on 05/06/2018.
|
|
//
|
|
|
|
#include <string.h>
|
|
#include "Elevator.h"
|
|
|
|
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
|
|
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
|
|
|
|
void _free__Elevator(THIS(Elevator)){
|
|
DELETE(this->passenger_ids);
|
|
if (this->name != NULL)
|
|
free(this->name);
|
|
|
|
pthread_mutex_unlock(&this->mutex_passenger);
|
|
pthread_mutex_destroy(&this->mutex_passenger);
|
|
|
|
pthread_mutex_unlock(&this->mutex_state);
|
|
pthread_mutex_destroy(&this->mutex_state);
|
|
|
|
free(this);
|
|
}
|
|
|
|
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
|
int num;
|
|
pthread_mutex_lock(&this->mutex_passenger);
|
|
num = this->passenger_ids->get_size(this->passenger_ids);
|
|
pthread_mutex_lock(&this->mutex_passenger);
|
|
return num;
|
|
}
|
|
|
|
int can_get_more_passengers_Elevator(THIS(Elevator)){
|
|
return (this->get_number_of_passengers(this) < MAX_ELEVATOR_CAPACITY);
|
|
}
|
|
|
|
void repair_Elevator(THIS(Elevator)){
|
|
this->set_state(this, running);
|
|
}
|
|
|
|
void *runnable_Elevator(void * void_this){
|
|
/* This is where the thread logic will be implemented */
|
|
Elevator * this = (Elevator*) void_this;
|
|
/* Returning this to keep gcc and clang quiet while developing, will return NULL */
|
|
return this;
|
|
}
|
|
|
|
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->mutex_passenger, NULL);
|
|
pthread_mutex_init(&new_elevator->mutex_state, NULL);
|
|
|
|
LINK_ALL(Elevator, new_elevator,
|
|
runnable,
|
|
get_number_of_passengers,
|
|
can_get_more_passengers,
|
|
get_state,
|
|
set_state,
|
|
repair
|
|
);
|
|
|
|
return new_elevator;
|
|
} |