From 9248f8176b86dba58ecc9fc710a7d6542b4f46c2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 14 Jun 2021 18:33:13 +0200 Subject: [PATCH] More 'objective' definitions --- include/base.h | 5 ++-- include/localworker.h | 2 +- include/scheduler.h | 2 +- src/localworker.c | 4 +-- src/main.c | 66 +++++++++++++++++++++++-------------------- src/scheduler.c | 10 +++---- 6 files changed, 47 insertions(+), 42 deletions(-) diff --git a/include/base.h b/include/base.h index 41d308b..9fa9168 100644 --- a/include/base.h +++ b/include/base.h @@ -54,11 +54,12 @@ struct { int nmaxThread; int nmaxCycles; pthread_t *id; -} typedef SchedulerParams_t; + bool stopped; +} typedef Scheduler_t; struct { pthread_t *id; -} typedef WorkerParams_t; +} typedef Worker_t; // // Local threads diff --git a/include/localworker.h b/include/localworker.h index e2124c6..5b6aaea 100644 --- a/include/localworker.h +++ b/include/localworker.h @@ -26,7 +26,7 @@ // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -pthread_t *WorkerInit(SchedulerParams_t *parameters); +pthread_t *WorkerInit(Worker_t *parameters); // -------------------------------------------------------------------------- // // Worker destructor function // diff --git a/include/scheduler.h b/include/scheduler.h index 5061829..990d9b1 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -26,7 +26,7 @@ // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // -pthread_t *SchedInit(SchedulerParams_t *parameters); +pthread_t *SchedInit(Scheduler_t *parameters); // -------------------------------------------------------------------------- // // Scheduler destructor function // diff --git a/src/localworker.c b/src/localworker.c index 8525b94..528194c 100644 --- a/src/localworker.c +++ b/src/localworker.c @@ -28,7 +28,7 @@ static void *LittleWorker(void *parameters); // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // -pthread_t *WorkerInit(SchedulerParams_t *parameters) +pthread_t *WorkerInit(Scheduler_t *parameters) { parameters->id = (pthread_t*) malloc(sizeof(pthread_t)); pthread_create(parameters->id, NULL, LittleWorker, parameters); @@ -40,7 +40,7 @@ pthread_t *WorkerInit(SchedulerParams_t *parameters) // -------------------------------------------------------------------------- // static void *LittleWorker(void *params) { - WorkerParams_t *parameters = (WorkerParams_t*) params; + Worker_t *parameters = (Worker_t*) params; int a = 4; for (int i = 0; i < 45000; i++) { diff --git a/src/main.c b/src/main.c index 55c0d6b..4717805 100644 --- a/src/main.c +++ b/src/main.c @@ -33,14 +33,14 @@ void SchedulerCrashTest(void) { const int maxthread = 16; - SchedulerParams_t *parameters = - (SchedulerParams_t*) calloc(1, sizeof(SchedulerParams_t)); + Scheduler_t *scheduler0 = + (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); pthread_t **schedThread = (pthread_t**) malloc(sizeof(pthread_t*) * maxthread); for (int i=0; i < maxthread; i++) { - schedThread[i] = SchedInit(parameters); + schedThread[i] = SchedInit(scheduler0); } for (int i=0; i < maxthread; i++) { @@ -48,61 +48,65 @@ void SchedulerCrashTest(void) SchedDestroy(schedThread[i]); } - free(parameters); + free(scheduler0); } int main(int argc, char **argv) { + time_t t; + Scheduler_t *scheduler0; + + // Go! printLog("Starting gem-graph-server...\n"); - time_t t = time(&t); + // + // random generator + // + t = time(&t); srand((unsigned) t); // - // Creating parameters structure for the Scheduler + // Creating structure for the Scheduler // - SchedulerParams_t *parameters = - (SchedulerParams_t*) malloc(sizeof(SchedulerParams_t)); + scheduler0 = (Scheduler_t*) malloc(sizeof(Scheduler_t)); - parameters->globalPreemptionSpace = + scheduler0->globalPreemptionSpace = (BoolArray_t*) malloc(sizeof(BoolArray_t)); - parameters->globalPreemptionSpace->space = + scheduler0->globalPreemptionSpace->space = (bool*) malloc(sizeof(bool)*SPACE_SIZE); - parameters->globalPreemptionSpace->size = SPACE_SIZE; + scheduler0->globalPreemptionSpace->size = SPACE_SIZE; - parameters->globalDrawingSpace = (IntArray_t*) malloc(sizeof(IntArray_t)); - parameters->globalDrawingSpace->space = + scheduler0->globalDrawingSpace = (IntArray_t*) malloc(sizeof(IntArray_t)); + scheduler0->globalDrawingSpace->space = (int*) malloc(sizeof(int)*SPACE_SIZE); - parameters->globalDrawingSpace->size = SPACE_SIZE; + scheduler0->globalDrawingSpace->size = SPACE_SIZE; - parameters->arrowList = (IntArray_t*) malloc(sizeof(IntArray_t)); - parameters->arrowList->space = (int*) malloc(sizeof(int)*ARROW_NUMBER); - parameters->arrowList->size = ARROW_NUMBER; + scheduler0->arrowList = (IntArray_t*) malloc(sizeof(IntArray_t)); + scheduler0->arrowList->space = (int*) malloc(sizeof(int)*ARROW_NUMBER); + scheduler0->arrowList->size = ARROW_NUMBER; - parameters->nmaxThread = MAX_THREAD; - parameters->nmaxCycles = MAX_CYCLES; + scheduler0->nmaxThread = MAX_THREAD; + scheduler0->nmaxCycles = MAX_CYCLES; // // Creating the Scheduler thread // - pthread_t *schedThread = SchedInit(parameters); + SchedInit(scheduler0); - SchedWait(schedThread); + SchedWait(scheduler0->id); - SchedDestroy(schedThread); + SchedDestroy(scheduler0->id); - free(parameters->globalDrawingSpace->space); - free(parameters->globalDrawingSpace); + free(scheduler0->globalDrawingSpace->space); + free(scheduler0->globalDrawingSpace); - free(parameters->globalPreemptionSpace->space); - free(parameters->globalPreemptionSpace); + free(scheduler0->globalPreemptionSpace->space); + free(scheduler0->globalPreemptionSpace); - free(parameters->arrowList->space); - free(parameters->arrowList); + free(scheduler0->arrowList->space); + free(scheduler0->arrowList); - free(parameters); - - //SchedulerCrashTest(); + free(scheduler0); return 0; } diff --git a/src/scheduler.c b/src/scheduler.c index 3da3c45..fa659a5 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -31,7 +31,7 @@ static void *GreatScheduler(void *parameters); // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // -pthread_t *SchedInit(SchedulerParams_t *parameters) +pthread_t *SchedInit(Scheduler_t *parameters) { parameters->id = (pthread_t*) malloc(sizeof(pthread_t)); pthread_create(parameters->id, NULL, GreatScheduler, parameters); @@ -74,7 +74,7 @@ static inline void centerRemove(Center_t *oldCenter) } // -------------------------------------------------------------------------- // -// Scheduler thread main function // +// Scheduler area finder function // // -------------------------------------------------------------------------- // static Center_t *findWorkArea(Center_t *centerList, int ruleRadius, size_t spaceSize) { @@ -102,8 +102,8 @@ static Center_t *findWorkArea(Center_t *centerList, int ruleRadius, size_t space static void *GreatScheduler(void *params) { // récupération des paramètres et annonce - SchedulerParams_t *parameters = (SchedulerParams_t*) params; - printLog("Scheduler id %lu: Bonjour I'm the boss !\n", *(parameters->id)); + Scheduler_t *args = (Scheduler_t*) params; + printLog("Scheduler id %lu: Bonjour I'm the boss !\n", *(args->id)); int ncpu = get_nprocs(); printLog("%d CPUs available.\n", ncpu); @@ -116,7 +116,7 @@ static void *GreatScheduler(void *params) Center_t *workArea; int i = 0; - while (1) { + while (!args->stopped) { while ((workArea = findWorkArea(centersList, 4, 30))) { i++; centerAdd(centersList, workArea);