From patchwork Sat Apr 3 01:38:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Narcisa Ana Maria Vasile X-Patchwork-Id: 90507 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B01ABA0548; Sat, 3 Apr 2021 03:39:15 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3B27B140EA1; Sat, 3 Apr 2021 03:39:15 +0200 (CEST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 255C3140EA0 for ; Sat, 3 Apr 2021 03:39:13 +0200 (CEST) Received: by linux.microsoft.com (Postfix, from userid 1059) id 62B6E20B5680; Fri, 2 Apr 2021 18:39:12 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 62B6E20B5680 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1617413952; bh=M22C88/3qhoEOxJ8dZeKYu5kvCtjVsb7jq1XjzyNpqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ROFA5U3zsO28tEoZ1/4HjlVlm6z9Z7M8ik8h1z89ZAyQcGBNlMMM2G57IGPtDn/nv wx/difDI2E+G5BZBpnxP8rVq/XtYdPEZUWcRmPAtTsCB2D0Llu1IiHAKeYIn5tcAT2 qNX1LyVCAU2k/AuEFoAEx1MCJ0elLu01q1HeNrm8= From: Narcisa Ana Maria Vasile To: dev@dpdk.org, thomas@monjalon.net, dmitry.kozliuk@gmail.com, khot@microsoft.com, navasile@microsoft.com, dmitrym@microsoft.com, roretzla@microsoft.com, talshn@nvidia.com, ocardona@microsoft.com Cc: bruce.richardson@intel.com, david.marchand@redhat.com, pallavi.kadam@intel.com Date: Fri, 2 Apr 2021 18:38:59 -0700 Message-Id: <1617413948-10504-2-git-send-email-navasile@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1617413948-10504-1-git-send-email-navasile@linux.microsoft.com> References: <1617057640-24301-2-git-send-email-navasile@linux.microsoft.com> <1617413948-10504-1-git-send-email-navasile@linux.microsoft.com> Subject: [dpdk-dev] [PATCH v6 01/10] eal: add thread id and simple thread functions 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" From: Narcisa Vasile Add the thread identifier type. Add functions for comparing thread ids and obtaining the thread id for the current thread. Signed-off-by: Narcisa Vasile --- lib/librte_eal/common/rte_thread.c | 101 ++++++++++++++++++ lib/librte_eal/include/rte_thread.h | 45 ++++++-- lib/librte_eal/include/rte_thread_types.h | 12 +++ .../include/rte_windows_thread_types.h | 12 +++ lib/librte_eal/windows/rte_thread.c | 13 +++ 5 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 lib/librte_eal/common/rte_thread.c create mode 100644 lib/librte_eal/include/rte_thread_types.h create mode 100644 lib/librte_eal/windows/include/rte_windows_thread_types.h diff --git a/lib/librte_eal/common/rte_thread.c b/lib/librte_eal/common/rte_thread.c new file mode 100644 index 000000000..5ec382949 --- /dev/null +++ b/lib/librte_eal/common/rte_thread.c @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2021 Mellanox Technologies, Ltd + * Copyright(c) 2021 Microsoft Corporation + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +struct eal_tls_key { + pthread_key_t thread_index; +}; + +rte_thread_t +rte_thread_self(void) +{ + return pthread_self(); +} + +int +rte_thread_equal(rte_thread_t t1, rte_thread_t t2) +{ + return pthread_equal(t1, t2); +} + +int +rte_thread_key_create(rte_thread_key *key, void (*destructor)(void *)) +{ + int err; + rte_thread_key k; + + k = malloc(sizeof(*k)); + if (k == NULL) { + RTE_LOG(DEBUG, EAL, "Cannot allocate TLS key.\n"); + return EINVAL; + } + err = pthread_key_create(&(k->thread_index), destructor); + if (err != 0) { + RTE_LOG(DEBUG, EAL, "pthread_key_create failed: %s\n", + strerror(err)); + free(k); + return err; + } + *key = k; + return 0; +} + +int +rte_thread_key_delete(rte_thread_key key) +{ + int err; + + if (key == NULL) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + return EINVAL; + } + err = pthread_key_delete(key->thread_index); + if (err != 0) { + RTE_LOG(DEBUG, EAL, "pthread_key_delete failed: %s\n", + strerror(err)); + free(key); + return err; + } + free(key); + return 0; +} + +int +rte_thread_value_set(rte_thread_key key, const void *value) +{ + int err; + + if (key == NULL) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + return EINVAL; + } + err = pthread_setspecific(key->thread_index, value); + if (err != 0) { + RTE_LOG(DEBUG, EAL, "pthread_setspecific failed: %s\n", + strerror(err)); + return err; + } + return 0; +} + +void * +rte_thread_value_get(rte_thread_key key) +{ + if (key == NULL) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + rte_errno = EINVAL; + return NULL; + } + return pthread_getspecific(key->thread_index); +} diff --git a/lib/librte_eal/include/rte_thread.h b/lib/librte_eal/include/rte_thread.h index 8be8ed8f3..cbc07f739 100644 --- a/lib/librte_eal/include/rte_thread.h +++ b/lib/librte_eal/include/rte_thread.h @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2021 Mellanox Technologies, Ltd + * Copyright(c) 2021 Microsoft Corporation */ #include @@ -20,6 +21,13 @@ extern "C" { #endif +#include +#if defined(RTE_USE_WINDOWS_THREAD_TYPES) +#include +#else +#include +#endif + /** * TLS key type, an opaque pointer. */ @@ -27,6 +35,31 @@ typedef struct eal_tls_key *rte_thread_key; #ifdef RTE_HAS_CPUSET +/** + * Get the id of the calling thread. + * + * @return + * Return the thread id of the calling thread. + */ +__rte_experimental +rte_thread_t rte_thread_self(void); + +/** + * Check if 2 thread ids are equal. + * + * @param t1 + * First thread id. + * + * @param t2 + * Second thread id. + * + * @return + * If the ids are equal, return nonzero. + * Otherwise, return 0. + */ +__rte_experimental +int rte_thread_equal(rte_thread_t t1, rte_thread_t t2); + /** * Set core affinity of the current thread. * Support both EAL and non-EAL thread and update TLS. @@ -63,9 +96,7 @@ void rte_thread_get_affinity(rte_cpuset_t *cpusetp); * * @return * On success, zero. - * On failure, a negative number and an error number is set in rte_errno. - * rte_errno can be: ENOMEM - Memory allocation error. - * ENOEXEC - Specific OS error. + * On failure, return a positive errno-style error number. */ __rte_experimental @@ -80,9 +111,7 @@ int rte_thread_key_create(rte_thread_key *key, * * @return * On success, zero. - * On failure, a negative number and an error number is set in rte_errno. - * rte_errno can be: EINVAL - Invalid parameter passed. - * ENOEXEC - Specific OS error. + * On failure, return a positive errno-style error number. */ __rte_experimental int rte_thread_key_delete(rte_thread_key key); @@ -97,9 +126,7 @@ int rte_thread_key_delete(rte_thread_key key); * * @return * On success, zero. - * On failure, a negative number and an error number is set in rte_errno. - * rte_errno can be: EINVAL - Invalid parameter passed. - * ENOEXEC - Specific OS error. + * On failure, return a positive errno-style error number. */ __rte_experimental int rte_thread_value_set(rte_thread_key key, const void *value); diff --git a/lib/librte_eal/include/rte_thread_types.h b/lib/librte_eal/include/rte_thread_types.h new file mode 100644 index 000000000..19fb85e38 --- /dev/null +++ b/lib/librte_eal/include/rte_thread_types.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2021 Microsoft Corporation + */ + +#ifndef _RTE_THREAD_TYPES_H_ +#define _RTE_THREAD_TYPES_H_ + +#include + +typedef pthread_t rte_thread_t; + +#endif /* _RTE_THREAD_TYPES_H_ */ diff --git a/lib/librte_eal/windows/include/rte_windows_thread_types.h b/lib/librte_eal/windows/include/rte_windows_thread_types.h new file mode 100644 index 000000000..ebd3d9e8f --- /dev/null +++ b/lib/librte_eal/windows/include/rte_windows_thread_types.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2021 Microsoft Corporation + */ + +#ifndef _RTE_THREAD_TYPES_H_ +#define _RTE_THREAD_TYPES_H_ + +#include + +typedef DWORD rte_thread_t; + +#endif /* _RTE_THREAD_TYPES_H_ */ diff --git a/lib/librte_eal/windows/rte_thread.c b/lib/librte_eal/windows/rte_thread.c index 667287c38..940d9c653 100644 --- a/lib/librte_eal/windows/rte_thread.c +++ b/lib/librte_eal/windows/rte_thread.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright 2021 Mellanox Technologies, Ltd + * Copyright(c) 2021 Microsoft Corporation */ #include @@ -11,6 +12,18 @@ struct eal_tls_key { DWORD thread_index; }; +rte_thread_t +rte_thread_self(void) +{ + return GetCurrentThreadId(); +} + +int +rte_thread_equal(rte_thread_t t1, rte_thread_t t2) +{ + return t1 == t2 ? 1 : 0; +} + int rte_thread_key_create(rte_thread_key *key, __rte_unused void (*destructor)(void *))