Repository navigation
sched/wqueue: Add work_queue_init() and work_queue_run() - #22268
chengkai15 wants to merge 1 commit into
Conversation
|
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
There was a problem hiding this comment.
Thanks, I'll rework it that way. One question on the API shape first.
(a) Two steps:
/* Any thread; allocates the queue, creates no worker */
FAR struct kwork_wqueue_s *work_queue_init(int nthreads);
/* From a thread the caller created: becomes one of the workers and runs
* the loop; returns after work_queue_free()
*/
int work_queue_run(FAR struct kwork_wqueue_s *wqueue);The handle exists before the thread runs, so the caller can queue work right after it creates the thread, and up to nthreads threads can serve one queue.
(b) work_queue_init() is called inside the new thread, which then runs the loop. The creator has to wait for the thread to hand back the handle, and only that thread can serve the queue.
I plan to do (a). work_queue_create() then becomes work_queue_init() plus kernel threads that run the same loop, and work_queue_create_pthread() is dropped. Please tell me if you prefer (b).
work_queue_free() needs a change with (a). Today it posts the semaphore nthreads times and waits for nthreads exits. With (a), some threads may not have reached work_queue_run() yet, or never will (for example when pthread_create() failed), so work_queue_free() would hang. I'll count the workers under the queue lock instead:
work_queue_run()registers the calling thread (its tid goes into a free worker slot) and increments the count. It returns -EBUSY when all nthreads slots are taken, and -ESHUTDOWN when the queue is already being freed.work_queue_free()setsexitunder the same lock, then waits only for the registered workers.- Since the queue memory is released when
work_queue_free()returns, a thread must not callwork_queue_run()after that. I'll document that the caller frees the queue only after its threads have started serving it, or have been joined.
As before, a worker that is killed when the task exits never reports back, so the queue must be freed before the task exits.
xiaoxiang781216
left a comment
There was a problem hiding this comment.
please don't merge this patch before fixing the problem.
6a105de to
4c73208
Compare
|
Thanks, the two-step 1.
The doc says the threads "must call work_queue_run() before the queue is freed", but no API lets the caller check that. Options:
The same applies to the libc implementation. 2. Workers that die inside the loop If a 3. Both implementations read 4. Minor
Apart from that, the refactor ( |
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 or the other resources 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_init(), which creates a custom queue with nthreads worker slots and no thread, and work_queue_run(), which makes the calling thread a worker and runs the work loop until the queue is freed. The caller creates the threads, so it chooses their kind, name, priority and stack; in the flat build they can be pthreads of the task. Work can be queued before the threads run. Exactly nthreads threads must call work_queue_run(), and work_queue_free() waits for all of them. A thread may reach work_queue_run() after work_queue_free() was called: it then posts the exit semaphore as its last access to the queue and returns -ESHUTDOWN, so the queue is not released while a thread still uses it. work_queue_run() returns -EBUSY when all the slots are taken, and -EINVAL for a queue that work_queue_init() did not create. A worker must stay in work_queue_run() until it returns; if it ends with its task, work_queue_free() blocks. work_queue_create() is unchanged; it shares the allocation and the loop with the new interfaces. The user-mode implementation in libc gets the same interfaces. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
4c73208 to
24bf49e
Compare
|
@cederom thanks for the careful review. Updated in the new push (rebased on the current master):
Tested:
@xiaoxiang781216 point 1 changes the API contract: exactly |
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 or the other resources 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 on current masterbt_enable()hangs on sim:zblue (see apache/nuttx-apps#4129).As suggested in the review, the caller now creates the threads and the work queue only provides the loop:
work_queue_init(nthreads)creates a custom queue withnthreadsworker slots and no thread. Work can be queued at once.work_queue_run(wqueue), called from a thread that the caller created, makes that thread a worker and runs the work loop until the queue is freed.The caller chooses the kind of thread, its name, priority and stack; in the flat build the workers can be pthreads of the task. The same interfaces are in the libc user-mode implementation (protected and kernel builds).
A thread may not have reached
work_queue_run()when the queue is freed, sowork_queue_free()now waits only for the workers that run the loop, counted under the queue lock.work_queue_run()returns-EBUSYwhen all the slots are taken and-ESHUTDOWNwhen the queue is being freed. The threads must callwork_queue_run()before the queue is freed, and if they end with their task, the queue must be freed before the task exits.work_queue_setpriority_wq()skips the slots that no thread took.Impact
work_queue_create()and HPWORK/LPWORK are unchanged;work_queue_create()shares the allocation and the loop with the new interfaces.text: 359903 -> 360023 (+120 bytes).include/nuttx/wqueue.handDocumentation/os/scheduling/wqueue.rst.testing/ostest/wqueue.c).Testing
The new ostest case creates a queue with
work_queue_init(2), opens/dev/nullin the test task, queues a callback, and only then starts two pthreads of the task that callwork_queue_run(). The callback checks thatgetpid()is the pid of the task and thatwrite()on the fd succeeds. Then the sync cancel, parallel cancel (both threads serve the queue at the same time) and priority tests run on the queue, a thirdwork_queue_run()must return-EBUSY, and both threads must return 0 fromwork_queue_run()afterwork_queue_free(). Last,work_queue_free()of a queue with a slot that no thread took must return.master (b5db91f), sim:ostest, the same check with
work_queue_create()(the problem):This PR, sim:ostest (flat), full ostest (4 loops, no errors):
mps2-an500:citest_protected (protected) +
CONFIG_LIBC_USRWORK, qemu-system-arm,ostest wqueue:qemu-armv7a:citest_knsh (kernel) +
CONFIG_LIBC_USRWORK, qemu-system-arm,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:-ENOENT(api_validation_test()). Master fails at the same assertion.usrwork_test()reports-110). Master fails the same way.With apache/nuttx-apps#4129, 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.
Builds: sim:ostest and sim:zblue (Make), mps2-an500:citest_protected, qemu-armv7a:citest_knsh, mps3-an547:nsh. Not tested: CMake, SMP, real hardware.