chengkai15 opened a new pull request, #22268: URL: https://github.com/apache/nuttx/pull/22268
*Note: Please adhere to [Contributing Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).* ## 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 (b5db91fe0d), 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
