mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-15 14:19:24 +00:00
Voilà comment gérer ses threads comme un boss
This commit is contained in:
@ -22,7 +22,7 @@ int call_elevator_SharedData(THIS(SharedData), int starting_floor, int destinati
|
||||
return 0;
|
||||
}
|
||||
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)){
|
||||
@ -39,18 +39,25 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
residents = this->main_building->residents;
|
||||
visitors = this->main_building->visitors;
|
||||
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_threads);
|
||||
/* 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->main_building->elevators[i], i);
|
||||
this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i);
|
||||
}
|
||||
|
||||
/* starts threading visitors */
|
||||
current = visitors->get_head(visitors);
|
||||
while (current != NULL){
|
||||
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);
|
||||
this->active_passengers++;
|
||||
i++;
|
||||
@ -60,10 +67,12 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
current = residents->get_head(residents);
|
||||
while (current != NULL){
|
||||
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);
|
||||
this->active_passengers++;
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_unlock(&this->mutex_threads);
|
||||
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)){
|
||||
int i;
|
||||
for (i=0; i<this->threads_nb; i++)
|
||||
pthread_join(this->threads[i], NULL);
|
||||
for (i=0; i<this->threads_nb; i++) {
|
||||
if (this->threads[i] != NULL)
|
||||
pthread_join(*this->threads[i], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int use_call_box_SharedData(THIS(SharedData), char * resident_name){
|
||||
@ -83,8 +94,11 @@ void _free__SharedData(THIS(SharedData)){
|
||||
int i;
|
||||
if (this->threads != NULL){
|
||||
for (i=0; i<this->threads_nb; i++) {
|
||||
printf("Quitting thread %d\n", (int) this->threads[i]);
|
||||
pthread_kill(this->threads[i], SIGUSR1);
|
||||
if (this->threads[i] != NULL) {
|
||||
printf("Quitting thread %d\n", (int) this->threads[i]);
|
||||
pthread_kill(*this->threads[i], SIGUSR1);
|
||||
}
|
||||
free(this->threads[i]);
|
||||
}
|
||||
free(this->threads);
|
||||
}
|
||||
@ -111,6 +125,12 @@ void decrement_active_passengers_SharedData(THIS(SharedData)){
|
||||
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(){
|
||||
static SharedData * new_shared_data = NULL;
|
||||
if (new_shared_data == NULL){
|
||||
@ -122,6 +142,7 @@ SharedData *_get_instance_SharedData(){
|
||||
new_shared_data->active_passengers = 0;
|
||||
|
||||
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,
|
||||
start_thread,
|
||||
@ -132,7 +153,8 @@ SharedData *_get_instance_SharedData(){
|
||||
use_call_box,
|
||||
call_elevator,
|
||||
decrement_active_passengers,
|
||||
is_active_passengers_left
|
||||
is_active_passengers_left,
|
||||
unregister_thread
|
||||
);
|
||||
}
|
||||
return new_shared_data;
|
||||
|
@ -12,9 +12,10 @@
|
||||
typedef struct o_SharedData {
|
||||
PRIVATE int threads_nb;
|
||||
PRIVATE int active_passengers;
|
||||
PRIVATE pthread_t *threads;
|
||||
PRIVATE pthread_t ** threads;
|
||||
PRIVATE Building * main_building;
|
||||
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);
|
||||
|
||||
@ -26,6 +27,7 @@ typedef struct o_SharedData {
|
||||
PUBLIC int (*use_call_box)(_THIS(SharedData), char * resident_name);
|
||||
SYNCHRONIZE PUBLIC void (*decrement_active_passengers)(_THIS(SharedData));
|
||||
SYNCHRONIZE PUBLIC int (*is_active_passengers_left)(_THIS(SharedData));
|
||||
SYNCHRONIZE PUBLIC void (*unregister_thread)(_THIS(SharedData), int thread_number);
|
||||
|
||||
DESTRUCTOR(SharedData);
|
||||
} SharedData;
|
||||
|
Reference in New Issue
Block a user