From b83118bb4a39f5a8540c1f776e0af0420737afd0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 16 Sep 2013 16:30:15 +0200 Subject: [PATCH] Cosmetic changes to load.c, load.h. Adds and cleans up comments. --- rtems/gw/libs/load.c | 33 ++++++++++++++++++++------------- rtems/gw/libs/load.h | 30 +++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/rtems/gw/libs/load.c b/rtems/gw/libs/load.c index ba7fac4..6afb7a9 100644 --- a/rtems/gw/libs/load.c +++ b/rtems/gw/libs/load.c @@ -1,11 +1,3 @@ -/* -* Implements simple producer/consumer thread pair to cause load on the CPU. -* -* Used in benchmarking the CAN gateway. -* -* Co-opted from http://support.dce.felk.cvut.cz/pos/cv3/src/semaphore.html. -*/ - #include #include @@ -13,7 +5,7 @@ #include "load.h" - +/* Load function for threads. */ static void* produce(void* arg); static void* consume(void* arg); @@ -27,7 +19,7 @@ static char running = 0; static void* produce(void* arg){ while (1) { sem_wait(&consumed); - n++; /* increment n by 1 */ + n++; sem_post(&produced); pthread_testcancel(); } @@ -37,13 +29,20 @@ static void* produce(void* arg){ static void* consume(void* arg){ while (1) { sem_wait(&produced); - n--; /* aaand decrement */ + n--; sem_post(&consumed); pthread_testcancel(); } return NULL; } +/* +* This function starts threads loading the CPU and creates associated semaphores. +* +* Has a guard to prevent starting again, before it was stopped. +* +* No error handling currently, only tries to report errors. +*/ int start_thread_load(){ if (running == 0){ printf("Attempting to start load.\n"); @@ -84,9 +83,10 @@ int start_thread_load(){ /* * This function stops threads loading the CPU and destroys associated semaphores. +* +* Has a guard against attempting to stop the threads if they are not running. * * No error handling currently, only tries to report errors. -* */ int end_thread_load(){ if (running == 1){ @@ -94,15 +94,22 @@ int end_thread_load(){ printf("Attempting to cancel producer thread.\n"); res = pthread_cancel(producer); if (res != 0){ + /* This means that sending cancel signal has failed... Just returning an error should be enough. */ + /* If we killed the thread, destroying the semaphore would lead to UB. */ printf("Failed.\n"); - /* Not sure what to do with error here, will have to figure out later. */ + return 1; } + printf("Attempting to cancel consumer thread.\n"); res = pthread_cancel(consumer); if (res != 0){ + /* Same here. */ printf("Failed.\n"); + return 1; } + printf("Preparing to destroy semaphores.\n"); + /* Wait a bit so that the threads can get to a cancellation point. */ sleep(1); sem_destroy(&produced); sem_destroy(&consumed); diff --git a/rtems/gw/libs/load.h b/rtems/gw/libs/load.h index b355c24..1cb6af9 100644 --- a/rtems/gw/libs/load.h +++ b/rtems/gw/libs/load.h @@ -1,7 +1,35 @@ +/* +* Implements simple producer/consumer thread pair to cause load on the CPU. +* +* Used in benchmarking the CAN gateway. +* +* Co-opted from http://support.dce.felk.cvut.cz/pos/cv3/src/semaphore.html. +*/ #ifndef __CPU_LOAD_H_ #define __CPU_LOAD_H_ + +/* +* This function starts threads loading the CPU and creates associated semaphores. +* +* Has a guard to prevent starting again, before it was stopped. +* +* No error handling currently, only tries to report errors. +* +* 0 if successfull, 1 otherwise. +*/ int start_thread_load(); + +/* +* This function stops threads loading the CPU and destroys associated semaphores. +* +* Has a guard against attempting to stop the threads if they are not running. +* +* No error handling currently, only tries to report errors. +* +* 0 if successfull, 1 otherwise. +*/ int end_thread_load(); -#endif \ No newline at end of file +#endif + -- 2.39.2