From: Narcisa Vasile <navas...@microsoft.com> 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 <navas...@microsoft.com> --- lib/librte_eal/common/rte_thread.c | 101 ++++++++++++++++++ lib/librte_eal/include/rte_thread.h | 39 ++++++- 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(+), 3 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..9b5cee890 --- /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 <errno.h> +#include <pthread.h> +#include <stdlib.h> +#include <string.h> + +#include <rte_common.h> +#include <rte_errno.h> +#include <rte_log.h> +#include <rte_thread.h> + +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_tls_key_create(rte_tls_key *key, void (*destructor)(void *)) +{ + int err; + rte_tls_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_tls_key_delete(rte_tls_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_tls_value_set(rte_tls_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_tls_value_get(rte_tls_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 ac5a89b1a..f7d398a22 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 <rte_os.h> @@ -20,6 +21,13 @@ extern "C" { #endif +#include <sched.h> +#if defined(RTE_USE_WINDOWS_THREAD_TYPES) +#include <rte_windows_thread_types.h> +#else +#include <rte_thread_types.h> +#endif + /** * TLS key type, an opaque pointer. */ @@ -27,6 +35,31 @@ typedef struct eal_tls_key *rte_tls_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,7 +96,7 @@ void rte_thread_get_affinity(rte_cpuset_t *cpusetp); * * @return * On success, zero. - * On failure, a negative number. + * On failure, return a positive errno-style error number. */ __rte_experimental @@ -77,7 +110,7 @@ int rte_thread_tls_key_create(rte_tls_key *key, void (*destructor)(void *)); * * @return * On success, zero. - * On failure, a negative number. + * On failure, return a positive errno-style error number. */ __rte_experimental int rte_thread_tls_key_delete(rte_tls_key key); @@ -92,7 +125,7 @@ int rte_thread_tls_key_delete(rte_tls_key key); * * @return * On success, zero. - * On failure, a negative number. + * On failure, return a positive errno-style error number. */ __rte_experimental int rte_thread_tls_value_set(rte_tls_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 <pthread.h> + +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 <rte_windows.h> + +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 2e2ab2917..43c09b2f3 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 <rte_common.h> @@ -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_tls_key_create(rte_tls_key *key, __rte_unused void (*destructor)(void *)) -- 2.30.0.vfs.0.2