mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-22 00:33:22 +00:00
Encore des mutex, le début des threads aussi
This commit is contained in:
parent
de64ae4902
commit
048017a1ba
@ -5,37 +5,61 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "Elevator.h"
|
#include "Elevator.h"
|
||||||
|
|
||||||
|
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
|
||||||
|
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
|
||||||
|
|
||||||
void _free__Elevator(THIS(Elevator)){
|
void _free__Elevator(THIS(Elevator)){
|
||||||
DELETE(this->passenger_ids);
|
DELETE(this->passenger_ids);
|
||||||
if (this->name != NULL)
|
if (this->name != NULL)
|
||||||
free(this->name);
|
free(this->name);
|
||||||
pthread_mutex_unlock(&this->passenger_mutex);
|
|
||||||
pthread_mutex_destroy(&this->passenger_mutex);
|
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);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
||||||
int num;
|
int num;
|
||||||
pthread_mutex_lock(&this->passenger_mutex);
|
pthread_mutex_lock(&this->mutex_passenger);
|
||||||
num = this->passenger_ids->get_size(this->passenger_ids);
|
num = this->passenger_ids->get_size(this->passenger_ids);
|
||||||
pthread_mutex_lock(&this->passenger_mutex);
|
pthread_mutex_lock(&this->mutex_passenger);
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
int can_get_more_passengers_Elevator(_THIS(Elevator)){
|
int can_get_more_passengers_Elevator(THIS(Elevator)){
|
||||||
return (this->get_number_of_passengers(this) < MAX_ELEVATOR_CAPACITY);
|
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 *_init_Elevator(char * name){
|
||||||
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
||||||
new_elevator->state = waiting;
|
new_elevator->state = waiting;
|
||||||
new_elevator->name = strdup(name);
|
new_elevator->name = strdup(name);
|
||||||
new_elevator->passenger_ids = NEW(List);
|
new_elevator->passenger_ids = NEW(List);
|
||||||
pthread_mutex_init(&new_elevator->passenger_mutex, NULL);
|
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
||||||
|
pthread_mutex_init(&new_elevator->mutex_state, NULL);
|
||||||
|
|
||||||
LINK_ALL(Elevator, new_elevator,
|
LINK_ALL(Elevator, new_elevator,
|
||||||
|
runnable,
|
||||||
get_number_of_passengers,
|
get_number_of_passengers,
|
||||||
can_get_more_passengers
|
can_get_more_passengers,
|
||||||
|
get_state,
|
||||||
|
set_state,
|
||||||
|
repair
|
||||||
);
|
);
|
||||||
|
|
||||||
return new_elevator;
|
return new_elevator;
|
||||||
|
@ -11,17 +11,25 @@
|
|||||||
|
|
||||||
#define MAX_ELEVATOR_CAPACITY 10
|
#define MAX_ELEVATOR_CAPACITY 10
|
||||||
|
|
||||||
typedef enum {running, waiting, sleeping} ELEVATOR_STATE;
|
typedef enum {running, waiting, sleeping, broken} ELEVATOR_STATE;
|
||||||
|
|
||||||
typedef struct o_Elevator {
|
typedef struct o_Elevator {
|
||||||
PRIVATE ELEVATOR_STATE state;
|
PRIVATE ELEVATOR_STATE state;
|
||||||
PRIVATE List * passenger_ids;
|
PRIVATE List * passenger_ids;
|
||||||
PRIVATE char * name;
|
PRIVATE char * name;
|
||||||
PRIVATE pthread_mutex_t passenger_mutex;
|
PRIVATE pthread_mutex_t mutex_passenger;
|
||||||
|
PRIVATE pthread_mutex_t mutex_state;
|
||||||
|
|
||||||
|
PUBLIC void * (*runnable)(void * void_this);
|
||||||
|
|
||||||
|
SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var);
|
||||||
|
|
||||||
|
SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
|
||||||
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
|
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
|
||||||
|
SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator));
|
||||||
SYNCHRONIZE PUBLIC int (*can_get_more_passengers)(_THIS(Elevator));
|
SYNCHRONIZE PUBLIC int (*can_get_more_passengers)(_THIS(Elevator));
|
||||||
DESTRUCTOR(Elevator);
|
DESTRUCTOR(Elevator);
|
||||||
|
|
||||||
} Elevator;
|
} Elevator;
|
||||||
|
|
||||||
FRIENDLY(state, Building)
|
FRIENDLY(state, Building)
|
||||||
|
11
Objects.h
11
Objects.h
@ -29,6 +29,17 @@
|
|||||||
#define SETTER(obj_type, variable_type, variable) void set_##variable##_##obj_type(obj_type *this, variable_type var){\
|
#define SETTER(obj_type, variable_type, variable) void set_##variable##_##obj_type(obj_type *this, variable_type var){\
|
||||||
this->variable = var; }
|
this->variable = var; }
|
||||||
|
|
||||||
|
#define SYNCHRONIZED_GETTER(obj_type, variable_type, variable) variable_type get_##variable##_##obj_type(obj_type *this){\
|
||||||
|
variable_type tmp_variable; \
|
||||||
|
pthread_mutex_lock(&this->mutex_##variable); \
|
||||||
|
tmp_variable = this->variable; \
|
||||||
|
pthread_mutex_unlock(&this->mutex_##variable); \
|
||||||
|
return tmp_variable; }
|
||||||
|
#define SYNCHRONIZED_SETTER(obj_type, variable_type, variable) void set_##variable##_##obj_type(obj_type *this, variable_type var){\
|
||||||
|
pthread_mutex_lock(&this->mutex_##variable); \
|
||||||
|
this->variable = var; \
|
||||||
|
pthread_mutex_unlock(&this->mutex_##variable);}
|
||||||
|
|
||||||
void * malloc_or_die(size_t size);
|
void * malloc_or_die(size_t size);
|
||||||
|
|
||||||
/* Link all method from variadic list, support max 20 args */
|
/* Link all method from variadic list, support max 20 args */
|
||||||
|
Loading…
Reference in New Issue
Block a user