Repository navigation
sched/wqueue: Add work_queue_create_pthread() - #22268
chengkai15 wants to merge 1 commit into
Conversation
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>
|
apache/nuttx-apps#4129 depends on this PR. zblue (merged in apache/nuttx-apps#3977) creates its Zephyr work queues with
apache/nuttx-apps#4129 switches zblue to |
|
|
|
||
| #if !defined(CONFIG_DISABLE_PTHREAD) && \ | ||
| (defined(CONFIG_BUILD_FLAT) || !defined(__KERNEL__)) | ||
| FAR struct kwork_wqueue_s *work_queue_create_pthread(FAR const char *name, |
There was a problem hiding this comment.
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
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 withEBADF, 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/ttyHCI0from 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 aswork_queue_create()except that the workers are pthreads of the calling task:work_queue_new().work_queue_create()and HPWORK/LPWORK keep their kernel threads. The pthread worker is inkwork_pthread.c, which is linked only when it is used.work_queue_create_pthread()iswork_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
work_queue_create()behaves as before.text: 360223 -> 360351 (+128 bytes, fromwork_queue_new()and the creation callback).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/nullin the test task, and checks in a callback thatgetpid()is the pid of the task and thatwrite()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):This PR, sim:ostest (flat), full ostest, 1 loop:
sim:smp (4 CPUs, with
CONFIG_TESTING_OSTEST_WQUEUE),ostest wqueue: passed 5 times out of 5.mps3-an547:knsh (protected) +
CONFIG_LIBC_USRWORK, qemu-system-arm,ostest wqueue:qemu-armv7a:knsh (kernel) +
CONFIG_LIBC_USRWORK, qemu-system-arm,/system/bin/ostest wqueue:On protected and kernel,
ostest wqueuefails on master before it reaches this case, in code that this PR does not change. For these two runs those checks were disabled locally: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.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.