1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-21 16:23:22 +00:00

Voilà comment gérer ses threads comme un boss

This commit is contained in:
Antoine Bartuccio 2018-06-22 02:50:04 +02:00
parent cf27667313
commit e2beb598a9
Signed by: klmp200
GPG Key ID: E7245548C53F904B
10 changed files with 80 additions and 29 deletions

View File

@ -12,6 +12,7 @@ SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state) SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
SYNCHRONIZED_GETTER(Elevator, int, floor) SYNCHRONIZED_GETTER(Elevator, int, floor)
SYNCHRONIZED_SETTER(Elevator, int, floor) SYNCHRONIZED_SETTER(Elevator, int, floor)
SETTER(Elevator, int, thread_number);
void _free__Elevator(THIS(Elevator)){ void _free__Elevator(THIS(Elevator)){
DELETE(this->passengers); DELETE(this->passengers);
@ -167,6 +168,7 @@ void *runnable_Elevator(void * void_this){
printf("Ascenseur %s : Je suis en route vers l'étage %d\n", this->name, this->target_floor); printf("Ascenseur %s : Je suis en route vers l'étage %d\n", this->name, this->target_floor);
} }
} }
data->unregister_thread(data, this->thread_number);
return NULL; return NULL;
} }
@ -176,6 +178,8 @@ Elevator *_init_Elevator(char * name){
new_elevator->name = strdup(name); new_elevator->name = strdup(name);
new_elevator->passengers = NEW(List); new_elevator->passengers = NEW(List);
new_elevator->target_floor = 0; new_elevator->target_floor = 0;
new_elevator->thread_number = -1;
pthread_mutex_init(&new_elevator->mutex_passengers, NULL); pthread_mutex_init(&new_elevator->mutex_passengers, NULL);
pthread_mutex_init(&new_elevator->mutex_state, NULL); pthread_mutex_init(&new_elevator->mutex_state, NULL);
pthread_mutex_init(&new_elevator->mutex_floor, NULL); pthread_mutex_init(&new_elevator->mutex_floor, NULL);
@ -191,7 +195,8 @@ Elevator *_init_Elevator(char * name){
set_state, set_state,
get_floor, get_floor,
set_floor, set_floor,
repair repair,
set_thread_number
); );
new_elevator->set_floor(new_elevator, 0); new_elevator->set_floor(new_elevator, 0);

View File

@ -21,11 +21,13 @@ typedef struct o_Elevator {
PRIVATE char * name; PRIVATE char * name;
PRIVATE int floor; PRIVATE int floor;
PRIVATE int target_floor; PRIVATE int target_floor;
PRIVATE int thread_number;
PRIVATE pthread_mutex_t mutex_passengers; PRIVATE pthread_mutex_t mutex_passengers;
PRIVATE pthread_mutex_t mutex_state; PRIVATE pthread_mutex_t mutex_state;
PRIVATE pthread_mutex_t mutex_floor; PRIVATE pthread_mutex_t mutex_floor;
PUBLIC void * (*runnable)(void * void_this); PUBLIC void * (*runnable)(void * void_this);
PUBLIC void (*set_thread_number)(_THIS(Elevator), int data);
SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var); SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var);
SYNCHRONIZE PRIVATE void (*set_floor)(_THIS(Elevator), int var); SYNCHRONIZE PRIVATE void (*set_floor)(_THIS(Elevator), int var);

View File

@ -42,6 +42,13 @@ void *runnable_Passenger(void * void_this){
return NULL; return NULL;
} }
void set_thread_number_Passenger(THIS(Passenger), int thread_number){
if (this->type == RESIDENT)
this->resident->set_thread_number(this->resident, thread_number);
if (this->type == VISITOR)
this->visitor->set_thread_number(this->visitor, thread_number);
}
void _free__Passenger(THIS(Passenger)){ void _free__Passenger(THIS(Passenger)){
free(this); free(this);
} }

View File

@ -23,6 +23,7 @@ typedef struct o_Passenger {
PUBLIC int (*get_destination)(_THIS(Passenger)); PUBLIC int (*get_destination)(_THIS(Passenger));
PUBLIC void * (*runnable)(void* void_this); PUBLIC void * (*runnable)(void* void_this);
PUBLIC int (*compare)(void * passenger1, void * passenger2);//yeah I know, but i needed int (*) (void*, void*) PUBLIC int (*compare)(void * passenger1, void * passenger2);//yeah I know, but i needed int (*) (void*, void*)
PUBLIC void (*set_thread_number)(_THIS(Passenger), int thread_number);
DESTRUCTOR(Passenger); DESTRUCTOR(Passenger);
} Passenger; } Passenger;

View File

@ -11,6 +11,7 @@ GETTER(Resident, char *, name);
GETTER(Resident, int, destination); GETTER(Resident, int, destination);
GETTER(Resident, int, id); GETTER(Resident, int, id);
GETTER(Resident, int, apartment_floor); GETTER(Resident, int, apartment_floor);
SETTER(Resident, int, thread_number);
void * runnable_Resident(void * void_this){ void * runnable_Resident(void * void_this){
Resident * this = (Resident*) void_this; Resident * this = (Resident*) void_this;
@ -30,6 +31,7 @@ void * runnable_Resident(void * void_this){
else else
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
data->decrement_active_passengers(data); data->decrement_active_passengers(data);
data->unregister_thread(data, this->thread_number);
return NULL; return NULL;
} }
@ -50,13 +52,15 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio
new_resident->position = new_resident->apartment_floor; new_resident->position = new_resident->apartment_floor;
new_resident->destination = destination; new_resident->destination = destination;
new_resident->passenger = NULL; new_resident->passenger = NULL;
new_resident->thread_number = -1;
LINK_ALL(Resident, new_resident, LINK_ALL(Resident, new_resident,
get_name, get_name,
get_destination, get_destination,
get_id, get_id,
runnable, runnable,
get_apartment_floor get_apartment_floor,
set_thread_number
) )
return new_resident; return new_resident;

View File

@ -9,6 +9,7 @@
typedef struct o_Resident { typedef struct o_Resident {
PRIVATE int id; PRIVATE int id;
PRIVATE int thread_number;
PRIVATE int apartment_floor; PRIVATE int apartment_floor;
PRIVATE int destination; PRIVATE int destination;
PRIVATE int position; PRIVATE int position;
@ -20,6 +21,7 @@ typedef struct o_Resident {
PUBLIC int (*get_id)(_THIS(Resident)); PUBLIC int (*get_id)(_THIS(Resident));
PUBLIC int (*get_apartment_floor)(_THIS(Resident)); PUBLIC int (*get_apartment_floor)(_THIS(Resident));
PUBLIC int (*get_destination)(_THIS(Resident)); PUBLIC int (*get_destination)(_THIS(Resident));
PUBLIC void (*set_thread_number)(_THIS(Resident), int data);
DESTRUCTOR(Resident); DESTRUCTOR(Resident);
} Resident; } Resident;

View File

@ -22,7 +22,7 @@ int call_elevator_SharedData(THIS(SharedData), int starting_floor, int destinati
return 0; return 0;
} }
void start_thread_SharedData(THIS(SharedData), void * (*thread)(void *), void * data, int thread_number){ void start_thread_SharedData(THIS(SharedData), void * (*thread)(void *), void * data, int thread_number){
pthread_create(&this->threads[thread_number], 0, thread, data); pthread_create(this->threads[thread_number], 0, thread, data);
} }
void start_all_threads_SharedData(THIS(SharedData)){ void start_all_threads_SharedData(THIS(SharedData)){
@ -39,18 +39,25 @@ void start_all_threads_SharedData(THIS(SharedData)){
residents = this->main_building->residents; residents = this->main_building->residents;
visitors = this->main_building->visitors; visitors = this->main_building->visitors;
this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB; this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB;
this->threads = malloc_or_die(sizeof(pthread_t) * this->threads_nb); this->threads = malloc_or_die(sizeof(pthread_t*) * this->threads_nb);
for (i=0; i < this->threads_nb; i++)
this->threads[i] = malloc_or_die(sizeof(pthread_t));
pthread_mutex_lock(&this->mutex_active_passengers); pthread_mutex_lock(&this->mutex_active_passengers);
pthread_mutex_lock(&this->mutex_threads);
/* starts threading elevators */ /* starts threading elevators */
for (i=0; i<ELEVATOR_NB; i++) for (i=0; i<ELEVATOR_NB; i++) {
this->start_thread(this, this->main_building->elevators[i]->runnable, this->start_thread(this, this->main_building->elevators[i]->runnable,
this->main_building->elevators[i], i); this->main_building->elevators[i], i);
this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i);
}
/* starts threading visitors */ /* starts threading visitors */
current = visitors->get_head(visitors); current = visitors->get_head(visitors);
while (current != NULL){ while (current != NULL){
this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i); this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i);
((Visitor*)current->get_data(current))->set_thread_number((Visitor*)current->get_data(current), i);
current = current->get_next(current); current = current->get_next(current);
this->active_passengers++; this->active_passengers++;
i++; i++;
@ -60,10 +67,12 @@ void start_all_threads_SharedData(THIS(SharedData)){
current = residents->get_head(residents); current = residents->get_head(residents);
while (current != NULL){ while (current != NULL){
this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i); this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i);
((Resident*)current->get_data(current))->set_thread_number((Resident*)current->get_data(current), i);
current = current->get_next(current); current = current->get_next(current);
this->active_passengers++; this->active_passengers++;
i++; i++;
} }
pthread_mutex_unlock(&this->mutex_threads);
pthread_mutex_unlock(&this->mutex_active_passengers); pthread_mutex_unlock(&this->mutex_active_passengers);
} }
} }
@ -71,8 +80,10 @@ void start_all_threads_SharedData(THIS(SharedData)){
void wait_all_threads_SharedData(THIS(SharedData)){ void wait_all_threads_SharedData(THIS(SharedData)){
int i; int i;
for (i=0; i<this->threads_nb; i++) for (i=0; i<this->threads_nb; i++) {
pthread_join(this->threads[i], NULL); if (this->threads[i] != NULL)
pthread_join(*this->threads[i], NULL);
}
} }
int use_call_box_SharedData(THIS(SharedData), char * resident_name){ int use_call_box_SharedData(THIS(SharedData), char * resident_name){
@ -83,8 +94,11 @@ void _free__SharedData(THIS(SharedData)){
int i; int i;
if (this->threads != NULL){ if (this->threads != NULL){
for (i=0; i<this->threads_nb; i++) { for (i=0; i<this->threads_nb; i++) {
if (this->threads[i] != NULL) {
printf("Quitting thread %d\n", (int) this->threads[i]); printf("Quitting thread %d\n", (int) this->threads[i]);
pthread_kill(this->threads[i], SIGUSR1); pthread_kill(*this->threads[i], SIGUSR1);
}
free(this->threads[i]);
} }
free(this->threads); free(this->threads);
} }
@ -111,6 +125,12 @@ void decrement_active_passengers_SharedData(THIS(SharedData)){
pthread_mutex_unlock(&this->mutex_active_passengers); pthread_mutex_unlock(&this->mutex_active_passengers);
} }
void unregister_thread_SharedData(THIS(SharedData), int thread_number){
pthread_mutex_lock(&this->mutex_threads);
this->threads[thread_number] = NULL;
pthread_mutex_unlock(&this->mutex_threads);
}
SharedData *_get_instance_SharedData(){ SharedData *_get_instance_SharedData(){
static SharedData * new_shared_data = NULL; static SharedData * new_shared_data = NULL;
if (new_shared_data == NULL){ if (new_shared_data == NULL){
@ -122,6 +142,7 @@ SharedData *_get_instance_SharedData(){
new_shared_data->active_passengers = 0; new_shared_data->active_passengers = 0;
pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL); pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL);
pthread_mutex_init(&new_shared_data->mutex_threads, NULL);
LINK_ALL(SharedData, new_shared_data, LINK_ALL(SharedData, new_shared_data,
start_thread, start_thread,
@ -132,7 +153,8 @@ SharedData *_get_instance_SharedData(){
use_call_box, use_call_box,
call_elevator, call_elevator,
decrement_active_passengers, decrement_active_passengers,
is_active_passengers_left is_active_passengers_left,
unregister_thread
); );
} }
return new_shared_data; return new_shared_data;

View File

@ -12,9 +12,10 @@
typedef struct o_SharedData { typedef struct o_SharedData {
PRIVATE int threads_nb; PRIVATE int threads_nb;
PRIVATE int active_passengers; PRIVATE int active_passengers;
PRIVATE pthread_t *threads; PRIVATE pthread_t ** threads;
PRIVATE Building * main_building; PRIVATE Building * main_building;
PRIVATE pthread_mutex_t mutex_active_passengers; PRIVATE pthread_mutex_t mutex_active_passengers;
PRIVATE pthread_mutex_t mutex_threads;
PRIVATE void (*start_thread)(_THIS(SharedData), void * (*thread)(void *), void * data, int thread_number); PRIVATE void (*start_thread)(_THIS(SharedData), void * (*thread)(void *), void * data, int thread_number);
@ -26,6 +27,7 @@ typedef struct o_SharedData {
PUBLIC int (*use_call_box)(_THIS(SharedData), char * resident_name); PUBLIC int (*use_call_box)(_THIS(SharedData), char * resident_name);
SYNCHRONIZE PUBLIC void (*decrement_active_passengers)(_THIS(SharedData)); SYNCHRONIZE PUBLIC void (*decrement_active_passengers)(_THIS(SharedData));
SYNCHRONIZE PUBLIC int (*is_active_passengers_left)(_THIS(SharedData)); SYNCHRONIZE PUBLIC int (*is_active_passengers_left)(_THIS(SharedData));
SYNCHRONIZE PUBLIC void (*unregister_thread)(_THIS(SharedData), int thread_number);
DESTRUCTOR(SharedData); DESTRUCTOR(SharedData);
} SharedData; } SharedData;

View File

@ -9,6 +9,7 @@
GETTER(Visitor, char*, name); GETTER(Visitor, char*, name);
GETTER(Visitor, int, destination); GETTER(Visitor, int, destination);
GETTER(Visitor, int, id); GETTER(Visitor, int, id);
SETTER(Visitor, int, thread_number);
void * runnable_Visitor(void * void_this){ void * runnable_Visitor(void * void_this){
Visitor *this = (Visitor*) void_this; Visitor *this = (Visitor*) void_this;
@ -24,6 +25,7 @@ void * runnable_Visitor(void * void_this){
printf("Visiteur %s : J'apelle à l'interphone\nVisiteur %s : J'apprends que %s habite à l'étage %d\n", this->name, this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name))); printf("Visiteur %s : J'apelle à l'interphone\nVisiteur %s : J'apprends que %s habite à l'étage %d\n", this->name, this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name)));
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
data->decrement_active_passengers(data); data->decrement_active_passengers(data);
data->unregister_thread(data, this->thread_number);
return NULL; return NULL;
} }
@ -44,6 +46,7 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){
new_visitor->position = 0; new_visitor->position = 0;
new_visitor->destination = -1; new_visitor->destination = -1;
new_visitor->passenger = NULL; new_visitor->passenger = NULL;
new_visitor->thread_number = -1;
if (contact_name != NULL) if (contact_name != NULL)
new_visitor->contact_name = strdup(contact_name); new_visitor->contact_name = strdup(contact_name);
@ -54,7 +57,8 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){
get_name, get_name,
get_destination, get_destination,
get_id, get_id,
runnable runnable,
set_thread_number
); );
return new_visitor; return new_visitor;

View File

@ -9,6 +9,7 @@
typedef struct o_Visitor { typedef struct o_Visitor {
PRIVATE int id; PRIVATE int id;
PRIVATE int thread_number;
PRIVATE char * name; PRIVATE char * name;
PRIVATE char * contact_name; PRIVATE char * contact_name;
PRIVATE int position; PRIVATE int position;
@ -19,6 +20,7 @@ typedef struct o_Visitor {
PUBLIC char * (*get_name)(_THIS(Visitor)); PUBLIC char * (*get_name)(_THIS(Visitor));
PUBLIC int (*get_id)(_THIS(Visitor)); PUBLIC int (*get_id)(_THIS(Visitor));
PUBLIC int (*get_destination)(_THIS(Visitor)); PUBLIC int (*get_destination)(_THIS(Visitor));
PUBLIC void (*set_thread_number)(_THIS(Visitor), int data);
DESTRUCTOR(Visitor); DESTRUCTOR(Visitor);
} Visitor; } Visitor;