chengkai15 commented on code in PR #22268:
URL: https://github.com/apache/nuttx/pull/22268#discussion_r4694612518


##########
include/nuttx/wqueue.h:
##########
@@ -357,6 +357,34 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const 
char *name,
                                              FAR void *stack_addr,
                                              int stack_size, int nthreads);
 
+/****************************************************************************
+ * Name: work_queue_create_pthread
+ *
+ * Description:
+ *   Create a custom work queue whose workers are pthreads of the calling
+ *   task, so that the work runs with the file descriptors and the other
+ *   resources of that task.  In the flat build, work_queue_create()
+ *   creates kernel threads instead; in user mode both are the same.
+ *   The workers end with the task: free the queue before the task exits.
+ *   This function must only be called from task context.
+ *
+ * Input Parameters:
+ *   The same as work_queue_create().
+ *
+ * Returned Value:
+ *   The work queue handle returned on success.  Otherwise, NULL
+ *
+ ****************************************************************************/
+
+#if !defined(CONFIG_DISABLE_PTHREAD) && \
+    (defined(CONFIG_BUILD_FLAT) || !defined(__KERNEL__))
+FAR struct kwork_wqueue_s *work_queue_create_pthread(FAR const char *name,

Review Comment:
   Thanks, I'll rework it that way. One question on the API shape first.
   
   (a) Two steps:
   
   ```c
   /* 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()` sets `exit` under 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 call `work_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.
   



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