X-Git-Url: http://rtime.felk.cvut.cz/gitweb/can-benchmark.git/blobdiff_plain/597f695d267970a7dd260f938e50efadc4fefa7d..b83118bb4a39f5a8540c1f776e0af0420737afd0:/rtems/gw/libs/load.c 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);