chengkai15 commented on code in PR #22268:
URL: https://github.com/apache/nuttx/pull/22268#discussion_r4735624846
##########
sched/wqueue/kwork_thread.c:
##########
@@ -591,20 +633,197 @@ int work_queue_free(FAR struct kwork_wqueue_s *wqueue)
nxsem_post(&wqueue->sem);
}
- for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ while (nworkers-- > 0)
{
nxsem_wait_uninterruptible(&wqueue->exsem);
}
- for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ return OK;
+}
+
+/****************************************************************************
+ * Name: work_queue_create
+ *
+ * Description:
+ * Create a custom work queue and return its handle. This function creates
+ * a pool containing nthreads workers.
+ *
+ * Input Parameters:
+ * name - Name of the new task
+ * priority - Priority of the new task
+ * stack_addr - Stack buffer of the new task
+ * stack_size - size (in bytes) of the stack needed
+ * nthreads - Number of worker threads to create
+ *
+ * Returned Value:
+ * The work queue handle returned on success. Otherwise, NULL
+ *
+ ****************************************************************************/
+
+FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name,
+ int priority,
+ FAR void *stack_addr,
+ int stack_size, int nthreads)
+{
+ FAR struct kwork_wqueue_s *wqueue;
+ int ret;
+
+ if (name == NULL || stack_size <= 0)
{
- nxsem_destroy(&worker[wndx].wait);
+ return NULL;
}
- nxsem_destroy(&wqueue->sem);
- nxsem_destroy(&wqueue->exsem);
- kmm_free(wqueue);
+ wqueue = work_queue_alloc(nthreads);
+ if (wqueue == NULL)
+ {
+ return NULL;
+ }
+
+ /* Create the work queue thread pool */
+
+ ret = work_thread_create(name, priority, stack_addr, stack_size, wqueue);
+ if (ret < 0)
+ {
+ work_queue_release(wqueue);
+ return NULL;
+ }
+
+ return wqueue;
+}
+
+/****************************************************************************
+ * Name: work_queue_init
+ *
+ * Description:
+ * Create a custom work queue without a worker. A thread that the caller
+ * creates then serves it with work_queue_run().
+ *
+ * Returned Value:
+ * The work queue handle returned on success. Otherwise, NULL
+ *
+ ****************************************************************************/
+FAR struct kwork_wqueue_s *work_queue_init(void)
Review Comment:
Done in 15d2254890: `work_queue_init(int nthreads)` is back. Up to
`nthreads` threads serve the queue, as with `work_queue_create()`.
`work_queue_stop()` waits only for the workers that are in the loop, so a
thread that was never created, or that calls `work_queue_run()` after the stop
(it gets `-ESHUTDOWN`), does not block it.
##########
libs/libc/wqueue/work_usrthread.c:
##########
@@ -275,6 +276,24 @@ static int work_usrtask(int argc, char *argv[])
#endif
#ifndef CONFIG_DISABLE_PTHREAD
+/****************************************************************************
+ * Name: work_thread
+ *
+ * Description:
+ * Entry of the worker threads of work_queue_create().
+ *
+ ****************************************************************************/
+
+static pthread_addr_t work_thread(pthread_addr_t arg)
+{
+ FAR struct usr_worker_s *worker = (FAR struct usr_worker_s *)arg;
+ FAR struct usr_wqueue_s *wqueue = worker->wqueue;
+
+ work_pthread(worker);
+ nxsem_post(&wqueue->exsem);
Review Comment:
Done in 15d2254890. When the queue is stopped, `work_pthread()` (and
`work_loop()` in the kernel) clears the worker slot and posts `exsem`, and the
`work_thread()` wrapper is removed. The predefined queues never stop, so they
are not affected.
--
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]