mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-11 12:19:25 +00:00
Ça rentre
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include "Elevator.h"
|
||||
#include "../SharedData/SharedData.h"
|
||||
|
||||
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
|
||||
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
|
||||
@ -30,8 +31,8 @@ void _free__Elevator(THIS(Elevator)){
|
||||
void add_passenger_Elevator(THIS(Elevator), int passenger_id){
|
||||
pthread_mutex_lock(&this->mutex_passenger);
|
||||
this->passenger_ids->insert_tail(this->passenger_ids, ((void *)&passenger_id), sizeof(int));
|
||||
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers\n",
|
||||
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids));
|
||||
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n",
|
||||
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids), this->name);
|
||||
if (this->passenger_ids->get_size(this->passenger_ids) >= MAX_ELEVATOR_CAPACITY)
|
||||
this->set_state(this, SLEEPING);
|
||||
pthread_mutex_unlock(&this->mutex_passenger);
|
||||
@ -45,8 +46,20 @@ int get_number_of_passengers_Elevator(THIS(Elevator)){
|
||||
return num;
|
||||
}
|
||||
|
||||
int can_get_more_passengers_Elevator(THIS(Elevator)){
|
||||
return (this->get_number_of_passengers(this) < MAX_ELEVATOR_CAPACITY);
|
||||
int can_get_inside_Elevator(THIS(Elevator), int floor){
|
||||
int permission;
|
||||
pthread_mutex_lock(&this->mutex_passenger);
|
||||
pthread_mutex_lock(&this->mutex_state);
|
||||
pthread_mutex_lock(&this->mutex_floor);
|
||||
|
||||
permission = (this->passenger_ids->get_size(this->passenger_ids) < MAX_ELEVATOR_CAPACITY &&
|
||||
this->state == WAITING && this->floor == floor);
|
||||
|
||||
pthread_mutex_unlock(&this->mutex_floor);
|
||||
pthread_mutex_unlock(&this->mutex_state);
|
||||
pthread_mutex_unlock(&this->mutex_passenger);
|
||||
|
||||
return permission;
|
||||
}
|
||||
|
||||
void repair_Elevator(THIS(Elevator)){
|
||||
@ -56,7 +69,11 @@ void repair_Elevator(THIS(Elevator)){
|
||||
void *runnable_Elevator(void * void_this){
|
||||
/* This is where the thread logic will be implemented */
|
||||
Elevator * this = (Elevator*) void_this;
|
||||
SharedData * data = GET_INSTANCE(SharedData);
|
||||
|
||||
printf("Je suis l'ascenseur %s\n", this->name);
|
||||
for (;;)
|
||||
data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -72,7 +89,7 @@ Elevator *_init_Elevator(char * name){
|
||||
LINK_ALL(Elevator, new_elevator,
|
||||
runnable,
|
||||
get_number_of_passengers,
|
||||
can_get_more_passengers,
|
||||
can_get_inside,
|
||||
add_passenger,
|
||||
get_state,
|
||||
set_state,
|
||||
|
@ -33,7 +33,7 @@ typedef struct o_Elevator {
|
||||
SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), int passenger_id);
|
||||
SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*can_get_more_passengers)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*can_get_inside)(_THIS(Elevator), int floor);
|
||||
DESTRUCTOR(Elevator);
|
||||
|
||||
} Elevator;
|
||||
|
Reference in New Issue
Block a user