From fd2463904ae3f0bdc6a2909f0cd0354f341af70b Mon Sep 17 00:00:00 2001 From: klmp200 Date: Mon, 11 Jun 2018 01:58:52 +0200 Subject: [PATCH] BEHOLD ! The threadinator ! --- Building/Building.c | 37 +++++++++++++----------- Building/Building.h | 4 +++ Elevator/Elevator.c | 4 +-- Resident/Resident.c | 8 ++++++ Resident/Resident.h | 1 + SharedData/SharedData.c | 56 ++++++++++++++++++++++++++++++++++++ SharedData/SharedData.h | 4 +++ Visitor/Visitor.c | 9 +++++- Visitor/Visitor.h | 1 + main.c | 64 ++++------------------------------------- 10 files changed, 110 insertions(+), 78 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 2f4b915..f6445e6 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -14,30 +14,28 @@ void remove_end_char(char * string, char character){ string[string_size - 1] = '\0'; } -void split(char * line, int number_to_split, char output[][LINE_BUFFER], char separator){ +List * split(char * line, char * separator){ + List * split = NEW(List); + char * to_delete = strdup(line); + char * to_delete_back = to_delete; char * token = NULL; - char * to_delete = NULL; - char * to_delete_back = NULL; - int i = 0; - to_delete = strdup(line); - to_delete_back = to_delete; - while(i < number_to_split && (token = strsep(&to_delete, &separator)) != NULL){ - strcpy(output[i], token); - i++; - } + while((token = strsep(&to_delete, separator)) != NULL) + split->insert_tail(split, (void*) token, sizeof(char) * strlen(token)); + free(to_delete_back); + return split; } void parse_residents_Building(THIS(Building), char * file){ /* File format is name;appartment_floor;destination */ FILE * f = fopen(file, "r"); + List * line_split = NULL; Resident * resident = NULL; size_t len = LINE_BUFFER; char * line = NULL; char * trash; - char data[3][LINE_BUFFER]; int i = 0; if (f == NULL) @@ -45,12 +43,15 @@ void parse_residents_Building(THIS(Building), char * file){ while (getline(&line, &len, f) > 0) { remove_end_char(line, '\n'); - split(line, 3, data, ';'); + line_split = split(line, ";"); - resident = NEW(Resident, i, data[0], (int) strtol(data[1], &trash, 10), (int) strtol(data[2], &trash, 10)); + resident = NEW(Resident, i, line_split->get_element_data(line_split, 0), + (int) strtol(line_split->get_element_data(line_split, 1), &trash, 10), + (int) strtol(line_split->get_element_data(line_split, 2), &trash, 10)); this->residents->insert_tail(this->residents, resident, sizeof(Resident)); resident->name = NULL; DELETE(resident); + DELETE(line_split); i++; } @@ -61,10 +62,10 @@ void parse_residents_Building(THIS(Building), char * file){ void parse_visitors_Building(THIS(Building), char * file){ /* File format is name;contact_name */ FILE * f = fopen(file, "r"); + List * line_split = NULL; Visitor * visitor = NULL; size_t len = LINE_BUFFER; char * line = NULL; - char data[2][LINE_BUFFER]; int i = 0; if (f == NULL) @@ -72,13 +73,17 @@ void parse_visitors_Building(THIS(Building), char * file){ while (getline(&line, &len, f) > 0) { remove_end_char(line, '\n'); - split(line, 2, data, ';'); - visitor = NEW(Visitor, i, data[0], data[1]); + line_split = split(line, ";"); + + visitor = NEW(Visitor, i, line_split->get_element_data(line_split, 0), + line_split->get_element_data(line_split, 1)); + this->visitors->insert_tail(this->visitors, visitor, sizeof(Visitor)); visitor->contact_name = NULL; visitor->name = NULL; DELETE(visitor); + DELETE(line_split); i++; } diff --git a/Building/Building.h b/Building/Building.h index f29d7eb..4cc4ec2 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -41,6 +41,10 @@ typedef struct o_Building { DESTRUCTOR(Building); } Building; +FRIENDLY(residents, SharedData) +FRIENDLY(visitors, SharedData) +FRIENDLY(elevators, SharedData) + Building *_init_Building(char * residents_file, char * visitors_file); #endif //LO41_BUILDING_H diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 6aa5e73..d6e8895 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -46,8 +46,8 @@ 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; - /* Returning this to keep gcc and clang quiet while developing, will return NULL */ - return this; + printf("Je suis l'ascenseur %s\n", this->name); + return NULL; } Elevator *_init_Elevator(char * name){ diff --git a/Resident/Resident.c b/Resident/Resident.c index 95575c4..4408128 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -9,6 +9,13 @@ GETTER(Resident, char *, name); GETTER(Resident, int, id); GETTER(Resident, int, apartment_floor); +void * runnable_Resident(void * void_this){ + Resident * this = (Resident*) void_this; + printf("Je suis le resident %s et je suis a l'etage %d en direction de l'etage %d\n", + this->name, this->apartment_floor, this->destination); + return NULL; +} + void _free__Resident(THIS(Resident)){ if (this->name != NULL) free(this->name); @@ -27,6 +34,7 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio LINK_ALL(Resident, new_resident, get_name, get_id, + runnable, get_apartment_floor ) diff --git a/Resident/Resident.h b/Resident/Resident.h index 54b4dc7..2dfbaa1 100644 --- a/Resident/Resident.h +++ b/Resident/Resident.h @@ -14,6 +14,7 @@ typedef struct o_Resident { PRIVATE int position; PRIVATE char* name; + PUBLIC void * (*runnable)(void * void_this); PUBLIC char * (*get_name)(_THIS(Resident)); PUBLIC int (*get_id)(_THIS(Resident)); PUBLIC int (*get_apartment_floor)(_THIS(Resident)); diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index 736bdb1..a8c5bac 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -29,6 +29,59 @@ int call_elevator_SharedData(THIS(SharedData), int starting_floor, int destinati return 0; } +/* void * runnable_type(void* void_this) */ + +void start_thread_SharedData(THIS(SharedData), void * (*thread)(void *), void * data, int thread_number){ + pthread_create(&this->threads[thread_number], 0, thread, data); +} + +void start_all_threads_SharedData(THIS(SharedData)){ + int i = 0; + int elevator_index; + Element * current = NULL; + List * residents = NULL; + List * visitors = NULL; + + if (this->threads == NULL){ + if (this->main_building == NULL) + CRASH("No building attached to SharedData and thus no thread can be created\n"); + else { + 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); + + /* 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); + current = current->get_next(current); + i++; + } + + /* starts threading residents */ + current = residents->get_head(residents); + while (current != NULL){ + this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i); + current = current->get_next(current); + i++; + } + + /* starts threading elevators */ + for (elevator_index=0; elevator_indexstart_thread(this, this->main_building->elevators[elevator_index]->runnable, + this->main_building->elevators[elevator_index], i); + i++; + } + } + } +} + +void wait_all_threads_SharedData(THIS(SharedData)){ + int i; + for (i=0; ithreads_nb; i++) + pthread_join(this->threads[i], NULL); +} void _free__SharedData(THIS(SharedData)){ int i; @@ -55,6 +108,9 @@ SharedData *_get_instance_SharedData(){ LINK_ALL(SharedData, new_shared_data, wait_threads, + start_thread, + start_all_threads, + wait_all_threads, set_main_building, get_main_building, get_box, diff --git a/SharedData/SharedData.h b/SharedData/SharedData.h index 0cb96e5..54c660d 100644 --- a/SharedData/SharedData.h +++ b/SharedData/SharedData.h @@ -21,6 +21,10 @@ typedef struct o_SharedData { PRIVATE Building * main_building; PRIVATE CommunicationBox * box; + PRIVATE void (*start_thread)(_THIS(SharedData), void * (*thread)(void *), void * data, int thread_number); + + PUBLIC void (*wait_all_threads)(_THIS(SharedData)); + PUBLIC void (*start_all_threads)(_THIS(SharedData)); PUBLIC void (*set_main_building)(_THIS(SharedData), Building * building); PUBLIC Building * (*get_main_building)(_THIS(SharedData)); PUBLIC CommunicationBox * (*get_box)(_THIS(SharedData)); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 9d4ff93..b18f420 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -8,6 +8,12 @@ GETTER(Visitor, char*, name); GETTER(Visitor, int, id); +void * runnable_Visitor(void * void_this){ + Visitor *this = (Visitor*) void_this; + printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name); + return NULL; +} + void _free__Visitor(THIS(Visitor)){ if (this->name != NULL) free(this->name); @@ -30,7 +36,8 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){ LINK_ALL(Visitor, new_visitor, get_name, - get_id + get_id, + runnable ); return new_visitor; diff --git a/Visitor/Visitor.h b/Visitor/Visitor.h index e1fdc06..6164c4f 100644 --- a/Visitor/Visitor.h +++ b/Visitor/Visitor.h @@ -14,6 +14,7 @@ typedef struct o_Visitor { PRIVATE int position; PRIVATE int destination; + PUBLIC void * (*runnable)(void* void_this); PUBLIC char * (*get_name)(_THIS(Visitor)); PUBLIC int (*get_id)(_THIS(Visitor)); diff --git a/main.c b/main.c index 0fd7dfa..ce712f9 100644 --- a/main.c +++ b/main.c @@ -9,15 +9,6 @@ #include "Visitor/Visitor.h" #include "Resident/Resident.h" -pthread_cond_t cond; - -void * test_sub(void *bite){ - usleep(2000000); - printf("Je relache le programme\n"); - pthread_cond_signal(&cond); - return bite; -} - void clean_exit(int error_code){ printf("Signal %d received, exiting app\n", error_code); DELETE(GET_INSTANCE(SharedData)); @@ -25,57 +16,12 @@ void clean_exit(int error_code){ } int main() { - List *l = NEW(List); - Visitor * roger = NEW(Visitor, 8, "Roger", "Sli"); - Resident * sli = NEW(Resident, 1, "Sli", 2, 2); - char text1[30] = "Patate"; - char text2[30] = "Patator"; - pthread_mutex_t mutex; - pthread_t thread; + SharedData * shared_data = GET_INSTANCE(SharedData); - GET_INSTANCE(SharedData); - signal(SIGINT, clean_exit); + shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt")); + shared_data->start_all_threads(shared_data); + shared_data->wait_all_threads(shared_data); + DELETE(shared_data); - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(&cond, NULL); - - - printf("La taille est de %d\n", l->get_size(l)); - pthread_create(&thread, 0, test_sub, NULL); - printf("FREEEZZZE\n"); - pthread_cond_wait(&cond, &mutex); - pthread_cond_destroy(&cond); - l->insert_tail(l, text1, sizeof(char) * 30); - l->insert_head(l, text2, sizeof(char) * 30); - l->insert_head(l, "Bite", sizeof(char) * 30); - printf("La taille est de %d\n", l->get_size(l)); - printf("%s\n", (char *) l->get_head_data(l)); - printf("%s\n", (char *) l->get_tail_data(l)); - printf("%s\n", (char *) l->get_element_data(l, 0)); - printf("%s\n", (char *) l->get_element_data(l, 1)); - l->insert_inside(l, "Rigolo", sizeof(char) * 30, 2); - printf("%s\n", (char *) l->get_element_data(l, 1)); - printf("%s\n", (char *) l->get_element_data(l, 3)); - l->remove_tail(l); - printf("La taille est de %d\n", l->get_size(l)); - DELETE(l); - - Building *main_building = NEW(Building, "../residents.txt", "../visitors.txt"); - printf("Il y a %d étages dans l'immeuble\n", main_building->floors); - printf("%s est le second visiteur, il veut aller voir %s\n", ((Visitor *) main_building->visitors->get_element_data(main_building->visitors, 1))->name, - ((Visitor *) main_building->visitors->get_element_data(main_building->visitors, 1))->contact_name); - printf("%s est le second resident, il veut aller à l'étage %d\n", ((Resident *) main_building->residents->get_element_data(main_building->residents, 1))->name, - ((Resident *) main_building->residents->get_element_data(main_building->residents, 1))->destination); - for (int i=0; i < ELEVATOR_NB; i++) - printf("Ascenseur %s\n", main_building->elevators[i]->name); - DELETE(main_building); - - printf("Hello, World!\n"); - printf("%s veut rentrer et aller voir %s\n", roger->get_name(roger), roger->contact_name); - printf("%s habite dans l'appartement %d\n", sli->get_name(sli), sli->get_apartment_floor(sli)); - - DELETE(GET_INSTANCE(SharedData)); - DELETE(roger); - DELETE(sli); return 0; } \ No newline at end of file