wqueue: Support custom user work queues. - #19982
Conversation
|
Prevent work_queue_free() from destroying predefined queues or freeing a custom queue from one of its own callbacks. Mark teardown under the queue lock, reject new submissions, return pending work to its owner, and wait for every worker before releasing queue resources. Clean up partially created worker pools, reject invalid delays, safely replace pending periodic work, and make synchronous cancellation wait for every concurrent callback using the same work structure. Tested on an STM32H7 PX4 FMUv6C with the matching ostest suite in Flat and Protected kernel builds. Assisted-by: Codex:GPT-5 Signed-off-by: DuoYuWang <thirteenking.wang@gmail.com>
Implement the handle-based create, queue, priority, cancellation, and teardown APIs for CONFIG_LIBC_USRWORK. Custom queues use configurable pthread worker pools while the predefined USRWORK queue remains available. Match scheduler-backend delay, replacement, cancellation, and lifecycle semantics. Restrict the libc backend to task context because it uses blocking synchronization. Tested on an STM32H7 PX4 FMUv6C with ostest wqueue in Protected user space. Assisted-by: Codex:GPT-5 Signed-off-by: DuoYuWang <thirteenking.wang@gmail.com>
Replace the local EINTR retry loop with nxsem_wait_uninterruptible(). This keeps the master implementation aligned with the libc semaphore API without changing cancellation behavior. Keep the cleanup separate so release branches where the helper is not available to Protected user space can use the functional commit without a downstream compatibility patch. Assisted-by: Codex:GPT-5 Signed-off-by: DuoYuWang <thirteenking.wang@gmail.com>
Describe the handle-based custom queue APIs, worker-pool creation and teardown, periodic requeue, cancellation semantics, and return values. Clarify that libc user work queue APIs use blocking synchronization and must only be called from task context, while kernel and Flat queue and asynchronous cancellation operations remain ISR-safe. Assisted-by: Codex:GPT-5 Signed-off-by: DuoYuWang <thirteenking.wang@gmail.com>
9dd9a74 to
d3b65e9
Compare
|
Updated the series organization and repeated the complete master hardware The series is now split into scheduler functionality, libc functionality with Fresh Three USB NSH runs per backend: All assertions passed. Steady-state heap usage remained unchanged after the The final PR WQ source tree matches the runtime-tested source tree. Full-range |
| * the wqueue timer. | ||
| FAR sem_t *sync_wait = NULL; | ||
| int wndx; | ||
| FAR struct kworker_s *worker = wq_get_worker(wqueue); |
There was a problem hiding this comment.
move before line 59
|
|
||
| pthread_attr_init(&attr); | ||
| pthread_attr_setstacksize(&attr, CONFIG_LIBC_USRWORKSTACKSIZE); | ||
|
|
There was a problem hiding this comment.
add pthread_attr_setdetachstate and remove line 617
|
|
||
| return (pid_t)usrwork; | ||
| pthread_detach(g_usrworker.tid); | ||
| return (pid_t)g_usrworker.tid; |
There was a problem hiding this comment.
move after line 619 and remove line 601
| list_initialize(&g_usrwork.q); | ||
| g_usrwork.exit = false; | ||
| g_usrworker.work = NULL; | ||
| g_usrworker.wait_count = 0; |
There was a problem hiding this comment.
why need zero again
|
|
||
| /* Initialize the work queue */ | ||
|
|
||
| list_initialize(&g_usrwork.q); |
There was a problem hiding this comment.
move the init to the variable definition
| int semcount; | ||
| int ret; | ||
|
|
||
| if (wqueue == NULL || work == NULL || worker == NULL || delay < 0 || |
There was a problem hiding this comment.
move delay < 0 to next line
|
|
||
| if (work->worker != NULL) | ||
| { | ||
| list_delete(&work->node); |
There was a problem hiding this comment.
it's wrong to modify the list without lock, let's call cancel directly
| */ | ||
|
|
||
| if (curr == head) | ||
| if (delay == 0 || curr == head) |
There was a problem hiding this comment.
why need check delay
| bool retimer; | ||
|
|
||
| if (wqueue == NULL || work == NULL || worker == NULL || | ||
| if (wqueue == NULL || work == NULL || worker == NULL || delay < 0 || |
There was a problem hiding this comment.
move delay < 0 || to next line
| int ret = OK; | ||
|
|
||
| if (wqueue == NULL || work == NULL || worker == NULL || | ||
| if (wqueue == NULL || work == NULL || worker == NULL || delay < 0 || |
There was a problem hiding this comment.
move to delay < 0 || next line
Summary
builds through the existing handle-based work queue API.
libs/libc/wqueuewhileretaining the predefined
USRWORKqueue.submissions during teardown, release pending work ownership, wait for all
workers, clean up partial creation, and synchronously cancel every callback
using the same
work_s.and idempotent cancellation behavior between scheduler and libc backends.
calls to task context; they must not be called from an ISR.
Commit organization
The libc functional commit deliberately contains an explicit
nxsem_wait()retry on-EINTR. The next master-only cleanup commit replacesthat loop with
nxsem_wait_uninterruptible()and produces the same behavior.An older release branch where that helper is not available to Protected user
space can therefore cherry-pick the scheduler and libc functional commits,
omit only the helper cleanup, and take the documentation commit without a
downstream-only compatibility patch.
Please keep the functional and helper-cleanup commits separate for that
reason. If a squashed history is preferred, that older PX4 release branch
will not be supported by this series rather than carrying a special NuttX
patch downstream.
Impact
work queues with configurable priority, stack size, and worker count.
USRWORK,HPWORK, andLPWORKusersremain supported. New user custom-queue APIs are task-context only.
Documentation/reference/os/wqueue.rstand publicheader comments describe custom queues, teardown, errors, and execution
context.
successful no-op, matching the scheduler backend's existing behavior.
Testing
Build host:
Target:
c6b349b0234466a54a624ae35ba77adb73a9ea0epatch-equivalent STM32H7 Protected-memory series from stm32h7: Fix Protected user SRAM placement and attributes. #19983.
make distclean, uploaded throughthe PX4 bootloader, and tested through USB NSH with
minicom.Before change:
Build output after change:
Runtime commands and results, three runs per backend:
Steady-state memory after the first run remained unchanged through all later
runs:
Every run covered one- and two-worker custom queues, explicit caller
priorities, invalid arguments, periodic requeue, pending replacement,
synchronous cancellation, two concurrent callbacks using one
work_s, foursimultaneous queues with 32 work items, self-destruction rejection, and
pending/running teardown. All assertions passed, heap usage did not grow, and
all custom worker pools completed teardown.
The final PR WQ source tree matches the runtime-tested source tree.
Validation:
The documentation HTML build was not run locally because
sphinx-buildisnot installed on the build host.
PR verification Self-Check