diff mbox series

[v6,07/10] eal: implement functions for mutex management

Message ID 1617413948-10504-8-git-send-email-navasile@linux.microsoft.com (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers show
Series eal: Add new API for threading | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Narcisa Ana Maria Vasile April 3, 2021, 1:39 a.m. UTC
From: Narcisa Vasile <navasile@microsoft.com>

Add functions for mutex init, destroy, lock, unlock.

Signed-off-by: Narcisa Vasile <navasile@microsoft.com>
---
 lib/librte_eal/common/rte_thread.c            | 24 +++++++++
 lib/librte_eal/include/rte_thread.h           | 53 +++++++++++++++++++
 lib/librte_eal/include/rte_thread_types.h     |  3 ++
 .../include/rte_windows_thread_types.h        |  1 +
 lib/librte_eal/windows/rte_thread.c           | 28 ++++++++++
 5 files changed, 109 insertions(+)

Comments

Dmitry Kozlyuk April 30, 2021, 5:47 p.m. UTC | #1
2021-04-02 18:39 (UTC-0700), Narcisa Ana Maria Vasile:
[...]
> diff --git a/lib/librte_eal/include/rte_thread_types.h b/lib/librte_eal/include/rte_thread_types.h
> index a884daf17..37bc7af2b 100644
> --- a/lib/librte_eal/include/rte_thread_types.h
> +++ b/lib/librte_eal/include/rte_thread_types.h
> @@ -7,9 +7,12 @@
>  
>  #include <pthread.h>
>  
> +#define RTE_THREAD_MUTEX_INITIALIZER     PTHREAD_MUTEX_INITIALIZER
> +

I'd wrap this in #ifndef RTE_EXEC_ENV_WINDOWS,
so that portable code couldn't accidentally rely on it.

>  #define EAL_THREAD_PRIORITY_NORMAL               0
>  #define EAL_THREAD_PRIORITY_REALTIME_CIRTICAL    99
>  
>  typedef pthread_t                       rte_thread_t;
> +typedef pthread_mutex_t                 rte_thread_mutex_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
> index 8cb4b3856..47c6b2664 100644
> --- a/lib/librte_eal/windows/include/rte_windows_thread_types.h
> +++ b/lib/librte_eal/windows/include/rte_windows_thread_types.h
> @@ -11,5 +11,6 @@
>  #define EAL_THREAD_PRIORITY_REALTIME_CIRTICAL  THREAD_PRIORITY_TIME_CRITICAL
>  
>  typedef DWORD                       rte_thread_t;
> +typedef CRITICAL_SECTION            rte_thread_mutex_t;
>  
>  #endif /* _RTE_THREAD_TYPES_H_ */

You have to ensure that sizeof(rte_thread_mutex_t) is the same with both
backends. MinGW's winpthread has "typedef void *pthread_mutex_t", while
CRITICAL_SECTION is much larger.

If you decide to make rte_thread_mutext_t a pointer on Windows,
consider implementing RTE_THREAD_MUTEX_INITILIZER.
diff mbox series

Patch

diff --git a/lib/librte_eal/common/rte_thread.c b/lib/librte_eal/common/rte_thread.c
index 29d38d193..8e963ed65 100644
--- a/lib/librte_eal/common/rte_thread.c
+++ b/lib/librte_eal/common/rte_thread.c
@@ -220,6 +220,30 @@  rte_thread_join(rte_thread_t thread_id, int *value_ptr)
 	return 0;
 }
 
+int
+rte_thread_mutex_init(rte_thread_mutex_t *mutex)
+{
+	return pthread_mutex_init(mutex, NULL);
+}
+
+int
+rte_thread_mutex_lock(rte_thread_mutex_t *mutex)
+{
+	return pthread_mutex_lock(mutex);
+}
+
+int
+rte_thread_mutex_unlock(rte_thread_mutex_t *mutex)
+{
+	return pthread_mutex_unlock(mutex);
+}
+
+int
+rte_thread_mutex_destroy(rte_thread_mutex_t *mutex)
+{
+	return pthread_mutex_destroy(mutex);
+}
+
 int rte_thread_cancel(rte_thread_t thread_id)
 {
 	/*
diff --git a/lib/librte_eal/include/rte_thread.h b/lib/librte_eal/include/rte_thread.h
index fa643433a..4ec7feca8 100644
--- a/lib/librte_eal/include/rte_thread.h
+++ b/lib/librte_eal/include/rte_thread.h
@@ -240,6 +240,58 @@  int rte_thread_create(rte_thread_t *thread_id,
 __rte_experimental
 int rte_thread_join(rte_thread_t thread_id, int *value_ptr);
 
+/**
+ * Initializes a mutex.
+ *
+ * @param mutex
+ *    The mutex to be initialized.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_mutex_init(rte_thread_mutex_t *mutex);
+
+/**
+ * Locks a mutex.
+ *
+ * @param mutex
+ *    The mutex to be locked.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_mutex_lock(rte_thread_mutex_t *mutex);
+
+/**
+ * Unlocks a mutex.
+ *
+ * @param mutex
+ *    The mutex to be unlocked.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_mutex_unlock(rte_thread_mutex_t *mutex);
+
+/**
+ * Releases all resources associated with a mutex.
+ *
+ * @param mutex
+ *    The mutex to be uninitialized.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_mutex_destroy(rte_thread_mutex_t *mutex);
+
 /**
  * Terminates a thread.
  *
@@ -259,6 +311,7 @@  int rte_thread_cancel(rte_thread_t thread_id);
  *
  * @param cpusetp
  *   Pointer to CPU affinity to set.
+ *
  * @return
  *   On success, return 0; otherwise return -1;
  */
diff --git a/lib/librte_eal/include/rte_thread_types.h b/lib/librte_eal/include/rte_thread_types.h
index a884daf17..37bc7af2b 100644
--- a/lib/librte_eal/include/rte_thread_types.h
+++ b/lib/librte_eal/include/rte_thread_types.h
@@ -7,9 +7,12 @@ 
 
 #include <pthread.h>
 
+#define RTE_THREAD_MUTEX_INITIALIZER     PTHREAD_MUTEX_INITIALIZER
+
 #define EAL_THREAD_PRIORITY_NORMAL               0
 #define EAL_THREAD_PRIORITY_REALTIME_CIRTICAL    99
 
 typedef pthread_t                       rte_thread_t;
+typedef pthread_mutex_t                 rte_thread_mutex_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
index 8cb4b3856..47c6b2664 100644
--- a/lib/librte_eal/windows/include/rte_windows_thread_types.h
+++ b/lib/librte_eal/windows/include/rte_windows_thread_types.h
@@ -11,5 +11,6 @@ 
 #define EAL_THREAD_PRIORITY_REALTIME_CIRTICAL  THREAD_PRIORITY_TIME_CRITICAL
 
 typedef DWORD                       rte_thread_t;
+typedef CRITICAL_SECTION            rte_thread_mutex_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 86bbd7bc2..c1221c2ea 100644
--- a/lib/librte_eal/windows/rte_thread.c
+++ b/lib/librte_eal/windows/rte_thread.c
@@ -421,6 +421,34 @@  rte_thread_join(rte_thread_t thread_id, int *value_ptr)
 	return ret;
 }
 
+int
+rte_thread_mutex_init(rte_thread_mutex_t *mutex)
+{
+	InitializeCriticalSection(mutex);
+	return 0;
+}
+
+int
+rte_thread_mutex_lock(rte_thread_mutex_t *mutex)
+{
+	EnterCriticalSection(mutex);
+	return 0;
+}
+
+int
+rte_thread_mutex_unlock(rte_thread_mutex_t *mutex)
+{
+	LeaveCriticalSection(mutex);
+	return 0;
+}
+
+int
+rte_thread_mutex_destroy(rte_thread_mutex_t *mutex)
+{
+	DeleteCriticalSection(mutex);
+	return 0;
+}
+
 int
 rte_thread_cancel(rte_thread_t thread_id)
 {