From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 11E5CA0C41; Wed, 25 Aug 2021 17:12:47 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 900D240041; Wed, 25 Aug 2021 17:12:46 +0200 (CEST) Received: from mail.lysator.liu.se (mail.lysator.liu.se [130.236.254.3]) by mails.dpdk.org (Postfix) with ESMTP id E42DB4003D for ; Wed, 25 Aug 2021 17:12:44 +0200 (CEST) Received: from mail.lysator.liu.se (localhost [127.0.0.1]) by mail.lysator.liu.se (Postfix) with ESMTP id 528D640016 for ; Wed, 25 Aug 2021 17:12:44 +0200 (CEST) Received: by mail.lysator.liu.se (Postfix, from userid 1004) id 4034940015; Wed, 25 Aug 2021 17:12:44 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on bernadotte.lysator.liu.se X-Spam-Level: X-Spam-Status: No, score=-2.2 required=5.0 tests=ALL_TRUSTED, AWL, NICE_REPLY_A autolearn=disabled version=3.4.2 X-Spam-Score: -2.2 Received: from [192.168.1.59] (h-62-63-215-114.A163.priv.bahnhof.se [62.63.215.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.lysator.liu.se (Postfix) with ESMTPSA id 7F93840014; Wed, 25 Aug 2021 17:12:33 +0200 (CEST) To: Honnappa Nagarahalli , dev@dpdk.org, olivier.matz@6wind.com, lucp.at.work@gmail.com, david.marchand@redhat.com, thomas@monjalon.net Cc: ruifeng.wang@arm.com, nd@arm.com References: <20210730213709.19400-1-honnappa.nagarahalli@arm.com> <20210802051652.3611-1-honnappa.nagarahalli@arm.com> From: =?UTF-8?Q?Mattias_R=c3=b6nnblom?= Message-ID: <2caf4e93-6a57-503d-fafb-22ea6a4b1982@lysator.liu.se> Date: Wed, 25 Aug 2021 17:12:32 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210802051652.3611-1-honnappa.nagarahalli@arm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV using ClamSMTP Subject: Re: [dpdk-dev] [RFC v2] eal: simplify the implementation of rte_ctrl_thread_create X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On 2021-08-02 07:16, Honnappa Nagarahalli wrote: > The current described behaviour of rte_ctrl_thread_create is > rigid which makes the implementation of the function complex. > The behavior is abstracted to allow for simplified implementation. > Have you considered using a POSIX condition variable instead of atomics for synchronization? > Signed-off-by: Honnappa Nagarahalli > --- > v2: Used compiler's C++11 atomic built-ins to access the shared variable. > > lib/eal/common/eal_common_thread.c | 65 +++++++++++++----------------- > lib/eal/include/rte_lcore.h | 8 ++-- > 2 files changed, 32 insertions(+), 41 deletions(-) > > diff --git a/lib/eal/common/eal_common_thread.c b/lib/eal/common/eal_common_thread.c > index 1a52f42a2b..e3e0bf4bff 100644 > --- a/lib/eal/common/eal_common_thread.c > +++ b/lib/eal/common/eal_common_thread.c > @@ -169,35 +169,35 @@ __rte_thread_uninit(void) > struct rte_thread_ctrl_params { > void *(*start_routine)(void *); > void *arg; > - pthread_barrier_t configured; > - unsigned int refcnt; > + int ret; > + /* Synchronization variable between the control thread > + * and the thread calling rte_ctrl_thread_create function. > + * 0 - Initialized > + * 1 - Control thread is running successfully > + * 2 - Control thread encountered an error. 'ret' has the > + * error code. > + */ > + unsigned int sync; > }; > > -static void ctrl_params_free(struct rte_thread_ctrl_params *params) > -{ > - if (__atomic_sub_fetch(¶ms->refcnt, 1, __ATOMIC_ACQ_REL) == 0) { > - (void)pthread_barrier_destroy(¶ms->configured); > - free(params); > - } > -} > - > static void *ctrl_thread_init(void *arg) > { > struct internal_config *internal_conf = > eal_get_internal_configuration(); > rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset; > struct rte_thread_ctrl_params *params = arg; > - void *(*start_routine)(void *); > + void *(*start_routine)(void *) = params->start_routine; > void *routine_arg = params->arg; > > __rte_thread_init(rte_lcore_id(), cpuset); > - > - pthread_barrier_wait(¶ms->configured); > - start_routine = params->start_routine; > - ctrl_params_free(params); > - > - if (start_routine == NULL) > + params->ret = pthread_setaffinity_np(pthread_self(), > + sizeof(*cpuset), cpuset); > + if (params->ret != 0) { > + __atomic_store_n(¶ms->sync, 2, __ATOMIC_RELEASE); > return NULL; > + } > + > + __atomic_store_n(¶ms->sync, 1, __ATOMIC_RELEASE); > > return start_routine(routine_arg); > } > @@ -207,9 +207,6 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, > const pthread_attr_t *attr, > void *(*start_routine)(void *), void *arg) > { > - struct internal_config *internal_conf = > - eal_get_internal_configuration(); > - rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset; > struct rte_thread_ctrl_params *params; > int ret; > > @@ -219,15 +216,12 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, > > params->start_routine = start_routine; > params->arg = arg; > - params->refcnt = 2; > - > - ret = pthread_barrier_init(¶ms->configured, NULL, 2); > - if (ret != 0) > - goto fail_no_barrier; > + params->ret = 0; > + params->sync = 0; > > ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params); > if (ret != 0) > - goto fail_with_barrier; > + goto thread_create_failed; > > if (name != NULL) { > ret = rte_thread_setname(*thread, name); > @@ -236,24 +230,21 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, > "Cannot set name for ctrl thread\n"); > } > > - ret = pthread_setaffinity_np(*thread, sizeof(*cpuset), cpuset); > - if (ret != 0) > - params->start_routine = NULL; > + /* Wait for the control thread to initialize successfully */ > + while (!__atomic_load_n(¶ms->sync, __ATOMIC_ACQUIRE)) > + rte_pause(); > + ret = params->ret; > > - pthread_barrier_wait(¶ms->configured); > - ctrl_params_free(params); > + free(params); > > - if (ret != 0) > - /* start_routine has been set to NULL above; */ > - /* ctrl thread will exit immediately */ > + if (__atomic_load_n(¶ms->sync, __ATOMIC_ACQUIRE) != 1) > + /* ctrl thread is exiting */ > pthread_join(*thread, NULL); > > return -ret; > > -fail_with_barrier: > - (void)pthread_barrier_destroy(¶ms->configured); > +thread_create_failed: > > -fail_no_barrier: > free(params); > > return -ret; > diff --git a/lib/eal/include/rte_lcore.h b/lib/eal/include/rte_lcore.h > index 1550b75da0..f1cc5e38dc 100644 > --- a/lib/eal/include/rte_lcore.h > +++ b/lib/eal/include/rte_lcore.h > @@ -420,10 +420,10 @@ rte_thread_unregister(void); > /** > * Create a control thread. > * > - * Wrapper to pthread_create(), pthread_setname_np() and > - * pthread_setaffinity_np(). The affinity of the new thread is based > - * on the CPU affinity retrieved at the time rte_eal_init() was called, > - * the dataplane and service lcores are then excluded. > + * Creates a control thread with the given name and attributes. The > + * affinity of the new thread is based on the CPU affinity retrieved > + * at the time rte_eal_init() was called, the dataplane and service > + * lcores are then excluded. > * > * @param thread > * Filled with the thread id of the new created thread. >