diff --git a/include/base.h b/include/base.h index 040fa98..9e9947e 100644 --- a/include/base.h +++ b/include/base.h @@ -85,7 +85,7 @@ struct { int nMaxThread; int nMaxCycles; int ruleRadius; - pthread_t *id; + pthread_t id; bool pleaseStop; } typedef Scheduler_t; @@ -104,7 +104,7 @@ struct Center_t { } typedef Center_t; struct { - pthread_t *id; + pthread_t id; Center_t *localWorkAreaCenter; Space_t *globalDrawingSpace; IntArray_t *conditionTree; @@ -120,7 +120,7 @@ struct { // Server // struct { - pthread_t *id; + pthread_t id; bool pleaseStop; } typedef Server_t; @@ -136,7 +136,7 @@ struct { // Supervisor // struct { - pthread_t *id; + pthread_t id; bool pleaseStop; int *workerCreationOccurency; } typedef Supervisor_t; diff --git a/include/localworker.h b/include/localworker.h index 7e2b6eb..f609054 100644 --- a/include/localworker.h +++ b/include/localworker.h @@ -37,8 +37,7 @@ static inline void WorkerDestroy(Worker_t *worker) worker->terminated = false; worker->returnValue = false; - free(worker->id); - worker->id = NULL; + worker->id = 0; free(worker->localWorkAreaCenter); worker->localWorkAreaCenter = NULL; } @@ -48,5 +47,5 @@ static inline void WorkerDestroy(Worker_t *worker) // -------------------------------------------------------------------------- // static inline void WorkerWait(Worker_t *worker) { - pthread_join(*worker->id, NULL); + pthread_join(worker->id, NULL); } diff --git a/include/scheduler.h b/include/scheduler.h index cb5eb40..0149738 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -33,7 +33,6 @@ void SchedInit(Scheduler_t *scheduler); // -------------------------------------------------------------------------- // static inline void SchedDestroy(Scheduler_t *scheduler) { - free(scheduler->id); free(scheduler->globalDrawingSpace->space); free(scheduler->globalDrawingSpace); @@ -46,5 +45,5 @@ static inline void SchedDestroy(Scheduler_t *scheduler) // -------------------------------------------------------------------------- // static inline void SchedWait(Scheduler_t *scheduler) { - pthread_join(*scheduler->id, NULL); + pthread_join(scheduler->id, NULL); } diff --git a/include/server.h b/include/server.h index ce08e27..6dbf3e3 100644 --- a/include/server.h +++ b/include/server.h @@ -33,7 +33,6 @@ void ServerInit(Server_t *server); // -------------------------------------------------------------------------- // static inline void ServerDestroy(Server_t *server) { - free(server->id); } // -------------------------------------------------------------------------- // @@ -41,5 +40,5 @@ static inline void ServerDestroy(Server_t *server) // -------------------------------------------------------------------------- // static inline void ServerWait(Server_t *server) { - pthread_join(*server->id, NULL); + pthread_join(server->id, NULL); } diff --git a/include/supervisor.h b/include/supervisor.h index d7b4e67..baa4d97 100644 --- a/include/supervisor.h +++ b/include/supervisor.h @@ -33,7 +33,6 @@ void SupervisorInit(Supervisor_t *supervisor); // -------------------------------------------------------------------------- // static inline void SupervisorDestroy(Supervisor_t *supervisor) { - free(supervisor->id); } // -------------------------------------------------------------------------- // @@ -41,5 +40,5 @@ static inline void SupervisorDestroy(Supervisor_t *supervisor) // -------------------------------------------------------------------------- // static inline void SupervisorWait(Supervisor_t *supervisor) { - pthread_join(*supervisor->id, NULL); + pthread_join(supervisor->id, NULL); } diff --git a/src/localworker.c b/src/localworker.c index 14fb910..1931a2d 100644 --- a/src/localworker.c +++ b/src/localworker.c @@ -30,9 +30,8 @@ static void *workerMain(void *worker); // -------------------------------------------------------------------------- // void WorkerInit(Worker_t *worker) { - worker->id = (pthread_t*) calloc(1, sizeof(pthread_t)); - if (pthread_create(worker->id, NULL, workerMain, worker)) { - printLog("Worker #%lu can't be initialized!\n", *(worker->id)); + if (pthread_create(&worker->id, NULL, workerMain, worker)) { + printLog("Worker #%lu can't be initialized!\n", worker->id); return; } } @@ -46,7 +45,7 @@ static void *workerMain(void *worker) int a = rand()%__INT_MAX__; args = (Worker_t*) worker; - printLog("Worker #%lu online\n", *args->id); + printLog("Worker #%lu online\n", args->id); int size = args->globalDrawingSpace->size; /* In each cell: the West (left) site is 0, the East (right) site is 1 */ @@ -85,7 +84,7 @@ static void *workerMain(void *worker) args->returnValue = a; args->terminated = true; - printLog("Worker #%lu offline\n", *args->id); + printLog("Worker #%lu offline\n", args->id); return NULL; } diff --git a/src/scheduler.c b/src/scheduler.c index cb6096d..f5254d8 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -35,8 +35,7 @@ static void *schedulerMain(void *scheduler); // -------------------------------------------------------------------------- // void SchedInit(Scheduler_t *scheduler) { - scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t)); - pthread_create(scheduler->id, NULL, schedulerMain, scheduler); + pthread_create(&scheduler->id, NULL, schedulerMain, scheduler); } /* -------------------------------------------------------------------------- */ @@ -88,7 +87,7 @@ static void *schedulerMain(void *scheduler) // Getting scheduler argument structure args = (Scheduler_t*) scheduler; - printLog("Scheduler #%lu online\n", *args->id); + printLog("Scheduler #%lu online\n", args->id); if (!args->nMaxThread) { // nmaxthread = 0 => no minimum ncpu = get_nprocs(); // allocating all the cpus available @@ -181,7 +180,7 @@ static void *schedulerMain(void *scheduler) // Remove preemption on space CenterRemove(workerArray[i].localWorkAreaCenter); printLog("Worker #%lu terminated with return %d. Cleaning...\n", - *workerArray[i].id, + workerArray[i].id, workerArray[i].returnValue ); // Remove Worker @@ -197,13 +196,13 @@ static void *schedulerMain(void *scheduler) // Waiting for remaining workers for (int i = 0; i < ncpu; i++) { if (workerArray[i].id) { - printLog("Waiting for termination of worker #%lu\n", *workerArray[i].id); + printLog("Waiting for termination of worker #%lu\n", workerArray[i].id); // Join the thread to wait for his termination WorkerWait(&workerArray[i]); // Remove preemption on space CenterRemove(workerArray[i].localWorkAreaCenter); printLog("Worker #%lu terminated with return %d. Cleaning...\n", - *workerArray[i].id, + workerArray[i].id, workerArray[i].returnValue ); // Remove Worker @@ -211,7 +210,7 @@ static void *schedulerMain(void *scheduler) } } - printLog("Scheduler #%lu offline\n", *args->id); + printLog("Scheduler #%lu offline\n", args->id); free(workerArray); free(centersList); diff --git a/src/server.c b/src/server.c index 58fc334..8830455 100644 --- a/src/server.c +++ b/src/server.c @@ -35,8 +35,7 @@ static void *serverMain(void *server); // -------------------------------------------------------------------------- // void ServerInit(Server_t *server) { - server->id = (pthread_t*) calloc(1, sizeof(pthread_t)); - pthread_create(server->id, NULL, serverMain, server); + pthread_create(&server->id, NULL, serverMain, server); } #define SEND_BUFFER_SIZE 80 * 24 @@ -107,7 +106,7 @@ static void *serverMain(void *server) // Get args args = (Server_t*) server; - printLog("Server #%lu online\n", *args->id); + printLog("Server #%lu online\n", args->id); // Create socket sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -157,6 +156,6 @@ serverExiting: close(sockfd); serverEarlyExiting: - printLog("Server #%lu offline\n", *args->id); + printLog("Server #%lu offline\n", args->id); return NULL; } diff --git a/src/supervisor.c b/src/supervisor.c index d173719..a3e8805 100644 --- a/src/supervisor.c +++ b/src/supervisor.c @@ -30,8 +30,7 @@ static void *supervisorMain(void *supervisor); // -------------------------------------------------------------------------- // void SupervisorInit(Supervisor_t *supervisor) { - supervisor->id = (pthread_t*) calloc(1, sizeof(pthread_t)); - pthread_create(supervisor->id, NULL, supervisorMain, supervisor); + pthread_create(&supervisor->id, NULL, supervisorMain, supervisor); } // -------------------------------------------------------------------------- //