Re: [RFC PATCH v2 06/10] perf workqueue: introduce workqueue struct
From: Jiri Olsa
Date: Mon Aug 09 2021 - 08:05:06 EST
On Fri, Jul 30, 2021 at 05:34:13PM +0200, Riccardo Mancini wrote:
SNIP
> +static void worker_thread(int tidx, struct task_struct *task)
> +{
> +
> + pr_info("Hi from worker %d, executing task %p\n", tidx, task);
> +}
> +
> +/**
> + * attach_threadpool_to_workqueue - start @wq workers on @pool
> + */
> +static int attach_threadpool_to_workqueue(struct workqueue_struct *wq,
> + struct threadpool *pool)
> +{
> + if (!threadpool__is_ready(pool)) {
> + pr_debug2("workqueue: cannot attach to pool: pool is not ready\n");
> + return -WORKQUEUE_ERROR__NOTALLOWED;
> + }
> +
> + wq->pool = pool;
> +
> + wq->pool_errno = threadpool__execute(pool, &wq->task);
> + if (wq->pool_errno)
> + return -WORKQUEUE_ERROR__POOLEXE;
SNIP
> +
> +/**
> + * create_workqueue - create a workqueue associated to @pool
> + *
> + * Only one workqueue can execute on a pool at a time.
> + */
> +struct workqueue_struct *create_workqueue(struct threadpool *pool)
> +{
> + int ret, err = 0;
> + struct workqueue_struct *wq = malloc(sizeof(struct workqueue_struct));
> +
> + if (!wq) {
> + err = -ENOMEM;
> + goto out_return;
> + }
> +
> + ret = pthread_mutex_init(&wq->lock, NULL);
> + if (ret) {
> + err = -ret;
> + goto out_free_wq;
> + }
> +
> + ret = pthread_cond_init(&wq->idle_cond, NULL);
> + if (ret) {
> + err = -ret;
> + goto out_destroy_mutex;
> + }
> +
> + wq->pool = NULL;
> + INIT_LIST_HEAD(&wq->busy_list);
> + INIT_LIST_HEAD(&wq->idle_list);
> +
> + INIT_LIST_HEAD(&wq->pending);
> +
> + ret = pipe(wq->msg_pipe);
> + if (ret) {
> + err = -ENOMEM;
> + goto out_destroy_cond;
> + }
> +
> + wq->task.fn = worker_thread;
> +
> + ret = attach_threadpool_to_workqueue(wq, pool);
> + if (ret) {
> + err = ret;
> + goto out_destroy_cond;
> + }
> +
> + wq->status = WORKQUEUE_STATUS__READY;
> +
> + return wq;
> +
> +out_destroy_cond:
> + pthread_cond_destroy(&wq->idle_cond);
leaking wq->msg_pipe?
thanks,
jirka
> +out_destroy_mutex:
> + pthread_mutex_destroy(&wq->lock);
> +out_free_wq:
> + free(wq);
> +out_return:
> + return ERR_PTR(err);
> +}
> +
SNIP