From cdacbc0fb6c93c2d67e64b5caea5d94485113861 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 23 Jun 2021 09:13:46 +0200 Subject: [PATCH] locked arrowArray in scheduler! --- Makefile | 2 +- include/base.h | 6 +++--- include/model.h | 5 +++++ include/scheduler.h | 4 ++-- src/arrows.c | 2 ++ src/cmds.c | 22 +++++----------------- src/model.c | 19 ++++++++++--------- src/scheduler.c | 39 ++++++++++++++++++++++++++------------- 8 files changed, 54 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index b70e681..6a7c510 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ SRCDIR=src DEBDIR=debian SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/localworker.o \ $(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \ - $(BINDIR)/main.o + $(BINDIR)/main.o $(BINDIR)/arrows.o CLIOBJ= $(BINDIR)/cli.o .DEFAULT_GOAL:= all diff --git a/include/base.h b/include/base.h index ae7a8d5..ee5acda 100644 --- a/include/base.h +++ b/include/base.h @@ -45,7 +45,7 @@ struct { struct { size_t size; - bool lock; + pthread_spinlock_t lock; Arrow_t *array; } typedef ArrowArray_t; @@ -81,7 +81,7 @@ struct { struct { Space_t *globalDrawingSpace; IntArray_t *transitionsTree; - ArrowArray_t *arrowList; + ArrowArray_t *arrowArray; int nmaxThread; int nmaxCycles; int ruleRadius; @@ -108,7 +108,7 @@ struct { Center_t *localWorkAreaCenter; Space_t *globalDrawingSpace; IntArray_t *transitionsTree; - ArrowArray_t *arrowList; + ArrowArray_t *arrowArray; bool pleaseStop; bool terminated; int returnValue; diff --git a/include/model.h b/include/model.h index 4256da6..f63decc 100644 --- a/include/model.h +++ b/include/model.h @@ -24,3 +24,8 @@ #endif void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model); + +void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowList, + Model_t *model); + + diff --git a/include/scheduler.h b/include/scheduler.h index 573a81f..cb5eb40 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -37,8 +37,8 @@ static inline void SchedDestroy(Scheduler_t *scheduler) free(scheduler->globalDrawingSpace->space); free(scheduler->globalDrawingSpace); - free(scheduler->arrowList->array); - free(scheduler->arrowList); + free(scheduler->arrowArray->array); + free(scheduler->arrowArray); } // -------------------------------------------------------------------------- // diff --git a/src/arrows.c b/src/arrows.c index 290079a..0e6419e 100644 --- a/src/arrows.c +++ b/src/arrows.c @@ -22,3 +22,5 @@ #include "../include/base.h" /* -------------------------------------------------------------------------- */ + + diff --git a/src/cmds.c b/src/cmds.c index 9faa487..da1d1b6 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -48,27 +48,15 @@ void *launchScheduler(void *args) // Creating structure for the model model0 = (Model_t*) calloc(1, sizeof(Model_t)); + model0->space_xmax = XMAX; + model0->siteNumber = SITE_NUMBER; + // Preparing global drawing space ModelPrepareSpace(scheduler0->globalDrawingSpace, model0); - scheduler0->arrowList = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); + scheduler0->arrowArray = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); - printLog("Populating a random arrow list...\n"); - - for (int i = 0; i < ARROW_NUMBER; i++) { - - if (scheduler0->globalDrawingSpace->xmax) - scheduler0->arrowList->array[i].x = - rand() % (scheduler0->globalDrawingSpace->xmax + 1); - - if (scheduler0->globalDrawingSpace->ymax) - scheduler0->arrowList->array[i].y = - rand() % (scheduler0->globalDrawingSpace->ymax + 1); - - if (scheduler0->globalDrawingSpace->zmax) - scheduler0->arrowList->array[i].z = - rand() % (scheduler0->globalDrawingSpace->zmax + 1); - } + ModelPrepareArrows(scheduler0->globalDrawingSpace, scheduler0->arrowArray, model0); scheduler0->nmaxThread = MAX_THREAD; scheduler0->nmaxCycles = MAX_CYCLES; diff --git a/src/model.c b/src/model.c index ba01a2f..80153a4 100644 --- a/src/model.c +++ b/src/model.c @@ -20,6 +20,9 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" +#include "../include/arrows.h" + +static Model_t *loadedModels; void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model) { @@ -37,20 +40,18 @@ void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model) } } -void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowList, +void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowArray, Model_t *model) { -#define ARROW_NUMBER 6 - - arrowList->array = (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); - arrowList->size = ARROW_NUMBER; + arrowArray->array = (Arrow_t*) calloc(6, sizeof(Arrow_t)); + arrowArray->size = 6; // Creating some arrows globalDrawingSpace->space[3].sites[1].narrow = 1; - arrowList->array[0].siteId = 1; - arrowList->array[0].x = 3; + arrowArray->array[0].siteId = 1; + arrowArray->array[0].x = 3; globalDrawingSpace->space[4].sites[0].narrow = 1; - arrowList->array[1].siteId = 0; - arrowList->array[1].x = 4; + arrowArray->array[1].siteId = 0; + arrowArray->array[1].x = 4; } diff --git a/src/scheduler.c b/src/scheduler.c index 68d17a4..aea2f34 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -22,6 +22,7 @@ #include "../include/base.h" #include "../include/centers.h" #include "../include/localworker.h" +#include "../include/arrows.h" #include @@ -85,7 +86,7 @@ static void *schedulerMain(void *scheduler) // A cycle is defined as the total renew of all allocated workers. // 1 cycle = thread * partial cycle. int nPartialCycles; - int ncpu, nworker; + int ncpu, nworker, err; // Getting scheduler argument structure args = (Scheduler_t*) scheduler; @@ -104,6 +105,11 @@ static void *schedulerMain(void *scheduler) centersList = (Center_t*) calloc(1, sizeof(Center_t)); nPartialCycles = 0; + // Initiate the arrowArray lock + if (err = createArrowLock(args->arrowArray), err != 0) { // TODO destroy this + printLog("Impossible to create the arrow array lock (error %d)\n", err); + return NULL; + } // // MAIN LOOP // @@ -114,17 +120,24 @@ static void *schedulerMain(void *scheduler) // Create a new thread if (nworker < ncpu) { - // Random choice of an arrow - electedArrow = - &args->arrowList->array[rand() % args->arrowList->size]; - // Find a local area - workArea = findWorkArea(centersList, electedArrow, args->ruleRadius, - args->globalDrawingSpace->size, - args->globalDrawingSpace->xmax, - args->globalDrawingSpace->ymax, - args->globalDrawingSpace->zmax - ); + workArea = NULL; + + // Acquiring lock to consistently read the arrowArray + if (acquireNonBlockinArrowgLock(args->arrowArray)) { //XXX does not return 0 + // Random choice of an arrow + electedArrow = + &args->arrowArray->array[rand() % args->arrowArray->size]; // XXX size is 0 + + // Find a local area + workArea = findWorkArea(centersList, electedArrow, args->ruleRadius, + args->globalDrawingSpace->size, + args->globalDrawingSpace->xmax, + args->globalDrawingSpace->ymax, + args->globalDrawingSpace->zmax + ); + releaseArrowLock(args->arrowArray); + } // If a free area exists, if (workArea) { @@ -141,8 +154,8 @@ static void *schedulerMain(void *scheduler) args->globalDrawingSpace; workerArray[i].transitionsTree = args->transitionsTree; - workerArray[i].arrowList = - args->arrowList; + workerArray[i].arrowArray = + args->arrowArray; // create the worker, WorkerInit(&workerArray[i]); // and increment worker count.