From 5213ffa9e2ff09d8ff609b61a7cf589d03050477 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Wed, 21 Aug 2013 20:21:43 +0200 Subject: [PATCH 1/1] Adds a library with pair of functions to put load on CPU. The implementation is, with the exception of error handling, feature complete, but not well commented. --- rtems/gw/libs/Makefile | 14 +++++ rtems/gw/libs/Makefile.omk | 5 ++ rtems/gw/libs/load.c | 116 +++++++++++++++++++++++++++++++++++++ rtems/gw/libs/load.h | 7 +++ 4 files changed, 142 insertions(+) create mode 100644 rtems/gw/libs/Makefile create mode 100644 rtems/gw/libs/Makefile.omk create mode 100644 rtems/gw/libs/load.c create mode 100644 rtems/gw/libs/load.h diff --git a/rtems/gw/libs/Makefile b/rtems/gw/libs/Makefile new file mode 100644 index 0000000..76b56fd --- /dev/null +++ b/rtems/gw/libs/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or parent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/rtems/gw/libs/Makefile.omk b/rtems/gw/libs/Makefile.omk new file mode 100644 index 0000000..dd4d803 --- /dev/null +++ b/rtems/gw/libs/Makefile.omk @@ -0,0 +1,5 @@ +lib_LIBRARIES = load + +include_HEADERS = load.h + +load_SOURCES = load.c diff --git a/rtems/gw/libs/load.c b/rtems/gw/libs/load.c new file mode 100644 index 0000000..ba7fac4 --- /dev/null +++ b/rtems/gw/libs/load.c @@ -0,0 +1,116 @@ +/* +* 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 +#include + +#include "load.h" + + +static void* produce(void* arg); +static void* consume(void* arg); + +/* semaphores for consumer/producer pair */ +static sem_t produced, consumed; +/* pthread handles for the loader */ +static pthread_t consumer, producer; +static int n = 0; +static char running = 0; + +static void* produce(void* arg){ + while (1) { + sem_wait(&consumed); + n++; /* increment n by 1 */ + sem_post(&produced); + pthread_testcancel(); + } + return NULL; +} + +static void* consume(void* arg){ + while (1) { + sem_wait(&produced); + n--; /* aaand decrement */ + sem_post(&consumed); + pthread_testcancel(); + } + return NULL; +} + +int start_thread_load(){ + if (running == 0){ + printf("Attempting to start load.\n"); + int res; + running = 1; + res = sem_init(&consumed, 0, 0); + if (res < 0){ + printf("Couldn't initialize consumed semaphore.\n"); + return 1; + } + res = sem_init(&produced, 0, 1); + if (res < 0){ + printf("Couldn't initialize produced semaphore.\n"); + return 1; + } + + res = pthread_create(&producer, NULL, produce, NULL); + if (res < 0){ + printf("Couldn't create producer thread.\n"); + return 1; + } + + res = pthread_create(&consumer, NULL, consume, NULL); + if (res < 0){ + printf("Couldn't create consumer thread.\n"); + return 1; + } + + pthread_detach(producer); + pthread_detach(consumer); + printf("Load started succesfully.\n"); + return 0; + } else { + printf("Load is already running.\n"); + return 0; + } +} + +/* +* This function stops threads loading the CPU and destroys associated semaphores. +* +* No error handling currently, only tries to report errors. +* +*/ +int end_thread_load(){ + if (running == 1){ + int res; + printf("Attempting to cancel producer thread.\n"); + res = pthread_cancel(producer); + if (res != 0){ + printf("Failed.\n"); + /* Not sure what to do with error here, will have to figure out later. */ + } + printf("Attempting to cancel consumer thread.\n"); + res = pthread_cancel(consumer); + if (res != 0){ + printf("Failed.\n"); + } + printf("Preparing to destroy semaphores.\n"); + sleep(1); + sem_destroy(&produced); + sem_destroy(&consumed); + running = 0; + printf("Finished.\n"); + return 0; + } else { + printf("Load is not running.\n"); + return 0; + } +} \ No newline at end of file diff --git a/rtems/gw/libs/load.h b/rtems/gw/libs/load.h new file mode 100644 index 0000000..b355c24 --- /dev/null +++ b/rtems/gw/libs/load.h @@ -0,0 +1,7 @@ +#ifndef __CPU_LOAD_H_ +#define __CPU_LOAD_H_ + +int start_thread_load(); +int end_thread_load(); + +#endif \ No newline at end of file -- 2.39.2