From 51835242fc57907004ec44d499b5278ac6cc822b Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 8 Mar 2009 23:33:41 +0000 Subject: Add support for priority inheritance git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1581 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/examples/ostest/Makefile | 32 ++-- nuttx/examples/ostest/main.c | 12 +- nuttx/examples/ostest/ostest.h | 6 +- nuttx/examples/ostest/prioinherit.c | 292 ++++++++++++++++++++++++++++++++++++ 4 files changed, 329 insertions(+), 13 deletions(-) create mode 100644 nuttx/examples/ostest/prioinherit.c (limited to 'nuttx/examples') diff --git a/nuttx/examples/ostest/Makefile b/nuttx/examples/ostest/Makefile index adb875932..d04de7bfe 100644 --- a/nuttx/examples/ostest/Makefile +++ b/nuttx/examples/ostest/Makefile @@ -1,7 +1,7 @@ ############################################################################ # examples/ostest/Makefile # -# Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. +# Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -39,35 +39,47 @@ ASRCS = AOBJS = $(ASRCS:.S=$(OBJEXT)) CSRCS = main.c dev_null.c + ifneq ($(CONFIG_DISABLE_PTHREAD),y) CSRCS += cancel.c cond.c mutex.c sem.c barrier.c ifneq ($(CONFIG_RR_INTERVAL),0) CSRCS += roundrobin.c -endif +endif # CONFIG_RR_INTERVAL ifeq ($(CONFIG_MUTEX_TYPES),y) CSRCS += rmutex.c -endif -endif +endif # CONFIG_MUTEX_TYPES +endif # CONFIG_DISABLE_PTHREAD + ifneq ($(CONFIG_DISABLE_SIGNALS),y) CSRCS += sighand.c ifneq ($(CONFIG_DISABLE_PTHREAD),y) ifneq ($(CONFIG_DISABLE_CLOCK),y) CSRCS += timedwait.c -endif -endif -endif +endif # CONFIG_DISABLE_CLOCK +endif # CONFIG_DISABLE_PTHREAD +endif # CONFIG_DISABLE_SIGNALS + ifneq ($(CONFIG_DISABLE_MQUEUE),y) ifneq ($(CONFIG_DISABLE_PTHREAD),y) CSRCS += mqueue.c ifneq ($(CONFIG_DISABLE_CLOCK),y) CSRCS += timedmqueue.c -endif -endif -endif +endif # CONFIG_DISABLE_CLOCK +endif # CONFIG_DISABLE_PTHREAD +endif # CONFIG_DISABLE_MQUEUE + ifneq ($(CONFIG_DISABLE_POSIX_TIMERS),y) CSRCS += posixtimer.c endif +ifneq ($(CONFIG_DISABLE_SIGNALS),y) +ifneq ($(CONFIG_DISABLE_PTHREAD),y) +ifeq ($(CONFIG_PRIORITY_INHERITANCE),y) +CSRCS += prioinherit.c +endif # CONFIG_PRIORITY_INHERITANCE +endif # CONFIG_DISABLE_PTHREAD +endif # CONFIG_DISABLE_SIGNALS + COBJS = $(CSRCS:.c=$(OBJEXT)) SRCS = $(ASRCS) $(CSRCS) diff --git a/nuttx/examples/ostest/main.c b/nuttx/examples/ostest/main.c index 636d8af13..a0c43416e 100644 --- a/nuttx/examples/ostest/main.c +++ b/nuttx/examples/ostest/main.c @@ -1,7 +1,7 @@ /**************************************************************************** - * main.c + * ostest/main.c * - * Copyright (C) 2007, 2008, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -382,6 +382,14 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif +#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) + /* Verify priority inheritance */ + + printf("\nuser_main: priority inheritance test\n"); + priority_inheritance(); + check_test_memory_usage(); +#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ + /* Compare memory usage at time user_start started until * user_main exits. These should not be identical, but should * be similar enough that we can detect any serious OS memory diff --git a/nuttx/examples/ostest/ostest.h b/nuttx/examples/ostest/ostest.h index c94412cc1..8c71906c5 100644 --- a/nuttx/examples/ostest/ostest.h +++ b/nuttx/examples/ostest/ostest.h @@ -1,7 +1,7 @@ /**************************************************************************** * examples/ostest/ostest.h * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -135,4 +135,8 @@ extern void rr_test(void); extern void barrier_test(void); +/* prioinherit.c ************************************************************/ + +extern void priority_inheritance(void); + #endif /* __OSTEST_H */ diff --git a/nuttx/examples/ostest/prioinherit.c b/nuttx/examples/ostest/prioinherit.c new file mode 100644 index 000000000..818f46b75 --- /dev/null +++ b/nuttx/examples/ostest/prioinherit.c @@ -0,0 +1,292 @@ +/**************************************************************************** + * examples/ostest/prioinherit.c + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include + +#include "ostest.h" + +#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static sem_t g_sem; +static volatile int g_middle = 0; + +/**************************************************************************** + * Name: highpri_thread + ****************************************************************************/ + +static void *highpri_thread(void *parameter) +{ + int ret; + + printf("highpri_thread: Thread %d started\n"); + + ret = sem_wait(&g_sem); + if (ret != 0) + { + printf("highpri_thread: sem_take failed: %d\n", ret); + } + else if (g_middle == 1) + { + printf("highpri_thread: Success midpri_thread is still running!\n"); + } + else + { + printf("highpri_thread: ERROR -- midpri_thread has already exited!\n"); + } + + sem_post(&g_sem); + printf("medpri_thread: Okay... I'm done!\n"); + fflush(stdout); + return NULL; +} + +/**************************************************************************** + * Name: medpri_thread + ****************************************************************************/ + +static void *medpri_thread(void *parameter) +{ + volatile long i; + + printf("medpri_thread: Thread %d started ... I won't let go of the CPU!\n"); + g_middle = 1; + for (i = 0; i < 0x7ffffff; i++); + printf("medpri_thread: Okay... I'm done!\n"); + fflush(stdout); + return NULL; +} + +/**************************************************************************** + * Name: lowpri_thread + ****************************************************************************/ + +static void *lowpri_thread(void *parameter) +{ + void *retval = (void*)-1; + int ret; + + printf("lowpri_thread: Thread %d started\n"); + + ret = sem_wait(&g_sem); + if (ret != 0) + { + printf("lowpri_thread: sem_take failed: %d\n", ret); + } + else + { + /* Hang on to the thread until the middle priority thread runs */ + + while (g_middle == 0) + { + printf("lowpri_thread: Waiting for the midle pri task to run\n"); + printf("lowpri_thread: I still have the semaphore\n"); + sleep(1); + } + + /* The middle priority task is running, let go of the semaphore */ + + if (g_middle == 1) + { + /* Good.. the middle priority task is still running but we got priority! */ + + retval = NULL; + } + else + { + printf("lowpri_thread: ERROR the middle priority task has already exitted!\n"); + } + } + + printf("lowpri_thread: Letting go of the semaphore\n"); + sem_post(&g_sem); + printf("lowpri_thread: Okay... I'm done!\n"); + fflush(stdout); + return retval; +} +#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: priority_inheritance + ****************************************************************************/ + +void priority_inheritance(void) +{ +#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) + pthread_t lowpri; + pthread_t medpri; + pthread_t highpri; + pthread_addr_t result; + pthread_attr_t attr; + struct sched_param sparam; + int my_pri; + int max_pri; + int mid_pri; + int min_pri; + int status; + + printf("priority_inheritance: Started\n"); + + status = sched_getparam (getpid(), &sparam); + if (status != 0) + { + printf("priority_inheritance: sched_getparam failed\n"); + sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY; + } + my_pri = sparam.sched_priority; + + max_pri = sched_get_priority_max(SCHED_FIFO); + min_pri = sched_get_priority_min(SCHED_FIFO); + mid_pri = my_pri - 1; + + sem_init(&g_sem, 0, 1); + + /* Start the low priority task */ + + printf("priority_inheritance: Starting lowpri_thread at %d\n", min_pri); + status = pthread_attr_init(&attr); + if (status != 0) + { + printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status); + } + sparam.sched_priority = min_pri; + status = pthread_attr_setschedparam(&attr,& sparam); + if (status != OK) + { + printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status); + } + else + { + printf("priority_inheritance: Set lowpri_thread priority to %d\n", sparam.sched_priority); + } + + status = pthread_create(&lowpri, &attr, lowpri_thread, NULL); + if (status != 0) + { + printf("priority_inheritance: pthread_create failed, status=%d\n", status); + } + + printf("priority_inheritance: Waiting...\n"); + sleep(5); + + /* Start the medium priority task */ + + printf("priority_inheritance: Starting medpri_thread at %d\n", mid_pri); + status = pthread_attr_init(&attr); + if (status != 0) + { + printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status); + } + + sparam.sched_priority = mid_pri; + status = pthread_attr_setschedparam(&attr,& sparam); + if (status != OK) + { + printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status); + } + else + { + printf("priority_inheritance: Set medpri_thread priority to %d\n", sparam.sched_priority); + } + + status = pthread_create(&medpri, &attr, medpri_thread, NULL); + if (status != 0) + { + printf("priority_inheritance: pthread_create failed, status=%d\n", status); + } + + /* Start the high priority task */ + + printf("priority_inheritance: Starting highpri_thread at %d\n", max_pri); + status = pthread_attr_init(&attr); + if (status != 0) + { + printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status); + } + + sparam.sched_priority = max_pri; + status = pthread_attr_setschedparam(&attr,& sparam); + if (status != OK) + { + printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status); + } + else + { + printf("priority_inheritance: Set highpri_thread priority to %d\n", sparam.sched_priority); + } + + status = pthread_create(&medpri, &attr, highpri_thread, NULL); + if (status != 0) + { + printf("priority_inheritance: pthread_create failed, status=%d\n", status); + } + + /* Wait for all thread instances to complete */ + + printf("priority_inheritance: Waiting for highpri_thread to complete\n"); + (void)pthread_join(highpri, &result); + printf("priority_inheritance: Waiting for medpri_thread to complete\n"); + (void)pthread_join(medpri, &result); + printf("priority_inheritance: Waiting for lowpri_thread to complete\n"); + (void)pthread_join(lowpri, &result); + + printf("priority_inheritance: Finished\n"); + sem_destroy(&g_sem); + fflush(stdout); +#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ +} -- cgit v1.2.3