1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-24 10:04:21 +00:00

BEHOLD ! The threadinator !

This commit is contained in:
Antoine Bartuccio 2018-06-11 01:58:52 +02:00
parent bd5860579e
commit fd2463904a
Signed by: klmp200
GPG Key ID: E7245548C53F904B
10 changed files with 110 additions and 78 deletions

View File

@ -14,30 +14,28 @@ void remove_end_char(char * string, char character){
string[string_size - 1] = '\0'; 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 * token = NULL;
char * to_delete = NULL;
char * to_delete_back = NULL;
int i = 0;
to_delete = strdup(line); while((token = strsep(&to_delete, separator)) != NULL)
to_delete_back = to_delete; split->insert_tail(split, (void*) token, sizeof(char) * strlen(token));
while(i < number_to_split && (token = strsep(&to_delete, &separator)) != NULL){
strcpy(output[i], token);
i++;
}
free(to_delete_back); free(to_delete_back);
return split;
} }
void parse_residents_Building(THIS(Building), char * file){ void parse_residents_Building(THIS(Building), char * file){
/* File format is name;appartment_floor;destination */ /* File format is name;appartment_floor;destination */
FILE * f = fopen(file, "r"); FILE * f = fopen(file, "r");
List * line_split = NULL;
Resident * resident = NULL; Resident * resident = NULL;
size_t len = LINE_BUFFER; size_t len = LINE_BUFFER;
char * line = NULL; char * line = NULL;
char * trash; char * trash;
char data[3][LINE_BUFFER];
int i = 0; int i = 0;
if (f == NULL) if (f == NULL)
@ -45,12 +43,15 @@ void parse_residents_Building(THIS(Building), char * file){
while (getline(&line, &len, f) > 0) { while (getline(&line, &len, f) > 0) {
remove_end_char(line, '\n'); 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)); this->residents->insert_tail(this->residents, resident, sizeof(Resident));
resident->name = NULL; resident->name = NULL;
DELETE(resident); DELETE(resident);
DELETE(line_split);
i++; i++;
} }
@ -61,10 +62,10 @@ void parse_residents_Building(THIS(Building), char * file){
void parse_visitors_Building(THIS(Building), char * file){ void parse_visitors_Building(THIS(Building), char * file){
/* File format is name;contact_name */ /* File format is name;contact_name */
FILE * f = fopen(file, "r"); FILE * f = fopen(file, "r");
List * line_split = NULL;
Visitor * visitor = NULL; Visitor * visitor = NULL;
size_t len = LINE_BUFFER; size_t len = LINE_BUFFER;
char * line = NULL; char * line = NULL;
char data[2][LINE_BUFFER];
int i = 0; int i = 0;
if (f == NULL) if (f == NULL)
@ -72,13 +73,17 @@ void parse_visitors_Building(THIS(Building), char * file){
while (getline(&line, &len, f) > 0) { while (getline(&line, &len, f) > 0) {
remove_end_char(line, '\n'); 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)); this->visitors->insert_tail(this->visitors, visitor, sizeof(Visitor));
visitor->contact_name = NULL; visitor->contact_name = NULL;
visitor->name = NULL; visitor->name = NULL;
DELETE(visitor); DELETE(visitor);
DELETE(line_split);
i++; i++;
} }

View File

@ -41,6 +41,10 @@ typedef struct o_Building {
DESTRUCTOR(Building); DESTRUCTOR(Building);
} Building; } Building;
FRIENDLY(residents, SharedData)
FRIENDLY(visitors, SharedData)
FRIENDLY(elevators, SharedData)
Building *_init_Building(char * residents_file, char * visitors_file); Building *_init_Building(char * residents_file, char * visitors_file);
#endif //LO41_BUILDING_H #endif //LO41_BUILDING_H

View File

@ -46,8 +46,8 @@ void repair_Elevator(THIS(Elevator)){
void *runnable_Elevator(void * void_this){ void *runnable_Elevator(void * void_this){
/* This is where the thread logic will be implemented */ /* This is where the thread logic will be implemented */
Elevator * this = (Elevator*) void_this; Elevator * this = (Elevator*) void_this;
/* Returning this to keep gcc and clang quiet while developing, will return NULL */ printf("Je suis l'ascenseur %s\n", this->name);
return this; return NULL;
} }
Elevator *_init_Elevator(char * name){ Elevator *_init_Elevator(char * name){

View File

@ -9,6 +9,13 @@ GETTER(Resident, char *, name);
GETTER(Resident, int, id); GETTER(Resident, int, id);
GETTER(Resident, int, apartment_floor); 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)){ void _free__Resident(THIS(Resident)){
if (this->name != NULL) if (this->name != NULL)
free(this->name); free(this->name);
@ -27,6 +34,7 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio
LINK_ALL(Resident, new_resident, LINK_ALL(Resident, new_resident,
get_name, get_name,
get_id, get_id,
runnable,
get_apartment_floor get_apartment_floor
) )

View File

@ -14,6 +14,7 @@ typedef struct o_Resident {
PRIVATE int position; PRIVATE int position;
PRIVATE char* name; PRIVATE char* name;
PUBLIC void * (*runnable)(void * void_this);
PUBLIC char * (*get_name)(_THIS(Resident)); PUBLIC char * (*get_name)(_THIS(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));

View File

@ -29,6 +29,59 @@ int call_elevator_SharedData(THIS(SharedData), int starting_floor, int destinati
return 0; 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_index<ELEVATOR_NB; elevator_index++){
this->start_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; i<this->threads_nb; i++)
pthread_join(this->threads[i], NULL);
}
void _free__SharedData(THIS(SharedData)){ void _free__SharedData(THIS(SharedData)){
int i; int i;
@ -55,6 +108,9 @@ SharedData *_get_instance_SharedData(){
LINK_ALL(SharedData, new_shared_data, LINK_ALL(SharedData, new_shared_data,
wait_threads, wait_threads,
start_thread,
start_all_threads,
wait_all_threads,
set_main_building, set_main_building,
get_main_building, get_main_building,
get_box, get_box,

View File

@ -21,6 +21,10 @@ typedef struct o_SharedData {
PRIVATE Building * main_building; PRIVATE Building * main_building;
PRIVATE CommunicationBox * box; 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 void (*set_main_building)(_THIS(SharedData), Building * building);
PUBLIC Building * (*get_main_building)(_THIS(SharedData)); PUBLIC Building * (*get_main_building)(_THIS(SharedData));
PUBLIC CommunicationBox * (*get_box)(_THIS(SharedData)); PUBLIC CommunicationBox * (*get_box)(_THIS(SharedData));

View File

@ -8,6 +8,12 @@
GETTER(Visitor, char*, name); GETTER(Visitor, char*, name);
GETTER(Visitor, int, id); 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)){ void _free__Visitor(THIS(Visitor)){
if (this->name != NULL) if (this->name != NULL)
free(this->name); free(this->name);
@ -30,7 +36,8 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){
LINK_ALL(Visitor, new_visitor, LINK_ALL(Visitor, new_visitor,
get_name, get_name,
get_id get_id,
runnable
); );
return new_visitor; return new_visitor;

View File

@ -14,6 +14,7 @@ typedef struct o_Visitor {
PRIVATE int position; PRIVATE int position;
PRIVATE int destination; PRIVATE int destination;
PUBLIC void * (*runnable)(void* void_this);
PUBLIC char * (*get_name)(_THIS(Visitor)); PUBLIC char * (*get_name)(_THIS(Visitor));
PUBLIC int (*get_id)(_THIS(Visitor)); PUBLIC int (*get_id)(_THIS(Visitor));

64
main.c
View File

@ -9,15 +9,6 @@
#include "Visitor/Visitor.h" #include "Visitor/Visitor.h"
#include "Resident/Resident.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){ void clean_exit(int error_code){
printf("Signal %d received, exiting app\n", error_code); printf("Signal %d received, exiting app\n", error_code);
DELETE(GET_INSTANCE(SharedData)); DELETE(GET_INSTANCE(SharedData));
@ -25,57 +16,12 @@ void clean_exit(int error_code){
} }
int main() { int main() {
List *l = NEW(List); SharedData * shared_data = GET_INSTANCE(SharedData);
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;
GET_INSTANCE(SharedData); shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt"));
signal(SIGINT, clean_exit); 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; return 0;
} }