diff --git a/Documentation/os/scheduling/wqueue.rst b/Documentation/os/scheduling/wqueue.rst index cb390d6365c1f..24604a0af3684 100644 --- a/Documentation/os/scheduling/wqueue.rst +++ b/Documentation/os/scheduling/wqueue.rst @@ -262,6 +262,23 @@ Work Queue Interfaces :return: A work queue handle on success; ``NULL`` on failure. +.. c:function:: FAR struct kwork_wqueue_s *work_queue_create_pthread( \ + FAR const char *name, int priority, FAR void *stack_addr, \ + int stack_size, int nthreads) + + Create a custom work queue like ``work_queue_create()``, with workers + that are pthreads of the calling task. The work then runs with the file + descriptors and the other resources of that task. In the flat build, + ``work_queue_create()`` creates kernel threads instead; in user mode + both interfaces are the same. The workers end with the task, so the + queue must be freed before the task exits. + + This interface must only be called from task context and needs pthread + support. It is not available to kernel code in the protected and kernel + builds. + + :return: A work queue handle on success; ``NULL`` on failure. + .. c:function:: int work_queue_free(FAR struct kwork_wqueue_s *wqueue) Destroy a custom queue, discard pending work, and wait for all running diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h index 5c0b7ee9c06e7..08775c15b4336 100644 --- a/include/nuttx/wqueue.h +++ b/include/nuttx/wqueue.h @@ -357,6 +357,34 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name, FAR void *stack_addr, int stack_size, int nthreads); +/**************************************************************************** + * Name: work_queue_create_pthread + * + * Description: + * Create a custom work queue whose workers are pthreads of the calling + * task, so that the work runs with the file descriptors and the other + * resources of that task. In the flat build, work_queue_create() + * creates kernel threads instead; in user mode both are the same. + * The workers end with the task: free the queue before the task exits. + * This function must only be called from task context. + * + * Input Parameters: + * The same as work_queue_create(). + * + * Returned Value: + * The work queue handle returned on success. Otherwise, NULL + * + ****************************************************************************/ + +#if !defined(CONFIG_DISABLE_PTHREAD) && \ + (defined(CONFIG_BUILD_FLAT) || !defined(__KERNEL__)) +FAR struct kwork_wqueue_s *work_queue_create_pthread(FAR const char *name, + int priority, + FAR void *stack_addr, + int stack_size, + int nthreads); +#endif + /**************************************************************************** * Name: work_queue_dispatch * diff --git a/libs/libc/wqueue/work_usrthread.c b/libs/libc/wqueue/work_usrthread.c index c0a8a7cf6d640..e6ab471726238 100644 --- a/libs/libc/wqueue/work_usrthread.c +++ b/libs/libc/wqueue/work_usrthread.c @@ -452,6 +452,24 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name, return (FAR struct kwork_wqueue_s *)wqueue; } +/**************************************************************************** + * Name: work_queue_create_pthread + * + * Description: + * User-mode workers are always pthreads of the calling task. + * + ****************************************************************************/ + +FAR struct kwork_wqueue_s *work_queue_create_pthread(FAR const char *name, + int priority, + FAR void *stack_addr, + int stack_size, + int nthreads) +{ + return work_queue_create(name, priority, stack_addr, stack_size, + nthreads); +} + /**************************************************************************** * Name: work_queue_free * diff --git a/sched/wqueue/CMakeLists.txt b/sched/wqueue/CMakeLists.txt index 90e012b098e72..ec4ef7addbc99 100644 --- a/sched/wqueue/CMakeLists.txt +++ b/sched/wqueue/CMakeLists.txt @@ -26,6 +26,10 @@ if(CONFIG_SCHED_WORKQUEUE) set(SRCS kwork_queue.c kwork_cancel.c kwork_thread.c) + if(CONFIG_BUILD_FLAT AND NOT CONFIG_DISABLE_PTHREAD) + list(APPEND SRCS kwork_pthread.c) + endif() + # Add low priority work queue files if(CONFIG_PRIORITY_INHERITANCE) diff --git a/sched/wqueue/Make.defs b/sched/wqueue/Make.defs index 04e735dae9237..6c59e0214cca5 100644 --- a/sched/wqueue/Make.defs +++ b/sched/wqueue/Make.defs @@ -26,6 +26,12 @@ ifeq ($(CONFIG_SCHED_WORKQUEUE),y) CSRCS += kwork_queue.c kwork_thread.c kwork_cancel.c +ifeq ($(CONFIG_BUILD_FLAT),y) +ifneq ($(CONFIG_DISABLE_PTHREAD),y) +CSRCS += kwork_pthread.c +endif +endif + ifeq ($(CONFIG_PRIORITY_INHERITANCE),y) CSRCS += kwork_inherit.c endif # CONFIG_PRIORITY_INHERITANCE diff --git a/sched/wqueue/kwork_pthread.c b/sched/wqueue/kwork_pthread.c new file mode 100644 index 0000000000000..a8c0208c62044 --- /dev/null +++ b/sched/wqueue/kwork_pthread.c @@ -0,0 +1,169 @@ +/**************************************************************************** + * sched/wqueue/kwork_pthread.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include + +#include "wqueue/wqueue.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct work_pthread_s +{ + FAR struct kwork_wqueue_s *wqueue; + FAR struct kworker_s *kworker; + sem_t started; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static FAR void *work_pthread(FAR void *arg) +{ + FAR struct work_pthread_s *start = arg; + FAR struct kwork_wqueue_s *wqueue = start->wqueue; + FAR struct kworker_s *kworker = start->kworker; + + /* start is on the stack of the creator: done with it */ + + nxsem_post(&start->started); + work_thread_loop(wqueue, kworker); + return NULL; +} + +/**************************************************************************** + * Name: work_pthread_create + * + * Description: + * Create one worker as a pthread of the calling task. + * + * Returned Value: + * The thread ID of the worker on success, a negated errno value on + * failure. + * + ****************************************************************************/ + +static int work_pthread_create(FAR const char *name, int priority, + FAR void *stack_addr, int stack_size, + FAR struct kwork_wqueue_s *wqueue, + FAR struct kworker_s *kworker) +{ + struct work_pthread_s start; + struct sched_param param; + pthread_attr_t attr; + pthread_t thread; + int ret; + + start.wqueue = wqueue; + start.kworker = kworker; + nxsem_init(&start.started, 0, 0); + + pthread_attr_init(&attr); + if (stack_addr != NULL) + { + ret = pthread_attr_setstack(&attr, stack_addr, stack_size); + } + else + { + ret = pthread_attr_setstacksize(&attr, stack_size); + } + + if (ret == 0) + { + ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); + } + + if (ret == 0) + { + param.sched_priority = priority; + ret = pthread_attr_setschedparam(&attr, ¶m); + } + + if (ret == 0) + { + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + } + + /* work_queue_free() waits for the workers through exsem */ + + if (ret == 0) + { + ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + } + + if (ret == 0) + { + ret = pthread_create(&thread, &attr, work_pthread, &start); + } + + pthread_attr_destroy(&attr); + + if (ret == 0) + { + pthread_setname_np(thread, name); + nxsem_wait_uninterruptible(&start.started); + ret = (int)thread; + } + else + { + ret = -ret; + } + + nxsem_destroy(&start.started); + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: work_queue_create_pthread + * + * Description: + * Create a custom work queue whose workers are pthreads of the calling + * task. The parameters and the returned value are those of + * work_queue_create(). + * + ****************************************************************************/ + +FAR struct kwork_wqueue_s *work_queue_create_pthread(FAR const char *name, + int priority, + FAR void *stack_addr, + int stack_size, + int nthreads) +{ + return work_queue_new(name, priority, stack_addr, stack_size, nthreads, + work_pthread_create); +} diff --git a/sched/wqueue/kwork_thread.c b/sched/wqueue/kwork_thread.c index 65652886032c0..49315e786c6d9 100644 --- a/sched/wqueue/kwork_thread.c +++ b/sched/wqueue/kwork_thread.c @@ -274,15 +274,39 @@ static int work_thread(int argc, FAR char *argv[]) kworker = (FAR struct kworker_s *) ((uintptr_t)strtoul(argv[2], NULL, 16)); - while (!work_dispatch(wqueue, kworker)) - { - /* Wait for the semaphore to be posted by the wqueue timer. */ + work_thread_loop(wqueue, kworker); + return OK; +} - nxsem_wait_uninterruptible(&wqueue->sem); - } +/**************************************************************************** + * Name: work_kthread_create + * + * Description: + * Create one worker as a kernel thread. + * + * Returned Value: + * The task ID of the worker on success, a negated errno value on + * failure. + * + ****************************************************************************/ - nxsem_post(&wqueue->exsem); - return OK; +static int work_kthread_create(FAR const char *name, int priority, + FAR void *stack_addr, int stack_size, + FAR struct kwork_wqueue_s *wqueue, + FAR struct kworker_s *kworker) +{ + FAR char *argv[3]; + char arg0[32]; + char arg1[32]; + + snprintf(arg0, sizeof(arg0), "%p", wqueue); + snprintf(arg1, sizeof(arg1), "%p", kworker); + argv[0] = arg0; + argv[1] = arg1; + argv[2] = NULL; + + return kthread_create_with_stack(name, priority, stack_addr, stack_size, + work_thread, argv); } /**************************************************************************** @@ -298,6 +322,7 @@ static int work_thread(int argc, FAR char *argv[]) * stack_addr - Stack buffer of the new task * stack_size - size (in bytes) of the stack needed * wqueue - Work queue instance + * create - Creates one worker * * Returned Value: * A negated errno value is returned on failure. @@ -306,12 +331,10 @@ static int work_thread(int argc, FAR char *argv[]) static int work_thread_create(FAR const char *name, int priority, FAR void *stack_addr, int stack_size, - FAR struct kwork_wqueue_s *wqueue) + FAR struct kwork_wqueue_s *wqueue, + work_create_t create) { FAR struct kworker_s *worker = wq_get_worker(wqueue); - FAR char *argv[3]; - char arg0[32]; - char arg1[32]; irqstate_t flags; int created = 0; int initialized = 0; @@ -330,12 +353,6 @@ static int work_thread_create(FAR const char *name, int priority, nxsem_init(&worker[wndx].wait, 0, 0); initialized++; - snprintf(arg0, sizeof(arg0), "%p", wqueue); - snprintf(arg1, sizeof(arg1), "%p", &worker[wndx]); - argv[0] = arg0; - argv[1] = arg1; - argv[2] = NULL; - /* In case of the stack_addr is NULL */ if (stack_addr) @@ -343,8 +360,8 @@ static int work_thread_create(FAR const char *name, int priority, stack = (FAR void *)((uintptr_t)stack_addr + wndx * stack_size); } - pid = kthread_create_with_stack(name, priority, stack, - stack_size, work_thread, argv); + pid = create(name, priority, stack, stack_size, wqueue, + &worker[wndx]); if (pid <= 0) { @@ -397,6 +414,32 @@ static int work_thread_create(FAR const char *name, int priority, * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: work_thread_loop + * + * Description: + * Run the work of a queue on one of its workers until the queue is + * freed. + * + * Input Parameters: + * wqueue - The work queue. + * kworker - The worker of the calling thread. + * + ****************************************************************************/ + +void work_thread_loop(FAR struct kwork_wqueue_s *wqueue, + FAR struct kworker_s *kworker) +{ + while (!work_dispatch(wqueue, kworker)) + { + /* Wait for the semaphore to be posted by the wqueue timer. */ + + nxsem_wait_uninterruptible(&wqueue->sem); + } + + nxsem_post(&wqueue->exsem); +} + /**************************************************************************** * Name: work_queue_dispatch * @@ -450,28 +493,20 @@ void work_timer_expired(wdparm_t arg) } /**************************************************************************** - * Name: work_queue_create + * Name: work_queue_new * * Description: - * Create a custom work queue and return its handle. This function creates - * a pool containing nthreads workers. - * - * Input Parameters: - * name - Name of the new task - * priority - Priority of the new task - * stack_addr - Stack buffer of the new task - * stack_size - size (in bytes) of the stack needed - * nthreads - Number of worker threads to create - * - * Returned Value: - * The work queue handle returned on success. Otherwise, NULL + * Allocate a custom work queue and create its workers with create. + * The other parameters and the returned value are those of + * work_queue_create(). * ****************************************************************************/ -FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name, - int priority, - FAR void *stack_addr, - int stack_size, int nthreads) +FAR struct kwork_wqueue_s *work_queue_new(FAR const char *name, + int priority, + FAR void *stack_addr, + int stack_size, int nthreads, + work_create_t create) { FAR struct kwork_wqueue_s *wqueue; int ret; @@ -504,7 +539,8 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name, /* Create the work queue thread pool */ - ret = work_thread_create(name, priority, stack_addr, stack_size, wqueue); + ret = work_thread_create(name, priority, stack_addr, stack_size, wqueue, + create); if (ret < 0) { nxsem_destroy(&wqueue->sem); @@ -516,6 +552,34 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name, return wqueue; } +/**************************************************************************** + * Name: work_queue_create + * + * Description: + * Create a custom work queue and return its handle. This function creates + * a pool containing nthreads workers. + * + * Input Parameters: + * name - Name of the new task + * priority - Priority of the new task + * stack_addr - Stack buffer of the new task + * stack_size - size (in bytes) of the stack needed + * nthreads - Number of worker threads to create + * + * Returned Value: + * The work queue handle returned on success. Otherwise, NULL + * + ****************************************************************************/ + +FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name, + int priority, + FAR void *stack_addr, + int stack_size, int nthreads) +{ + return work_queue_new(name, priority, stack_addr, stack_size, nthreads, + work_kthread_create); +} + /**************************************************************************** * Name: work_queue_free * @@ -787,11 +851,13 @@ int work_start_highpri(void) CONFIG_SCHED_HPWORKPRIORITY, hp_work_stack, CONFIG_SCHED_HPWORKSTACKSIZE, - (FAR struct kwork_wqueue_s *)&g_hpwork); + (FAR struct kwork_wqueue_s *)&g_hpwork, + work_kthread_create); #else return work_thread_create(HPWORKNAME, CONFIG_SCHED_HPWORKPRIORITY, NULL, CONFIG_SCHED_HPWORKSTACKSIZE, - (FAR struct kwork_wqueue_s *)&g_hpwork); + (FAR struct kwork_wqueue_s *)&g_hpwork, + work_kthread_create); #endif } #endif /* CONFIG_SCHED_HPWORK */ @@ -827,12 +893,14 @@ int work_start_lowpri(void) CONFIG_SCHED_LPWORKPRIORITY, lp_work_stack, CONFIG_SCHED_LPWORKSTACKSIZE, - (FAR struct kwork_wqueue_s *)&g_lpwork); + (FAR struct kwork_wqueue_s *)&g_lpwork, + work_kthread_create); #else return work_thread_create(LPWORKNAME, CONFIG_SCHED_LPWORKPRIORITY, NULL, CONFIG_SCHED_LPWORKSTACKSIZE, - (FAR struct kwork_wqueue_s *)&g_lpwork); + (FAR struct kwork_wqueue_s *)&g_lpwork, + work_kthread_create); #endif } #endif /* CONFIG_SCHED_LPWORK */ diff --git a/sched/wqueue/wqueue.h b/sched/wqueue/wqueue.h index fe8f64bbecc5d..49a604fdc584e 100644 --- a/sched/wqueue/wqueue.h +++ b/sched/wqueue/wqueue.h @@ -69,6 +69,16 @@ struct kworker_s int16_t wait_count; }; +/* Creates one worker of a custom queue: returns its thread ID or a + * negated errno value. + */ + +struct kwork_wqueue_s; +typedef CODE int (*work_create_t)(FAR const char *name, int priority, + FAR void *stack_addr, int stack_size, + FAR struct kwork_wqueue_s *wqueue, + FAR struct kworker_s *kworker); + /* This structure defines the state of one kernel-mode work queue */ struct kwork_wqueue_s @@ -283,6 +293,32 @@ bool work_remove(FAR struct kwork_wqueue_s *wqueue, void work_timer_expired(wdparm_t arg); +/**************************************************************************** + * Name: work_thread_loop + * + * Description: + * Run the work of a queue on one of its workers until the queue is + * freed. + * + ****************************************************************************/ + +void work_thread_loop(FAR struct kwork_wqueue_s *wqueue, + FAR struct kworker_s *kworker); + +/**************************************************************************** + * Name: work_queue_new + * + * Description: + * Allocate a custom work queue and create its workers with create. + * + ****************************************************************************/ + +FAR struct kwork_wqueue_s *work_queue_new(FAR const char *name, + int priority, + FAR void *stack_addr, + int stack_size, int nthreads, + work_create_t create); + /**************************************************************************** * Name: work_timer_reset *