From patchwork Mon Aug 2 05:16:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Honnappa Nagarahalli X-Patchwork-Id: 490309 Delivered-To: patch@linaro.org Received: by 2002:a05:6638:1185:0:0:0:0 with SMTP id f5csp1602227jas; Sun, 1 Aug 2021 22:17:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0EuJYRhFep3jIYOXSpoo/oq/5SCz7z4b+lSd9b4P5wfD4NBdK1cNm7XGIMY/hTh99HpiO X-Received: by 2002:a17:906:11ca:: with SMTP id o10mr13489251eja.21.1627881436933; Sun, 01 Aug 2021 22:17:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627881436; cv=none; d=google.com; s=arc-20160816; b=e08DX0jQF6knfkhERiakxcjFm4AoRRNor45kh3CXUt2AmW96fq3yeiPBDE7AeQczsA H5m3MryQxJZTsBKIxhhy60DJjew+feRlnkQ6I46C/A1iIfHFin1xm8U0Ui/Iapi804sj wW+f2zEd6ipZzcGeKChtHimKJr51ueOyKmuKpjj38zRa9srX1pqFTceAQ+hRqFDkQaW2 XZ4SIkATIMD4BvPViTfPLVZ5bnoNwj3xwS4/U13r+AxpjVumbGAojubrDRNe4k4Ifn9t u+N76K5rIRwEmD4YoP8K+/HE5Jq6pWhjM0b8XdiwHcLNtOAdMSM7s5KDaPIXTrPne4GC re/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:cc:to:from; bh=+BU7oubPnRcbEPe/s7Fw0dSTGQA8aWyzaUtDmDfRDAs=; b=aY1bKALQyChUKgRbZvLUiMweUTtaqC+4cYkk+ex1MOpQIbjQDupJ5hALBBm02DHv0+ 2+DS90oLbBzwRmw0bbmwAFQVBzr9ifEOpMN8VBhQ2qpdkJOglmpFpIJO1DGog9ZTx5x8 cYLKuOYaxoVwNdzYUo0N6XFtIjOZ0ZmyUpbARrD585XZHpfhXO+fNzAmOx1rjS1/Kbv6 Pg0CdXNFyjRErZ+JUai2yaEYq3UyD2wwGnkjmPdgPuLhlV17N7iCmYiKJqpyQC35xMLu c3f11FML5qX5WxRNwElrNEC4k0Y1RYz7YSMP28u079Kk1OUigt869mR9vJfIy1zBsyad mtXg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 217.70.189.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from mails.dpdk.org (mails.dpdk.org. [217.70.189.124]) by mx.google.com with ESMTP id u23si8671850edy.77.2021.08.01.22.17.16; Sun, 01 Aug 2021 22:17:16 -0700 (PDT) Received-SPF: pass (google.com: domain of dev-bounces@dpdk.org designates 217.70.189.124 as permitted sender) client-ip=217.70.189.124; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 217.70.189.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9DB5440143; Mon, 2 Aug 2021 07:17:15 +0200 (CEST) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mails.dpdk.org (Postfix) with ESMTP id 2E6DD40140 for ; Mon, 2 Aug 2021 07:17:14 +0200 (CEST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 605C1106F; Sun, 1 Aug 2021 22:17:13 -0700 (PDT) Received: from qc2400f-1.austin.arm.com (qc2400f-1.austin.arm.com [10.118.12.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 468413F719; Sun, 1 Aug 2021 22:17:13 -0700 (PDT) From: Honnappa Nagarahalli To: dev@dpdk.org, honnappa.nagarahalli@arm.com, olivier.matz@6wind.com, lucp.at.work@gmail.com, david.marchand@redhat.com, thomas@monjalon.net Cc: ruifeng.wang@arm.com, nd@arm.com Date: Mon, 2 Aug 2021 00:16:52 -0500 Message-Id: <20210802051652.3611-1-honnappa.nagarahalli@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210730213709.19400-1-honnappa.nagarahalli@arm.com> References: <20210730213709.19400-1-honnappa.nagarahalli@arm.com> Subject: [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" 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. 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(-) -- 2.17.1 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.