diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h index cad35a56dd..8af1c938fa 100644 --- a/include/nuttx/wqueue.h +++ b/include/nuttx/wqueue.h @@ -169,6 +169,8 @@ # define CONFIG_SCHED_WORKSTACKSIZE CONFIG_IDLETHREAD_STACKSIZE # endif +#endif /* CONFIG_SCHED_HPWORK */ + /* Low priority kernel work queue configuration *****************************/ #ifdef CONFIG_SCHED_LPWORK @@ -215,7 +217,6 @@ #endif #endif /* CONFIG_SCHED_LPWORK */ -#endif /* CONFIG_SCHED_HPWORK */ /* User space work queue configuration **************************************/ @@ -235,58 +236,42 @@ #endif /* CONFIG_SCHED_USRWORK */ -/* How many worker threads are there? In the user-space phase of a kernel - * build, there will be no more than one. +/* Work queue IDs: * - * Work queue IDs (indices): + * Kernel Work Queues: + * HPWORK: This ID of the high priority work queue that should only be + * used for hi-priority, time-critical, driver bottom-half functions. * - * Kernel Work Queues: There are none and any attempts to use them - * should generate errors. + * LPWORK: This is the ID of the low priority work queue that can be + * used for any purpose. if CONFIG_SCHED_LPWORK is not defined, then + * there is only one kernel work queue and LPWORK == HPWORK. * - * User Work Queue: Will be available if CONFIG_SCHED_USRWORK is defined + * User Work Queue: + * USRWORK: In the kernel phase a a kernel build, there should be no + * references to user-space work queues. That would be an error. + * Otherwise, in a flat build, user applications will use the lower + * priority work queue (if there is one). */ -#if defined(CONFIG_BUILD_PROTECTED) && !defined(__KERNEL__) -# ifdef CONFIG_SCHED_USRWORK -# define NWORKERS 1 -# define USRWORK 0 -# endif +#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) +/* User mode */ + +# define USRWORK 2 /* User mode work queue */ +# define HPWORK USRWORK /* Redirect kernel-mode references */ +# define LPWORK USRWORK + #else +/* Kernel mode */ - /* In a flat build (CONFIG_BUILD_PROTECTED=n) or during the kernel phase of - * the kernel build, there may be 0, 1, or 2 work queues. - * - * Work queue IDs (indices): - * - * Kernel Work Queues: - * HPWORK: This ID of the high priority work queue that should only be - * used for hi-priority, time-critical, driver bottom-half functions. - * - * LPWORK: This is the ID of the low priority work queue that can be - * used for any purpose. if CONFIG_SCHED_LPWORK is not defined, then - * there is only one kernel work queue and LPWORK == HPWORK. - * - * User Work Queue: - * USRWORK: In the kernel phase a a kernel build, there should be no - * references to user-space work queues. That would be an error. - * Otherwise, in a flat build, user applications will use the lower - * priority work queue (if there is one). - */ - -# define HPWORK 0 +# define HPWORK 0 /* High priority, kernel-mode work queue */ # ifdef CONFIG_SCHED_LPWORK -# define LPWORK (HPWORK+1) -# define NWORKERS 2 +# define LPWORK (HPWORK+1) /* Low priority, kernel-mode work queue */ # else -# define LPWORK HPWORK -# define NWORKERS 1 +# define LPWORK HPWORK /* Redirect low-priority references */ # endif +# define USRWORK LPWORK /* Redirect user-mode references */ -# if !defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL) -# define USRWORK LPWORK -# endif - -#endif /* CONFIG_BUILD_PROTECTED && !__KERNEL__ */ +#endif /* CONFIG_SCHED_USRWORK && !__KERNEL__ /**************************************************************************** * Public Types @@ -335,32 +320,6 @@ extern "C" #define EXTERN extern #endif -/* The state of each work queue. This data structure is used internally by - * the OS and worker queue logic and should not be accessed by application - * logic. - */ - -#ifdef CONFIG_BUILD_PROTECTED - - /* Play some games in the kernel mode build to assure that different - * naming is used for the global work queue data structures. This may - * not be necessary but it safer. - */ - -# ifdef __KERNEL__ -EXTERN struct wqueue_s g_kernelwork[NWORKERS]; -# define g_work g_kernelwork -# else -EXTERN struct wqueue_s g_usrwork[NWORKERS]; -# define g_work g_usrwork -# endif - -#else /* CONFIG_BUILD_PROTECTED */ - -EXTERN struct wqueue_s g_work[NWORKERS]; - -#endif /* CONFIG_BUILD_PROTECTED */ - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -427,7 +386,7 @@ int work_usrstart(void); #endif /**************************************************************************** - * Name: work_queue + * Name: work_queue and work_qqueue * * Description: * Queue work to be performed at a later time. All queued work will be @@ -440,6 +399,11 @@ int work_usrstart(void); * from the queue, or (2) work_cancel() has been called to cancel the work * and remove it from the work queue. * + * work_queue() is the application interface. It simply maps the qid to + * the correct work queue and calls work_qqueue(). + * work_qqueue() is the common cancellation logic that operates on the + * particular work queue selected by work_queue(). + * * Input parameters: * qid - The work queue ID * work - The work structure to queue @@ -457,15 +421,22 @@ int work_usrstart(void); int work_queue(int qid, FAR struct work_s *work, worker_t worker, FAR void *arg, uint32_t delay); +int work_qqueue(FAR struct wqueue_s *wqueue, FAR struct work_s *work, + worker_t worker, FAR void *arg, uint32_t delay); /**************************************************************************** - * Name: work_cancel + * Name: work_cancel and work_qcancel; * * Description: * Cancel previously queued work. This removes work from the work queue. - * After work has been canceled, it may be re-queue by calling work_queue() + * After work has been cancelled, it may be re-queue by calling work_queue() * again. * + * work_cancel() is the application interface. It simply maps the qid to + * the correct work queue and calls work_qcancel(). + * work_qcancel() is the common cancellation logic that operates on the + * particular work queue selected by work_cancel(). + * * Input parameters: * qid - The work queue ID * work - The previously queue work structure to cancel @@ -473,8 +444,12 @@ int work_queue(int qid, FAR struct work_s *work, worker_t worker, * Returned Value: * Zero on success, a negated errno on failure * + * -ENOENT - There is no such work queued. + * -EINVAL - An invalid work queue was specified + * ****************************************************************************/ +int work_qcancel(FAR struct wqueue_s *wqueue, FAR struct work_s *work); int work_cancel(int qid, FAR struct work_s *work); /**************************************************************************** diff --git a/libc/wqueue/work_cancel.c b/libc/wqueue/work_cancel.c index d3bcd0608a..9484918c53 100644 --- a/libc/wqueue/work_cancel.c +++ b/libc/wqueue/work_cancel.c @@ -1,7 +1,7 @@ /**************************************************************************** * libc/wqueue/work_cancel.c * - * Copyright (C) 2009-2010, 2012-2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2009-2010, 2012-2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -42,11 +42,12 @@ #include #include #include -#include #include #include +#include "wqueue/wqueue.h" + #ifdef CONFIG_SCHED_WORKQUEUE /**************************************************************************** @@ -74,7 +75,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: work_cancel + * Name: work_qcancel * * Description: * Cancel previously queued work. This removes work from the work queue. @@ -90,16 +91,16 @@ * reported: * * -ENOENT - There is no such work queued. + * -EINVAL - An invalid work queue was specified * ****************************************************************************/ -int work_cancel(int qid, FAR struct work_s *work) +int work_qcancel(FAR struct wqueue_s *wqueue, FAR struct work_s *work) { - FAR struct wqueue_s *wqueue = &g_work[qid]; irqstate_t flags; int ret = -ENOENT; - DEBUGASSERT(work != NULL && (unsigned)qid < NWORKERS); + DEBUGASSERT(work != NULL); /* Cancelling the work is simply a matter of removing the work structure * from the work queue. This must be done with interrupts disabled because @@ -115,7 +116,7 @@ int work_cancel(int qid, FAR struct work_s *work) DEBUGASSERT(work->dq.blink || (FAR dq_entry_t *)work == wqueue->q.head); /* Remove the entry from the work queue and make sure that it is - * mark as availalbe (i.e., the worker field is nullified). + * mark as available (i.e., the worker field is nullified). */ dq_rem((FAR dq_entry_t *)work, &wqueue->q); @@ -127,4 +128,39 @@ int work_cancel(int qid, FAR struct work_s *work) return ret; } +/**************************************************************************** + * Name: work_cancel + * + * Description: + * Cancel previously queued user-mode work. This removes work from the + * user mode work queue. After work has been cancelled, it may be re-queue + * by calling work_queue() again. + * + * Input parameters: + * qid - The work queue ID (must be USRWORK) + * work - The previously queue work structure to cancel + * + * Returned Value: + * Zero (OK) on success, a negated errno on failure. This error may be + * reported: + * + * -ENOENT - There is no such work queued. + * + ****************************************************************************/ + +#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) + +int work_cancel(int qid, FAR struct work_s *work) +{ + if (qid == USRWORK) + { + return work_qcancel(&g_usrwork, work); + } + else + { + return -EINVAL; + } +} + +#endif /* CONFIG_SCHED_USRWORK && !__KERNEL__ */ #endif /* CONFIG_SCHED_WORKQUEUE */ diff --git a/libc/wqueue/work_process.c b/libc/wqueue/work_process.c index fb0ecfaa8a..5171d6ff06 100644 --- a/libc/wqueue/work_process.c +++ b/libc/wqueue/work_process.c @@ -62,30 +62,6 @@ * Public Data ****************************************************************************/ -/* The state of each work queue. */ - -#ifdef CONFIG_BUILD_PROTECTED - - /* Play some games in the kernel mode build to assure that different - * naming is used for the global work queue data structures. This may - * not be necessary but it safer. - * - * In this case g_work is #define'd to be either g_kernelwork or - * g_usrwork in include/nuttx/wqueue.h - */ - -# ifdef __KERNEL__ -struct wqueue_s g_kernelwork[NWORKERS]; -# else -struct wqueue_s g_usrwork[NWORKERS]; -# endif - -#else /* CONFIG_BUILD_PROTECTED */ - -struct wqueue_s g_work[NWORKERS]; - -#endif /* CONFIG_BUILD_PROTECTED */ - /**************************************************************************** * Private Data ****************************************************************************/ diff --git a/libc/wqueue/work_queue.c b/libc/wqueue/work_queue.c index c85f564e54..edfbb166ec 100644 --- a/libc/wqueue/work_queue.c +++ b/libc/wqueue/work_queue.c @@ -1,7 +1,7 @@ /**************************************************************************** * libc/wqueue/work_queue.c * - * Copyright (C) 2009-2011, 2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2009-2011, 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,12 +43,13 @@ #include #include #include -#include #include #include #include +#include "wqueue/wqueue.h" + #ifdef CONFIG_SCHED_WORKQUEUE /**************************************************************************** @@ -76,7 +77,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: work_queue + * Name: work_qqueue * * Description: * Queue work to be performed at a later time. All queued work will be @@ -104,13 +105,12 @@ * ****************************************************************************/ -int work_queue(int qid, FAR struct work_s *work, worker_t worker, - FAR void *arg, uint32_t delay) +int work_qqueue(FAR struct wqueue_s *wqueue, FAR struct work_s *work, + worker_t worker, FAR void *arg, uint32_t delay) { - FAR struct wqueue_s *wqueue = &g_work[qid]; irqstate_t flags; - DEBUGASSERT(work != NULL && (unsigned)qid < NWORKERS); + DEBUGASSERT(work != NULL); /* First, initialize the work structure */ @@ -133,4 +133,49 @@ int work_queue(int qid, FAR struct work_s *work, worker_t worker, return OK; } +/**************************************************************************** + * Name: work_queue + * + * Description: + * Queue user-mode work to be performed at a later time. All queued work + * will be performed on the worker thread of of execution (not the caller's). + * + * The work structure is allocated by caller, but completely managed by + * the work queue logic. The caller should never modify the contents of + * the work queue structure; the caller should not call work_queue() + * again until either (1) the previous work has been performed and removed + * from the queue, or (2) work_cancel() has been called to cancel the work + * and remove it from the work queue. + * + * Input parameters: + * qid - The work queue ID (index) + * work - The work structure to queue + * worker - The worker callback to be invoked. The callback will invoked + * on the worker thread of execution. + * arg - The argument that will be passed to the workder callback when + * int is invoked. + * delay - Delay (in clock ticks) from the time queue until the worker + * is invoked. Zero means to perform the work immediately. + * + * Returned Value: + * Zero on success, a negated errno on failure + * + ****************************************************************************/ + +#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) + +int work_queue(int qid, FAR struct work_s *work, worker_t worker, + FAR void *arg, uint32_t delay) +{ + if (qid == USRWORK) + { + return work_qqueue(&g_usrwork, work, worker, arg, delay); + } + else + { + return -EINVAL; + } +} + +#endif /* CONFIG_SCHED_USRWORK && !__KERNEL__ */ #endif /* CONFIG_SCHED_WORKQUEUE */ diff --git a/libc/wqueue/work_signal.c b/libc/wqueue/work_signal.c index d7459cb974..2d6b9e2388 100644 --- a/libc/wqueue/work_signal.c +++ b/libc/wqueue/work_signal.c @@ -1,7 +1,7 @@ /**************************************************************************** * libc/wqueue/work_signal.c * - * Copyright (C) 2009-2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2009-2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -40,10 +40,11 @@ #include #include -#include #include +#include "wqueue/wqueue.h" + #ifdef CONFIG_SCHED_WORKQUEUE /**************************************************************************** @@ -85,10 +86,28 @@ * ****************************************************************************/ +#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) + int work_signal(int qid) { - DEBUGASSERT((unsigned)qid < NWORKERS); - return kill(g_work[qid].pid, SIGWORK); + int ret; + + if (qid == USRWORK) + { + /* Signal the worker thread */ + + ret = kill(g_usrwork.pid, SIGWORK); + if (ret < 0) + { + int errcode = errno; + return -errcode; + } + } + else + { + return -EINVAL; + } } +#endif /* CONFIG_SCHED_USRWORK && !__KERNEL__ */ #endif /* CONFIG_SCHED_WORKQUEUE */ diff --git a/libc/wqueue/work_usrthread.c b/libc/wqueue/work_usrthread.c index b836e27ca6..f46924a069 100644 --- a/libc/wqueue/work_usrthread.c +++ b/libc/wqueue/work_usrthread.c @@ -48,6 +48,10 @@ * Pre-processor Definitions ****************************************************************************/ +/* The state of the user mode work queue. */ + +extern struct wqueue_s g_usrwork; + /**************************************************************************** * Private Type Declarations ****************************************************************************/ @@ -93,7 +97,7 @@ int work_usrthread(int argc, char *argv[]) * we process items in the work list. */ - work_process(&g_work[USRWORK]); + work_process(&g_usrwork); } return OK; /* To keep some compilers happy */ diff --git a/libc/wqueue/wqueue.h b/libc/wqueue/wqueue.h new file mode 100644 index 0000000000..9f6d2dde5b --- /dev/null +++ b/libc/wqueue/wqueue.h @@ -0,0 +1,71 @@ +/**************************************************************************** + * sched/libc/wqueue.h + * + * Copyright (C) 2014 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. + * + ****************************************************************************/ + +#ifndef __LIBC_WQUEUE_WQUEUE_H +#define __LIBC_WQUEUE_WQUEUE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#ifdef CONFIG_SCHED_WORKQUEUE + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) + +/* The state of the user mode work queue */ + +extern struct wqueue_s g_usrwork; +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#endif /* CONFIG_SCHED_WORKQUEUE */ +#endif /* __LIBC_WQUEUE_WQUEUE_H */ diff --git a/sched/init/os_bringup.c b/sched/init/os_bringup.c index 751a154491..b4a5537d51 100644 --- a/sched/init/os_bringup.c +++ b/sched/init/os_bringup.c @@ -222,11 +222,11 @@ static inline void os_workqueues(void) svdbg("Starting kernel worker thread\n"); #endif - g_work[HPWORK].pid = kernel_thread(HPWORKNAME, CONFIG_SCHED_WORKPRIORITY, - CONFIG_SCHED_WORKSTACKSIZE, - (main_t)work_hpthread, - (FAR char * const *)NULL); - DEBUGASSERT(g_work[HPWORK].pid > 0); + 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 * tasks @@ -236,11 +236,11 @@ static inline void os_workqueues(void) svdbg("Starting low-priority kernel worker thread\n"); - g_work[LPWORK].pid = kernel_thread(LPWORKNAME, CONFIG_SCHED_LPWORKPRIORITY, - CONFIG_SCHED_LPWORKSTACKSIZE, - (main_t)work_lpthread, - (FAR char * const *)NULL); - DEBUGASSERT(g_work[LPWORK].pid > 0); + 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); #endif /* CONFIG_SCHED_LPWORK */ #endif /* CONFIG_SCHED_HPWORK */ diff --git a/sched/wqueue/Make.defs b/sched/wqueue/Make.defs index 86c5edc126..4a375926ae 100644 --- a/sched/wqueue/Make.defs +++ b/sched/wqueue/Make.defs @@ -37,18 +37,20 @@ ifeq ($(CONFIG_SCHED_WORKQUEUE),y) +CSRCS += kwork_queue.c kwork_cancel.c kwork_signal.c + # Add high priority work queue files ifeq ($(CONFIG_SCHED_HPWORK),y) -CSRCS += work_hpthread.c +CSRCS += kwork_hpthread.c endif # Add low priority work queue files ifeq ($(CONFIG_SCHED_LPWORK),y) -CSRCS += work_lpthread.c +CSRCS += kwork_lpthread.c ifeq ($(CONFIG_PRIORITY_INHERITANCE),y) -CSRCS += work_inherit.c +CSRCS += kwork_inherit.c endif # CONFIG_PRIORITY_INHERITANCE endif # CONFIG_SCHED_LPWORK diff --git a/sched/wqueue/kwork_cancel.c b/sched/wqueue/kwork_cancel.c new file mode 100644 index 0000000000..92e70e20d4 --- /dev/null +++ b/sched/wqueue/kwork_cancel.c @@ -0,0 +1,120 @@ +/**************************************************************************** + * sched/wqueue/kwork_cancel.c + * + * Copyright (C) 2014 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 "wqueue/wqueue.h" + +#ifdef CONFIG_SCHED_WORKQUEUE + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: work_cancel + * + * Description: + * Cancel previously queued user-mode work. This removes work from the + * user mode work queue. After work has been cancelled, it may be re-queue + * by calling work_queue() again. + * + * Input parameters: + * qid - The work queue ID (must be USRWORK) + * work - The previously queue work structure to cancel + * + * Returned Value: + * Zero (OK) on success, a negated errno on failure. This error may be + * reported: + * + * -ENOENT - There is no such work queued. + * -EINVAL - An invalid work queue was specified + * + ****************************************************************************/ + +int work_cancel(int qid, FAR struct work_s *work) +{ +#ifdef CONFIG_SCHED_HPWORK + if (qid == HPWORK) + { + /* Cancel high priority work */ + + return work_qcancel(&g_hpwork, work); + } + else +#endif +#ifdef CONFIG_SCHED_LPWORK + if (qid == LPWORK) + { + /* Cancel low priority work */ + + return work_qcancel(&g_lpwork, work); + } + else +#endif + { + return -EINVAL; + } +} + +#endif /* CONFIG_SCHED_WORKQUEUE */ diff --git a/sched/wqueue/work_hpthread.c b/sched/wqueue/kwork_hpthread.c similarity index 97% rename from sched/wqueue/work_hpthread.c rename to sched/wqueue/kwork_hpthread.c index 7df4ad5de0..88b2e502d5 100644 --- a/sched/wqueue/work_hpthread.c +++ b/sched/wqueue/kwork_hpthread.c @@ -58,6 +58,10 @@ * Public Data ****************************************************************************/ +/* The state of the kernel mode, high priority work queue. */ + +struct wqueue_s g_hpwork; + /**************************************************************************** * Private Data ****************************************************************************/ @@ -113,7 +117,7 @@ int work_hpthread(int argc, char *argv[]) * we process items in the work list. */ - work_process(&g_work[HPWORK]); + work_process(&g_hpwork); } return OK; /* To keep some compilers happy */ diff --git a/sched/wqueue/work_inherit.c b/sched/wqueue/kwork_inherit.c similarity index 99% rename from sched/wqueue/work_inherit.c rename to sched/wqueue/kwork_inherit.c index 7e6c1e6f48..ad71b1ba32 100644 --- a/sched/wqueue/work_inherit.c +++ b/sched/wqueue/kwork_inherit.c @@ -91,7 +91,7 @@ void lpwork_boostpriority(uint8_t reqprio) * thread from the process ID. */ - wpid = g_work[LPWORK].pid; + wpid = g_lpwork.pid; wtcb = sched_gettcb(wpid); /* Prevent context switches until we get the priorities right */ @@ -214,7 +214,7 @@ void lpwork_restorepriority(uint8_t reqprio) * thread from the process ID. */ - wpid = g_work[LPWORK].pid; + wpid = g_lpwork.pid; wtcb = sched_gettcb(wpid); /* Prevent context switches until we get the priorities right */ diff --git a/sched/wqueue/work_lpthread.c b/sched/wqueue/kwork_lpthread.c similarity index 97% rename from sched/wqueue/work_lpthread.c rename to sched/wqueue/kwork_lpthread.c index 70dcc7e7a4..c3991dc4d3 100644 --- a/sched/wqueue/work_lpthread.c +++ b/sched/wqueue/kwork_lpthread.c @@ -58,6 +58,10 @@ * Public Data ****************************************************************************/ +/* The state of the kernel mode, low priority work queue(s). */ + +struct wqueue_s g_lpwork; + /**************************************************************************** * Private Data ****************************************************************************/ @@ -110,7 +114,7 @@ int work_lpthread(int argc, char *argv[]) * we process items in the work list. */ - work_process(&g_work[LPWORK]); + work_process(&g_lpwork); } return OK; /* To keep some compilers happy */ diff --git a/sched/wqueue/kwork_queue.c b/sched/wqueue/kwork_queue.c new file mode 100644 index 0000000000..357b9fe840 --- /dev/null +++ b/sched/wqueue/kwork_queue.c @@ -0,0 +1,133 @@ +/**************************************************************************** + * sched/wqueue/kwork_queue.c + * + * Copyright (C) 2014 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 "wqueue/wqueue.h" + +#ifdef CONFIG_SCHED_WORKQUEUE + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: work_queue + * + * Description: + * Queue kernel-mode work to be performed at a later time. All queued work + * will be performed on the worker thread of of execution (not the caller's). + * + * The work structure is allocated by caller, but completely managed by + * the work queue logic. The caller should never modify the contents of + * the work queue structure; the caller should not call work_queue() + * again until either (1) the previous work has been performed and removed + * from the queue, or (2) work_cancel() has been called to cancel the work + * and remove it from the work queue. + * + * Input parameters: + * qid - The work queue ID (index) + * work - The work structure to queue + * worker - The worker callback to be invoked. The callback will invoked + * on the worker thread of execution. + * arg - The argument that will be passed to the workder callback when + * int is invoked. + * delay - Delay (in clock ticks) from the time queue until the worker + * is invoked. Zero means to perform the work immediately. + * + * Returned Value: + * Zero on success, a negated errno on failure + * + ****************************************************************************/ + +#if defined(CONFIG_SCHED_USRWORK) && !defined(__KERNEL__) + +int work_queue(int qid, FAR struct work_s *work, worker_t worker, + FAR void *arg, uint32_t delay) +{ +#ifdef CONFIG_SCHED_HPWORK + if (qid == HPWORK) + { + /* Cancel high priority work */ + + return work_qqueue(&g_hpwork, work, worker, arg, delay); + } + else +#endif +#ifdef CONFIG_SCHED_LPWORK + if (qid == LPWORK) + { + /* Cancel low priority work */ + + return work_qqueue(&g_lpwork, work, worker, arg, delay); + } + else +#endif + { + return -EINVAL; + } +} + +#endif /* CONFIG_SCHED_USRWORK && !__KERNEL__ */ +#endif /* CONFIG_SCHED_WORKQUEUE */ diff --git a/sched/wqueue/kwork_signal.c b/sched/wqueue/kwork_signal.c new file mode 100644 index 0000000000..1f99af7b55 --- /dev/null +++ b/sched/wqueue/kwork_signal.c @@ -0,0 +1,127 @@ +/**************************************************************************** + * sched/wqueue/work_signal.c + * + * Copyright (C) 2014 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 "wqueue/wqueue.h" + +#ifdef CONFIG_SCHED_WORKQUEUE + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ +/**************************************************************************** + * Name: work_signal + * + * Description: + * Signal the worker thread to process the work queue now. This function + * is used internally by the work logic but could also be used by the + * user to force an immediate re-assessment of pending work. + * + * Input parameters: + * qid - The work queue ID + * + * Returned Value: + * Zero on success, a negated errno on failure + * + ****************************************************************************/ + +int work_signal(int qid) +{ + pid_t pid; + int ret; + + /* Get the process ID of the worker thread */ + +#ifdef CONFIG_SCHED_HPWORK + if (qid == HPWORK) + { + pid = g_hpwork.pid; + } + else +#endif +#ifdef CONFIG_SCHED_LPWORK + if (qid == LPWORK) + { + pid = g_lpwork.pid; + } + else +#endif + { + return -EINVAL; + } + + /* Signal the worker thread */ + + ret = kill(pid, SIGWORK); + if (ret < 0) + { + int errcode = errno; + return -errcode; + } + + return OK; +} + +#endif /* CONFIG_SCHED_WORKQUEUE */ diff --git a/sched/wqueue/wqueue.h b/sched/wqueue/wqueue.h index acd03409c4..1c1127ee81 100644 --- a/sched/wqueue/wqueue.h +++ b/sched/wqueue/wqueue.h @@ -53,9 +53,21 @@ ****************************************************************************/ /**************************************************************************** - * Global Variables + * Public Data ****************************************************************************/ +#ifdef CONFIG_SCHED_HPWORK +/* The state of the kernel mode, high priority work queue. */ + +extern struct wqueue_s g_hpwork; +#endif + +#ifdef CONFIG_SCHED_LPWORK +/* The state of the kernel mode, low priority work queue(s). */ + +extern struct wqueue_s g_lpwork; +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/