[v2] eal/windows: add pthread TLS function support

Message ID 20201213202437.12880-1-talshn@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series [v2] eal/windows: add pthread TLS function support |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-intel-Performance success Performance Testing PASS
ci/travis-robot success Travis build: passed

Commit Message

Tal Shnaiderman Dec. 13, 2020, 8:24 p.m. UTC
  Add the following functions to the pthread shim implementation
for Windows as they are needed for thread safe rte_flow functions.

pthread_key_create.
pthread_key_delete.
pthread_getspecific.
pthread_setspecific.

Signed-off-by: Tal Shnaiderman <talshn@nvidia.com>
---
v2: fix style issues
---
 lib/librte_eal/windows/include/pthread.h | 43 ++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
  

Comments

Dmitry Kozlyuk Dec. 15, 2020, 10:36 p.m. UTC | #1
On Sun, 13 Dec 2020 22:24:37 +0200, Tal Shnaiderman wrote:
> Add the following functions to the pthread shim implementation
> for Windows as they are needed for thread safe rte_flow functions.
> 
> pthread_key_create.
> pthread_key_delete.
> pthread_getspecific.
> pthread_setspecific.
> 
> Signed-off-by: Tal Shnaiderman <talshn@nvidia.com>
> ---
> v2: fix style issues
> ---
>  lib/librte_eal/windows/include/pthread.h | 43 ++++++++++++++++++++++++++++++++
>  1 file changed, 43 insertions(+)

It's been planned to remove the shim, not to extend it; and to introduce a
generic threading API in EAL. Why not start with these functions? It's quite
an isolated set.
  

Patch

diff --git a/lib/librte_eal/windows/include/pthread.h b/lib/librte_eal/windows/include/pthread.h
index fb11a07ce6..8fe18e6f00 100644
--- a/lib/librte_eal/windows/include/pthread.h
+++ b/lib/librte_eal/windows/include/pthread.h
@@ -34,6 +34,8 @@  typedef CRITICAL_SECTION pthread_mutex_t;
 
 typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
 
+typedef DWORD pthread_key_t;
+
 #define pthread_barrier_init(barrier, attr, count) \
 	InitializeSynchronizationBarrier(barrier, count, -1)
 #define pthread_barrier_wait(barrier) EnterSynchronizationBarrier(barrier, \
@@ -179,6 +181,47 @@  pthread_mutex_destroy(pthread_mutex_t *mutex)
 	return 0;
 }
 
+static inline int
+pthread_key_create(pthread_key_t *key,
+		    __rte_unused void (*destructor)(void *))
+{
+	*key = TlsAlloc();
+	if ((*key) == TLS_OUT_OF_INDEXES) {
+		RTE_LOG_WIN32_ERR("TlsAlloc()");
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+static inline int
+pthread_key_delete(pthread_key_t key)
+{
+	if (!TlsFree(key)) {
+		RTE_LOG_WIN32_ERR("TlsFree()");
+		return -1;
+	}
+	return 0;
+}
+
+static inline void *
+pthread_getspecific(pthread_key_t key)
+{
+	return TlsGetValue(key);
+}
+
+static inline int
+pthread_setspecific(pthread_key_t key, const void *value)
+{
+	/* discard const qualifier */
+	char *p = (char *) (uintptr_t) value;
+
+	if (!TlsSetValue(key, p)) {
+		RTE_LOG_WIN32_ERR("TlsSetValue()");
+		return -1;
+	}
+	return 0;
+}
+
 #ifdef __cplusplus
 }
 #endif