Re: [RFC PATCH v3 07/15] perf workqueue: implement worker thread and management

From: Jiri Olsa
Date: Mon Aug 30 2021 - 03:22:57 EST


On Fri, Aug 20, 2021 at 12:53:53PM +0200, Riccardo Mancini wrote:

SNIP

> /**
> * create_workqueue - create a workqueue associated to @pool
> *
> @@ -41,7 +361,8 @@ static const char * const workqueue_errno_str[] = {
> */
> struct workqueue_struct *create_workqueue(int nr_threads)
> {
> - int ret, err = 0;
> + int ret, err = 0, t;
> + struct worker *worker;
> struct workqueue_struct *wq = zalloc(sizeof(struct workqueue_struct));
>
> if (!wq) {
> @@ -56,10 +377,16 @@ struct workqueue_struct *create_workqueue(int nr_threads)
> goto out_free_wq;
> }
>
> + wq->workers = calloc(nr_threads, sizeof(*wq->workers));
> + if (!wq->workers) {
> + err = -ENOMEM;
> + goto out_delete_pool;
> + }
> +
> ret = pthread_mutex_init(&wq->lock, NULL);
> if (ret) {
> err = -ret;
> - goto out_delete_pool;
> + goto out_free_workers;
> }
>
> ret = pthread_cond_init(&wq->idle_cond, NULL);
> @@ -77,12 +404,41 @@ struct workqueue_struct *create_workqueue(int nr_threads)
> goto out_destroy_cond;
> }
>
> + wq->task.fn = worker_thread;
> +
> + wq->pool_errno = threadpool__execute(wq->pool, &wq->task);
> + if (wq->pool_errno) {
> + err = -WORKQUEUE_ERROR__POOLEXE;
> + goto out_close_pipe;
> + }

hum, why the threadpool__execute in here? threads are not runnig at this
point, so nothing will happen right?

jirka

> +
> + for (t = 0; t < nr_threads; t++) {
> + err = spinup_worker(wq, t);
> + if (err)
> + goto out_stop_pool;
> + }
> +
> return wq;
>
> +out_stop_pool:
> + lock_workqueue(wq);
> + for_each_idle_worker(wq, worker) {
> + ret = stop_worker(worker);
> + if (ret)
> + err = ret;
> + }
> + unlock_workqueue(wq);
> +out_close_pipe:
> + close(wq->msg_pipe[0]);
> + wq->msg_pipe[0] = -1;
> + close(wq->msg_pipe[1]);
> + wq->msg_pipe[1] = -1;
> out_destroy_cond:
> pthread_cond_destroy(&wq->idle_cond);
> out_destroy_mutex:
> pthread_mutex_destroy(&wq->lock);
> +out_free_workers:
> + free(wq->workers);
> out_delete_pool:
> threadpool__delete(wq->pool);
> out_free_wq:
> @@ -96,12 +452,27 @@ struct workqueue_struct *create_workqueue(int nr_threads)
> */

SNIP