Skip to content

sched/wqueue: Add work_queue_create_pthread() - #22268

Open
chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:wqueue-pthread
Open

chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:wqueue-pthread

Conversation

@chengkai15

@chengkai15 chengkai15 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In the flat build, the workers of a custom queue created with work_queue_create() are kernel threads, which do not share the file descriptors of the task that created the queue. A callback cannot use the files that the task opened: write() fails with EBADF, or reaches another file that has the same number in the kernel thread group.

This shows up when an application moves its deferred work to a NuttX work queue: the Zephyr Bluetooth host of nuttx-apps#3977 runs its work queues on work_queue_create(), and the HCI driver writes /dev/ttyHCI0 from them. The PR works around it with an extra TX work queue that opens the device itself. With this change the host queues can be pthreads of the application and write directly.

Add work_queue_create_pthread(), the same as work_queue_create() except that the workers are pthreads of the calling task:

  • flat build: the creation of one worker is passed to a common work_queue_new(). work_queue_create() and HPWORK/LPWORK keep their kernel threads. The pthread worker is in kwork_pthread.c, which is linked only when it is used.
  • protected and kernel builds (user mode): the workers are already pthreads of the caller, so work_queue_create_pthread() is work_queue_create(). It is not declared for kernel code.

The workers end with the task, so the queue must be freed before the task exits. This is documented in the header and in Documentation/os/scheduling/wqueue.rst.

Impact

  • New API only, no change to existing callers. work_queue_create() behaves as before.
  • Code size when the new API is not used (mps3-an547:nsh, flat, arm-none-eabi-gcc 10.3), text: 360223 -> 360351 (+128 bytes, from work_queue_new() and the creation callback).
  • Documentation updated.
  • The regression test is in nuttx-apps (testing/ostest/wqueue.c); that PR will depend on this one.

Testing

The new ostest case creates a queue with work_queue_create_pthread(..., 2), opens /dev/null in the test task, and checks in a callback that getpid() is the pid of the task and that write() on that fd succeeds. It then runs the existing sync cancel, parallel cancel and priority tests on the queue.

master (b5db91f), sim:ostest, the same case with work_queue_create() (the problem):

wqueue_test: pthread workers
wqueue_test: worker pid = 0, expect = 5, write = -1
dump_assert_info: Current Version: NuttX  13.1.1-RC0 b5db91fe0d Nov 24 2026 19:51:44 sim
dump_assert_info: Assertion failed : at file: wqueue.c:1180 task: ostest process: ostest 0x4005045d

This PR, sim:ostest (flat), full ostest, 1 loop:

wqueue_test: pthread workers
wqueue_test: priority = 100, expect = 100
wqueue_test: worker pid = 5, expect = 5, write = 1
wqueue_test: sync cancel finished = 1, expect = 1
wqueue_test: parallel callbacks = 2, expect = 2
wqueue_test: pthread workers done
...
ostest_main: Exiting with status 0

sim:smp (4 CPUs, with CONFIG_TESTING_OSTEST_WQUEUE), ostest wqueue: passed 5 times out of 5.

wqueue_test: pthread workers
wqueue_test: priority = 100, expect = 100
wqueue_test: worker pid = 9, expect = 9, write = 1
wqueue_test: sync cancel finished = 1, expect = 1
wqueue_test: parallel callbacks = 2, expect = 2
wqueue_test: pthread workers done

mps3-an547:knsh (protected) + CONFIG_LIBC_USRWORK, qemu-system-arm, ostest wqueue:

wqueue_test: pthread workers
wqueue_test: priority = 100, expect = 100
wqueue_test: worker pid = 5, expect = 5, write = 1
wqueue_test: sync cancel finished = 1, expect = 1
wqueue_test: parallel callbacks = 2, expect = 2
wqueue_test: pthread workers done

qemu-armv7a:knsh (kernel) + CONFIG_LIBC_USRWORK, qemu-system-arm, /system/bin/ostest wqueue:

wqueue_test: pthread workers
wqueue_test: priority = 100, expect = 100
wqueue_test: worker pid = 4, expect = 4, write = 1
wqueue_test: sync cancel finished = 1, expect = 1
wqueue_test: parallel callbacks = 2, expect = 2
wqueue_test: pthread workers done

On protected and kernel, ostest wqueue fails on master before it reaches this case, in code that this PR does not change. For these two runs those checks were disabled locally:

  • protected and kernel: on a libc custom queue, work_cancel_wq() / work_cancel_sync_wq() of idle work return 0 instead of -ENOENT (api_validation_test()). The master build fails at the same assertion.
  • kernel: the callbacks of the predefined USRWORK queue do not run (usrwork_test() reports -110).

Builds: sim:ostest and sim:smp (Make), sim:ostest (CMake), mps3-an547:nsh, mps3-an547:knsh, qemu-armv7a:knsh. Not tested on real hardware.

In the flat build, the workers of a custom queue created with
work_queue_create() are kernel threads.  They do not share the file
descriptors of the task that created the queue, so a callback cannot
use the files that the task opened: write() fails with EBADF, or
reaches another file that has the same number in the kernel thread
group.

Add work_queue_create_pthread(), the same as work_queue_create() except
that the workers are pthreads of the calling task.  In the flat build
the creation of one worker is passed to the common work_queue_new(), so
work_queue_create() keeps its kernel threads, and the pthread code is
in kwork_pthread.c, which is linked only when it is used.  In user mode
(protected and kernel builds) the workers are already pthreads of the
caller and work_queue_create_pthread() is work_queue_create().

The workers end with the task, so the queue must be freed before the
task exits.

Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Nov 24, 2026
@chengkai15

Copy link
Copy Markdown
Contributor Author

apache/nuttx-apps#4129 depends on this PR.

zblue (merged in apache/nuttx-apps#3977) creates its Zephyr work queues with work_queue_create(). In the flat build the workers are kernel threads, while the HCI receive thread and the application threads are pthreads of the application, so the host is split over two task groups. On current master this makes bt_enable() hang on sim:zblue:

  • The thread-specific key of the zblue porting layer is created by the first work queue, so it belongs to the kernel thread group. Since 0bc013b, pthread_setspecific() returns EINVAL in the application's receive thread. That thread then keeps the cooperative lock while it blocks in read(), and the host TX work queue waits on that lock forever, so HCI_Reset is never sent.
  • File descriptors are not shared either, which is why zblue needed a separate TX work queue that opens /dev/ttyHCI0 a second time.

apache/nuttx-apps#4129 switches zblue to work_queue_create_pthread(), removes that TX work queue, and writes in send() again. With this PR and #4129 on master, sim:zblue with a CSR controller passes discovery, MTU exchange, write, 200 reads on one connection, and 10 reads on new connections. All host threads are pthreads of the application. #4129 stays in draft until this PR is merged.

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

mps2-an500-citest

  • flash: .text +112 B (+0.0%, 247,352 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +64 B (+0.1%, 116,472 B / 4,194,304 B, total: 3% used)

qemu-armv7a-citest

  • ROM: .data +16 B, .init_section -136 B, .text +136 B (+0.0%, 554,088 B / 1,048,576 B, total: 53% used)

qemu-armv8a

  • Code: .text.work_queue_create -152 B, .text.work_start_highpri +8 B, .text.work_thread -436 B, .text.work_thread_create -76 B (+0.1%, 377,744 B)

stm32-nucleo-f103rb

  • flash: .text +72 B (+0.2%, 35,168 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread include/nuttx/wqueue.h

#if !defined(CONFIG_DISABLE_PTHREAD) && \
(defined(CONFIG_BUILD_FLAT) || !defined(__KERNEL__))
FAR struct kwork_wqueue_s *work_queue_create_pthread(FAR const char *name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's provide a new work_queue_init which is called from the caller created thread context and run the work loop in this context directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants