xiaoxiang781216 commented on code in PR #22268:
URL: https://github.com/apache/nuttx/pull/22268#discussion_r4730272364
##########
sched/wqueue/kwork_thread.c:
##########
@@ -516,6 +565,108 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const
char *name,
return wqueue;
}
+/****************************************************************************
+ * Name: work_queue_init
+ *
+ * Description:
+ * Create a custom work queue without workers. Up to nthreads threads
+ * that the caller creates then serve it with work_queue_run().
+ *
+ * Input Parameters:
+ * nthreads - Number of threads that may serve the queue
+ *
+ * Returned Value:
+ * The work queue handle returned on success. Otherwise, NULL
+ *
+ ****************************************************************************/
+
+FAR struct kwork_wqueue_s *work_queue_init(int nthreads)
+{
+ FAR struct kwork_wqueue_s *wqueue;
+ FAR struct kworker_s *worker;
+ int wndx;
+
+ wqueue = work_queue_alloc(nthreads);
+ if (wqueue == NULL)
+ {
+ return NULL;
+ }
+
+ worker = wq_get_worker(wqueue);
+ for (wndx = 0; wndx < nthreads; wndx++)
+ {
+ nxsem_init(&worker[wndx].wait, 0, 0);
Review Comment:
why not merge into work_queue_alloc
##########
sched/wqueue/wqueue.h:
##########
@@ -81,6 +81,7 @@ struct kwork_wqueue_s
int nthreads; /* Number of worker threads */
bool exit; /* A flag to request the thread to exit */
bool dynamic; /* Dynamically allocated queue */
+ bool run; /* Served by work_queue_run() */
Review Comment:
remove, the wrong usage will be cached by
https://github.com/apache/nuttx/pull/22268/changes#diff-e692bbd69daa1470b1d56a37ed008bd703cf7e0ed3920a0b6d6688673c3c75d2R660
##########
include/nuttx/wqueue.h:
##########
@@ -357,6 +357,61 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const
char *name,
FAR void *stack_addr,
int stack_size, int nthreads);
+/****************************************************************************
+ * Name: work_queue_init
+ *
+ * Description:
+ * Create a custom work queue without worker threads and return its
+ * handle. Work can be queued at once; it runs when the threads that the
+ * caller creates call work_queue_run(). The caller chooses the kind of
+ * thread, its priority and its stack, so in the flat build the work can
+ * run in a pthread of the calling task.
+ * User-mode custom queues require pthread support.
+ *
+ * Exactly nthreads threads must call work_queue_run(), because
Review Comment:
let's support the error case that work_queue_fee is called because not all
thread can be created
##########
libs/libc/wqueue/work_usrthread.c:
##########
@@ -435,23 +428,175 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const
char *name,
nxsem_init(&wqueue->worker[wndx].wait, 0, 0);
}
+ return wqueue;
+}
+
+/****************************************************************************
+ * Name: work_queue_release
+ *
+ * Description:
+ * Release a queue whose workers have all returned.
+ *
+ ****************************************************************************/
+
+static void work_queue_release(FAR struct usr_wqueue_s *wqueue)
+{
+ int wndx;
+
+ for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ {
+ nxsem_destroy(&wqueue->worker[wndx].wait);
+ }
+
+ if (wqueue->run)
+ {
+ nxsem_destroy(&wqueue->exsem);
+ }
+
+ nxsem_destroy(&wqueue->wake);
+ nxmutex_destroy(&wqueue->lock);
+ free(wqueue);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: work_queue_create
+ *
+ * Description:
+ * Create a user-mode custom work queue. This function must only be
+ * called from task context.
+ *
+ ****************************************************************************/
+
+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 usr_wqueue_s *wqueue;
+ int ret;
+
+ if (name == NULL || stack_size <= 0)
+ {
+ return NULL;
+ }
+
+ wqueue = work_queue_alloc(nthreads);
+ if (wqueue == NULL)
+ {
+ return NULL;
+ }
+
ret = work_thread_create(name, priority, stack_addr, stack_size, wqueue);
if (ret < 0)
{
- for (wndx = 0; wndx < nthreads; wndx++)
- {
- nxsem_destroy(&wqueue->worker[wndx].wait);
- }
+ work_queue_release(wqueue);
+ return NULL;
+ }
+
+ return (FAR struct kwork_wqueue_s *)wqueue;
+}
- nxsem_destroy(&wqueue->wake);
- nxmutex_destroy(&wqueue->lock);
- free(wqueue);
+/****************************************************************************
+ * Name: work_queue_init
+ *
+ * Description:
+ * Create a user-mode custom work queue that threads of the caller serve
+ * with work_queue_run().
+ *
+ ****************************************************************************/
+
+FAR struct kwork_wqueue_s *work_queue_init(int nthreads)
+{
+ FAR struct usr_wqueue_s *wqueue;
+ int wndx;
+
+ wqueue = work_queue_alloc(nthreads);
+ if (wqueue == NULL)
+ {
return NULL;
}
+ for (wndx = 0; wndx < nthreads; wndx++)
+ {
+ wqueue->worker[wndx].tid = INVALID_PROCESS_ID;
+ }
+
+ nxsem_init(&wqueue->exsem, 0, 0);
Review Comment:
merge into work_queue_alloc
##########
libs/libc/wqueue/work_usrthread.c:
##########
@@ -505,15 +650,27 @@ int work_queue_free(FAR struct kwork_wqueue_s *handle)
nxsem_post(&wqueue->wake);
}
- for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ if (wqueue->run)
{
- pthread_join(wqueue->worker[wndx].tid, NULL);
- nxsem_destroy(&wqueue->worker[wndx].wait);
+ /* The threads belong to the caller and cannot be joined here. Each
+ * of the nthreads work_queue_run() threads posts exsem, also one
+ * that arrives after the exit request.
+ */
+
+ for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ {
+ nxsem_wait_uninterruptible(&wqueue->exsem);
+ }
+ }
+ else
+ {
+ for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ {
+ pthread_join(wqueue->worker[wndx].tid, NULL);
Review Comment:
let's initialize pthread in the unjoinable state
##########
libs/libc/wqueue/work_usrthread.c:
##########
@@ -435,23 +428,175 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const
char *name,
nxsem_init(&wqueue->worker[wndx].wait, 0, 0);
}
+ return wqueue;
+}
+
+/****************************************************************************
+ * Name: work_queue_release
+ *
+ * Description:
+ * Release a queue whose workers have all returned.
+ *
+ ****************************************************************************/
+
+static void work_queue_release(FAR struct usr_wqueue_s *wqueue)
+{
+ int wndx;
+
+ for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ {
+ nxsem_destroy(&wqueue->worker[wndx].wait);
+ }
+
+ if (wqueue->run)
+ {
+ nxsem_destroy(&wqueue->exsem);
+ }
+
+ nxsem_destroy(&wqueue->wake);
+ nxmutex_destroy(&wqueue->lock);
+ free(wqueue);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: work_queue_create
+ *
+ * Description:
+ * Create a user-mode custom work queue. This function must only be
+ * called from task context.
+ *
+ ****************************************************************************/
+
+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 usr_wqueue_s *wqueue;
+ int ret;
+
+ if (name == NULL || stack_size <= 0)
+ {
+ return NULL;
+ }
+
+ wqueue = work_queue_alloc(nthreads);
+ if (wqueue == NULL)
+ {
+ return NULL;
+ }
+
ret = work_thread_create(name, priority, stack_addr, stack_size, wqueue);
if (ret < 0)
{
- for (wndx = 0; wndx < nthreads; wndx++)
- {
- nxsem_destroy(&wqueue->worker[wndx].wait);
- }
+ work_queue_release(wqueue);
+ return NULL;
+ }
+
+ return (FAR struct kwork_wqueue_s *)wqueue;
+}
- nxsem_destroy(&wqueue->wake);
- nxmutex_destroy(&wqueue->lock);
- free(wqueue);
+/****************************************************************************
+ * Name: work_queue_init
+ *
+ * Description:
+ * Create a user-mode custom work queue that threads of the caller serve
+ * with work_queue_run().
+ *
+ ****************************************************************************/
+
+FAR struct kwork_wqueue_s *work_queue_init(int nthreads)
+{
+ FAR struct usr_wqueue_s *wqueue;
+ int wndx;
+
+ wqueue = work_queue_alloc(nthreads);
+ if (wqueue == NULL)
+ {
return NULL;
}
+ for (wndx = 0; wndx < nthreads; wndx++)
+ {
+ wqueue->worker[wndx].tid = INVALID_PROCESS_ID;
Review Comment:
move to work_queue_alloc
##########
libs/libc/wqueue/work_usrthread.c:
##########
@@ -505,15 +650,27 @@ int work_queue_free(FAR struct kwork_wqueue_s *handle)
nxsem_post(&wqueue->wake);
}
- for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ if (wqueue->run)
{
- pthread_join(wqueue->worker[wndx].tid, NULL);
- nxsem_destroy(&wqueue->worker[wndx].wait);
+ /* The threads belong to the caller and cannot be joined here. Each
+ * of the nthreads work_queue_run() threads posts exsem, also one
+ * that arrives after the exit request.
+ */
+
+ for (wndx = 0; wndx < wqueue->nthreads; wndx++)
+ {
+ nxsem_wait_uninterruptible(&wqueue->exsem);
Review Comment:
let's wai exsem for the internal created pthread
--
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]