chengkai15 commented on PR #22268:
URL: https://github.com/apache/nuttx/pull/22268#issuecomment-7211367101

   @cederom thanks for the careful review. Updated in the new push (rebased on 
the current master):
   
   1. **`work_queue_free()` vs. a late `work_queue_run()`**: you were right, 
and the `-ESHUTDOWN` path itself was not safe either: a late thread still 
touched the queue lock after `work_queue_free()` had stopped waiting for 
workers, and could run into `kmm_free()` / `free()`. Now exactly `nthreads` 
threads must call `work_queue_run()`, `work_queue_free()` waits for all 
`nthreads`, and a late caller posts `exsem` as its last access to the queue 
before it returns `-ESHUTDOWN`. Same change in the kernel and in libc. 
`nworkers` is gone, so the kernel free path is back to the original wait loop.
   2. **A worker that leaves the loop**: documented in `work_queue_run()`. If 
the task exits, or a callback calls `pthread_exit()` or is cancelled, 
`work_queue_free()` blocks forever.
   3. **`work_queue_priority_wq()` before any worker**: it returns `-ESRCH` in 
both the kernel and libc, and that is now documented. Slots are taken from 
index 0 and never released, so `worker[0]` is valid as soon as one thread runs. 
`work_queue_setpriority_wq()` only changes the threads already in 
`work_queue_run()`; threads that join later keep their own priority. This is 
documented too.
   4. **Return values**: the kernel now has a `run` flag like libc, so 
`work_queue_run()` on a `work_queue_create()` queue returns `-EINVAL` in both 
builds. There are no new source files, so CMake needed no change.
   
   Tested:
   - sim:ostest (make): the new cases in apache/nuttx-apps#4140 pass.
   - sim:smp (CMake, 4 CPUs): ostest exits with status 0.
   - stm32f4discovery:kostest (CMake, protected) with `CONFIG_LIBC_USRWORK=y`: 
builds, so the user and the kernel copies of `work_usrthread.c` are compiled.
   
   @xiaoxiang781216 point 1 changes the API contract: exactly `nthreads` 
threads must call `work_queue_run()`. Could you take another look and confirm 
that this works for you, and whether it resolves your earlier change request?
   


-- 
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]

Reply via email to