From b2cebaa9d4a38cdbe561a05f086ea415be51df72 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 10 Oct 2014 09:34:03 -0600 Subject: [PATCH] Modularize starting of worker threads to better isolate individual initialization characteristics --- include/nuttx/wqueue.h | 23 ------- libc/wqueue/Make.defs | 4 +- libc/wqueue/work_usrstart.c | 115 ---------------------------------- libc/wqueue/work_usrthread.c | 48 +++++++++++++- sched/init/os_bringup.c | 92 ++++++++++----------------- sched/wqueue/kwork_hpthread.c | 49 ++++++++++++++- sched/wqueue/kwork_lpthread.c | 49 ++++++++++++++- sched/wqueue/wqueue.h | 58 ++++++++--------- 8 files changed, 203 insertions(+), 235 deletions(-) delete mode 100644 libc/wqueue/work_usrstart.c diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h index 8af1c938fa..e572493fae 100644 --- a/include/nuttx/wqueue.h +++ b/include/nuttx/wqueue.h @@ -343,29 +343,6 @@ extern "C" void work_process(FAR struct wqueue_s *wqueue); -/**************************************************************************** - * Name: work_usrthread - * - * Description: - * This is the worker thread that performs the actions placed on the user - * work queue. - * - * This is a user mode work queue. It must be used by applications for - * miscellaneous operations. The user work thread must be started by - * application start-up logic by calling work_usrstart(). - * - * Input parameters: - * argc, argv (not used) - * - * Returned Value: - * Does not return - * - ****************************************************************************/ - -#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) -int work_usrthread(int argc, char *argv[]); -#endif - /**************************************************************************** * Name: work_usrstart * diff --git a/libc/wqueue/Make.defs b/libc/wqueue/Make.defs index 87c5437e34..2b953626e4 100644 --- a/libc/wqueue/Make.defs +++ b/libc/wqueue/Make.defs @@ -40,10 +40,10 @@ ifeq ($(CONFIG_SCHED_WORKQUEUE),y) CSRCS += work_process.c work_queue.c work_cancel.c work_signal.c ifeq ($(CONFIG_BUILD_PROTECTED),y) -CSRCS += work_usrstart.c work_usrthread.c +CSRCS += work_usrthread.c else ifeq ($(CONFIG_BUILD_KERNEL),y) -CSRCS += work_usrstart.c work_usrthread.c +CSRCS += work_usrthread.c endif endif diff --git a/libc/wqueue/work_usrstart.c b/libc/wqueue/work_usrstart.c deleted file mode 100644 index 00264cbe89..0000000000 --- a/libc/wqueue/work_usrstart.c +++ /dev/null @@ -1,115 +0,0 @@ -/**************************************************************************** - * libc/wqueue/work_usrstart.c - * - * Copyright (C) 2013 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 - -#if defined(CONFIG_BUILD_PROTECTED) && !defined(__KERNEL__) && \ - defined(CONFIG_SCHED_WORKQUEUE) && defined(CONFIG_SCHED_USRWORK) - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Type Declarations - ****************************************************************************/ - -/**************************************************************************** - * Public Variables - ****************************************************************************/ - -/**************************************************************************** - * Private Variables - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ -/**************************************************************************** - * Name: work_usrstart - * - * Description: - * Start the user mode work queue. - * - * Input parameters: - * None - * - * Returned Value: - * The task ID of the worker thread is returned on success. A negated - * errno value is returned on failure. - * - ****************************************************************************/ - -int work_usrstart(void) -{ - /* Start a user-mode worker thread for use by applications. */ - - svdbg("Starting user-mode worker thread\n"); - - g_usrwork[USRWORK].pid = task_create("usrwork", - CONFIG_SCHED_USRWORKPRIORITY, - CONFIG_SCHED_USRWORKSTACKSIZE, - (main_t)work_usrthread, - (FAR char * const *)NULL); - - DEBUGASSERT(g_usrwork[USRWORK].pid > 0); - if (g_usrwork[USRWORK].pid < 0) - { - int errcode = errno; - DEBUGASSERT(errcode > 0); - - sdbg("task_create failed: %d\n", errcode); - return -errcode; - } - - return g_usrwork[USRWORK].pid; -} - -#endif /* CONFIG_BUILD_PROTECTED && !__KERNEL__ CONFIG_SCHED_WORKQUEUE && CONFIG_SCHED_USRWORK */ diff --git a/libc/wqueue/work_usrthread.c b/libc/wqueue/work_usrthread.c index f46924a069..b20babcea6 100644 --- a/libc/wqueue/work_usrthread.c +++ b/libc/wqueue/work_usrthread.c @@ -39,6 +39,8 @@ #include +#include + #include #if defined(CONFIG_SCHED_WORKQUEUE) && defined(CONFIG_SCHED_USRWORK) && \ @@ -87,7 +89,7 @@ extern struct wqueue_s g_usrwork; * ****************************************************************************/ -int work_usrthread(int argc, char *argv[]) +static int work_usrthread(int argc, char *argv[]) { /* Loop forever */ @@ -103,4 +105,48 @@ int work_usrthread(int argc, char *argv[]) return OK; /* To keep some compilers happy */ } +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: work_usrstart + * + * Description: + * Start the user mode work queue. + * + * Input parameters: + * None + * + * Returned Value: + * The task ID of the worker thread is returned on success. A negated + * errno value is returned on failure. + * + ****************************************************************************/ + +int work_usrstart(void) +{ + /* Start a user-mode worker thread for use by applications. */ + + svdbg("Starting user-mode worker thread\n"); + + g_usrwork.pid = task_create("uwork", + CONFIG_SCHED_USRWORKPRIORITY, + CONFIG_SCHED_USRWORKSTACKSIZE, + (main_t)work_usrthread, + (FAR char * const *)NULL); + + DEBUGASSERT(g_usrwork.pid > 0); + if (g_usrwork.pid < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + sdbg("task_create failed: %d\n", errcode); + return -errcode; + } + + return g_usrwork.pid; +} + #endif /* CONFIG_SCHED_WORKQUEUE && CONFIG_SCHED_USRWORK && !__KERNEL__*/ diff --git a/sched/init/os_bringup.c b/sched/init/os_bringup.c index b4a5537d51..09f31d2e06 100644 --- a/sched/init/os_bringup.c +++ b/sched/init/os_bringup.c @@ -117,24 +117,10 @@ # endif #endif -/* If NuttX is built as a separately compiled module, then the config.h header - * file should contain the address of the entry point (or path to the file) - * that will perform the application-level initialization. - */ +/* In the protected build (only) we also need to start the user work queue */ -/* Customize some strings */ - -#ifdef CONFIG_SCHED_WORKQUEUE -# ifdef CONFIG_SCHED_HPWORK -# if defined(CONFIG_SCHED_LPWORK) -# define HPWORKNAME "hpwork" -# define LPWORKNAME "lpwork" -# elif defined(CONFIG_SCHED_USRWORK) -# define HPWORKNAME "knlwork" -# else -# define HPWORKNAME "work" -# endif -# endif +#if !defined(CONFIG_BUILD_PROTECTED) +# undef CONFIG_SCHED_USRWORK #endif /**************************************************************************** @@ -211,47 +197,35 @@ static inline void os_pgworker(void) #ifdef CONFIG_SCHED_WORKQUEUE static inline void os_workqueues(void) { -#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_SCHED_USRWORK) - int taskid; +#ifdef CONFIG_SCHED_USRWORK + pid_t pid; #endif #ifdef CONFIG_SCHED_HPWORK + /* Start the high-priority worker thread to support device driver lower + * halves. + */ + + (void)work_hpstart(); + +#endif /* CONFIG_SCHED_HPWORK */ + #ifdef CONFIG_SCHED_LPWORK - svdbg("Starting high-priority kernel worker thread\n"); -#else - svdbg("Starting kernel worker thread\n"); -#endif - - g_hpwork.pid = kernel_thread(HPWORKNAME, CONFIG_SCHED_WORKPRIORITY, - CONFIG_SCHED_WORKSTACKSIZE, - (main_t)work_hpthread, - (FAR char * const *)NULL); - DEBUGASSERT(g_hpwork.pid > 0); - - /* Start a lower priority worker thread for other, non-critical continuation + /* Start the low-priority worker thread for other, non-critical continuation * tasks */ -#ifdef CONFIG_SCHED_LPWORK - - svdbg("Starting low-priority kernel worker thread\n"); - - g_lpwork.pid = kernel_thread(LPWORKNAME, CONFIG_SCHED_LPWORKPRIORITY, - CONFIG_SCHED_LPWORKSTACKSIZE, - (main_t)work_lpthread, - (FAR char * const *)NULL); - DEBUGASSERT(g_lpwork.pid > 0); + (void)work_lpstart(); #endif /* CONFIG_SCHED_LPWORK */ -#endif /* CONFIG_SCHED_HPWORK */ -#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_SCHED_USRWORK) +#ifdef CONFIG_SCHED_USRWORK /* Start the user-space work queue */ DEBUGASSERT(USERSPACE->work_usrstart != NULL); - taskid = USERSPACE->work_usrstart(); - DEBUGASSERT(taskid > 0); - UNUSED(taskid); + pid = USERSPACE->work_usrstart(); + DEBUGASSERT(pid > 0); + UNUSED(pid); #endif } @@ -278,7 +252,7 @@ static inline void os_workqueues(void) #if defined(CONFIG_INIT_ENTRYPOINT) static inline void os_do_appstart(void) { - int taskid; + int pid; #ifdef CONFIG_BOARD_INITIALIZE /* Perform any last-minute, board-specific initialization, if so @@ -298,16 +272,16 @@ static inline void os_do_appstart(void) #ifdef CONFIG_BUILD_PROTECTED DEBUGASSERT(USERSPACE->us_entrypoint != NULL); - taskid = task_create("init", SCHED_PRIORITY_DEFAULT, - CONFIG_USERMAIN_STACKSIZE, USERSPACE->us_entrypoint, - (FAR char * const *)NULL); + pid = task_create("init", SCHED_PRIORITY_DEFAULT, + CONFIG_USERMAIN_STACKSIZE, USERSPACE->us_entrypoint, + (FAR char * const *)NULL); #else - taskid = task_create("init", SCHED_PRIORITY_DEFAULT, - CONFIG_USERMAIN_STACKSIZE, - (main_t)CONFIG_USER_ENTRYPOINT, - (FAR char * const *)NULL); + pid = task_create("init", SCHED_PRIORITY_DEFAULT, + CONFIG_USERMAIN_STACKSIZE, + (main_t)CONFIG_USER_ENTRYPOINT, + (FAR char * const *)NULL); #endif - ASSERT(taskid > 0); + ASSERT(pid > 0); } #elif defined(CONFIG_INIT_FILEPATH) @@ -389,16 +363,16 @@ static int os_start_task(int argc, FAR char **argv) static inline void os_start_application(void) { #ifdef CONFIG_BOARD_INITTHREAD - int taskid; + int pid; /* Do the board/application initialization on a separate thread of * execution. */ - taskid = kernel_thread("AppBringUp", CONFIG_BOARD_INITTHREAD_PRIORITY, - CONFIG_BOARD_INITTHREAD_STACKSIZE, - (main_t)os_start_task, (FAR char * const *)NULL); - ASSERT(taskid > 0); + pid = kernel_thread("AppBringUp", CONFIG_BOARD_INITTHREAD_PRIORITY, + CONFIG_BOARD_INITTHREAD_STACKSIZE, + (main_t)os_start_task, (FAR char * const *)NULL); + ASSERT(pid > 0); #else /* Do the board/application initialization on this thread of execution. */ diff --git a/sched/wqueue/kwork_hpthread.c b/sched/wqueue/kwork_hpthread.c index 88b2e502d5..45c7004314 100644 --- a/sched/wqueue/kwork_hpthread.c +++ b/sched/wqueue/kwork_hpthread.c @@ -39,7 +39,11 @@ #include +#include +#include + #include +#include #include #include "wqueue/wqueue.h" @@ -96,7 +100,7 @@ struct wqueue_s g_hpwork; * ****************************************************************************/ -int work_hpthread(int argc, char *argv[]) +static int work_hpthread(int argc, char *argv[]) { /* Loop forever */ @@ -123,4 +127,47 @@ int work_hpthread(int argc, char *argv[]) return OK; /* To keep some compilers happy */ } +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: work_hpstart + * + * Description: + * Start the high-priority, kernel-mode work queue. + * + * Input parameters: + * None + * + * Returned Value: + * The task ID of the worker thread is returned on success. A negated + * errno value is returned on failure. + * + ****************************************************************************/ + +int work_hpstart(void) +{ + /* Start the high-priority, kernel mode worker thread */ + + svdbg("Starting high-priority kernel worker thread\n"); + + g_hpwork.pid = kernel_thread(HPWORKNAME, CONFIG_SCHED_WORKPRIORITY, + CONFIG_SCHED_WORKSTACKSIZE, + (main_t)work_hpthread, + (FAR char * const *)NULL); + + DEBUGASSERT(g_hpwork.pid > 0); + if (g_hpwork.pid < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + slldbg("kernel_thread failed: %d\n", errcode); + return -errcode; + } + + return g_hpwork.pid; +} + #endif /* CONFIG_SCHED_WORKQUEUE && CONFIG_SCHED_HPWORK*/ diff --git a/sched/wqueue/kwork_lpthread.c b/sched/wqueue/kwork_lpthread.c index c3991dc4d3..96fd1a68db 100644 --- a/sched/wqueue/kwork_lpthread.c +++ b/sched/wqueue/kwork_lpthread.c @@ -39,7 +39,11 @@ #include +#include +#include + #include +#include #include #include "wqueue/wqueue.h" @@ -95,7 +99,7 @@ struct wqueue_s g_lpwork; * ****************************************************************************/ -int work_lpthread(int argc, char *argv[]) +static int work_lpthread(int argc, char *argv[]) { /* Loop forever */ @@ -120,4 +124,47 @@ int work_lpthread(int argc, char *argv[]) return OK; /* To keep some compilers happy */ } +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: work_lpstart + * + * Description: + * Start the low-priority, kernel-mode worker thread(s) + * + * Input parameters: + * None + * + * Returned Value: + * The task ID of the worker thread is returned on success. A negated + * errno value is returned on failure. + * + ****************************************************************************/ + +int work_lpstart(void) +{ + /* Start the low-priority, kernel mode worker thread(s) */ + + svdbg("Starting low-priority kernel worker thread\n"); + + g_lpwork.pid = kernel_thread(LPWORKNAME, CONFIG_SCHED_LPWORKPRIORITY, + CONFIG_SCHED_LPWORKSTACKSIZE, + (main_t)work_lpthread, + (FAR char * const *)NULL); + + DEBUGASSERT(g_lpwork.pid > 0); + if (g_lpwork.pid < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + slldbg("kernel_thread failed: %d\n", errcode); + return -errcode; + } + + return g_lpwork.pid; +} + #endif /* CONFIG_SCHED_WORKQUEUE && CONFIG_SCHED_LPWORK */ diff --git a/sched/wqueue/wqueue.h b/sched/wqueue/wqueue.h index 1c1127ee81..0c95b8501c 100644 --- a/sched/wqueue/wqueue.h +++ b/sched/wqueue/wqueue.h @@ -48,6 +48,19 @@ * Pre-processor Definitions ****************************************************************************/ +/* Customize kernel thread names */ + +#ifdef CONFIG_SCHED_HPWORK +# if defined(CONFIG_SCHED_LPWORK) +# define HPWORKNAME "hpwork" +# define LPWORKNAME "lpwork" +# elif defined(CONFIG_SCHED_USRWORK) +# define HPWORKNAME "kwork" +# else +# define HPWORKNAME "work" +# endif +#endif + /**************************************************************************** * Public Type Definitions ****************************************************************************/ @@ -73,62 +86,41 @@ extern struct wqueue_s g_lpwork; ****************************************************************************/ /**************************************************************************** - * Name: work_hpthread + * Name: work_hpstart * * Description: - * This is the worker thread that performs the actions placed on the high - * priority work queue. - * - * This, along with the lower priority worker thread(s) are the kernel - * mode work queues (also build in the flat build). One of these threads - * also performs periodic garbage collection (that would otherwise be - * performed by the idle thread if CONFIG_SCHED_WORKQUEUE is not defined). - * That will be the higher priority worker thread only if a lower priority - * worker thread is available. - * - * All kernel mode worker threads are started by the OS during normal - * bring up. This entry point is referenced by OS internally and should - * not be accessed by application logic. + * Start the high-priority, kernel-mode work queue. * * Input parameters: - * argc, argv (not used) + * None * * Returned Value: - * Does not return + * The task ID of the worker thread is returned on success. A negated + * errno value is returned on failure. * ****************************************************************************/ #ifdef CONFIG_SCHED_HPWORK -int work_hpthread(int argc, char *argv[]); +int work_hpstart(void); #endif /**************************************************************************** - * Name: work_lpthread + * Name: work_lpstart * * Description: - * These are the worker thread(s) that performs the actions placed on the - * low priority work queue. - * - * These, along with the higher priority worker thread are the kernel mode - * work queues (also build in the flat build). One of these threads also - * performs periodic garbage collection (that would otherwise be performed - * by the idle thread if CONFIG_SCHED_WORKQUEUE is not defined). That will - * be the lower priority worker thread if it is available. - * - * All kernel mode worker threads are started by the OS during normal - * bring up. This entry point is referenced by OS internally and should - * not be accessed by application logic. + * Start the low-priority, kernel-mode worker thread(s) * * Input parameters: - * argc, argv (not used) + * None * * Returned Value: - * Does not return + * The task ID of the worker thread is returned on success. A negated + * errno value is returned on failure. * ****************************************************************************/ #ifdef CONFIG_SCHED_LPWORK -int work_lpthread(int argc, char *argv[]); +int work_lpstart(void); #endif #endif /* CONFIG_SCHED_WORKQUEUE */