mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-11 04:09:24 +00:00
Petit refactor avec des signaux
This commit is contained in:
@ -27,6 +27,16 @@ void _free__Elevator(THIS(Elevator)){
|
||||
free(this);
|
||||
}
|
||||
|
||||
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));
|
||||
if (this->passenger_ids->get_size(this->passenger_ids) >= MAX_ELEVATOR_CAPACITY)
|
||||
this->set_state(this, SLEEPING);
|
||||
pthread_mutex_unlock(&this->mutex_passenger);
|
||||
}
|
||||
|
||||
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
||||
int num;
|
||||
pthread_mutex_lock(&this->mutex_passenger);
|
||||
@ -40,7 +50,7 @@ int can_get_more_passengers_Elevator(THIS(Elevator)){
|
||||
}
|
||||
|
||||
void repair_Elevator(THIS(Elevator)){
|
||||
this->set_state(this, running);
|
||||
this->set_state(this, RUNNING);
|
||||
}
|
||||
|
||||
void *runnable_Elevator(void * void_this){
|
||||
@ -52,7 +62,7 @@ void *runnable_Elevator(void * void_this){
|
||||
|
||||
Elevator *_init_Elevator(char * name){
|
||||
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
||||
new_elevator->state = waiting;
|
||||
new_elevator->state = WAITING;
|
||||
new_elevator->name = strdup(name);
|
||||
new_elevator->passenger_ids = NEW(List);
|
||||
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
||||
@ -63,6 +73,7 @@ Elevator *_init_Elevator(char * name){
|
||||
runnable,
|
||||
get_number_of_passengers,
|
||||
can_get_more_passengers,
|
||||
add_passenger,
|
||||
get_state,
|
||||
set_state,
|
||||
get_floor,
|
||||
|
@ -9,9 +9,10 @@
|
||||
#include "../Objects.h"
|
||||
#include "../List/List.h"
|
||||
|
||||
#define MAX_ELEVATOR_CAPACITY 10
|
||||
//#define MAX_ELEVATOR_CAPACITY 10
|
||||
#define MAX_ELEVATOR_CAPACITY 3
|
||||
|
||||
typedef enum {running, waiting, sleeping, broken} ELEVATOR_STATE;
|
||||
typedef enum {RUNNING, WAITING, SLEEPING, BROKEN} ELEVATOR_STATE;
|
||||
|
||||
typedef struct o_Elevator {
|
||||
PRIVATE ELEVATOR_STATE state;
|
||||
@ -29,6 +30,7 @@ typedef struct o_Elevator {
|
||||
|
||||
SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(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));
|
||||
|
Reference in New Issue
Block a user