[1/2] eal/windows: add pthread mutex lock
diff mbox series

Message ID 1601194817-208834-2-git-send-email-suanmingm@nvidia.com
State Superseded
Delegated to: Thomas Monjalon
Headers show
Series
  • ethdev: make rte flow API thread safe
Related show

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Suanming Mou Sept. 27, 2020, 8:20 a.m. UTC
Add pthread mutex lock as it is needed for the thread safe rte flow
functions.

Signed-off-by: Suanming Mou <suanmingm@nvidia.com>
---
 lib/librte_eal/windows/include/pthread.h | 46 ++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

Comments

Dmitry Kozlyuk Sept. 27, 2020, 3:56 p.m. UTC | #1
Hi Suanming,

There's a remark in patch 2/2 and cover letter:

> If no lock contention
> with the added rte flow level mutex, the mutex only does the atomic
> increasing in pthread_mutex_lock() and decreasing in
> pthread_mutex_unlock(). No futex() syscall will be involved.

Is this property important? To get the described behavior on Windows, you
should've used CRITICAL_SECTION (preferably wrapped in a struct). Mutexes are
kernel objects on Windows and always require syscalls. Otherwise, if mutexes
are sufficient, see a comment inline.

> Add pthread mutex lock as it is needed for the thread safe rte flow
> functions.
> 
> Signed-off-by: Suanming Mou <suanmingm@nvidia.com>
> ---
>  lib/librte_eal/windows/include/pthread.h | 46 ++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/lib/librte_eal/windows/include/pthread.h b/lib/librte_eal/windows/include/pthread.h
> index 99013dc..4e2e0b3 100644
> --- a/lib/librte_eal/windows/include/pthread.h
> +++ b/lib/librte_eal/windows/include/pthread.h
> @@ -28,6 +28,10 @@
>  /* defining pthread_attr_t type on Windows since there is no in Microsoft libc*/
>  typedef void *pthread_attr_t;
>  
> +typedef void *pthread_mutexattr_t;
> +
> +typedef HANDLE pthread_mutex_t;
> +
>  typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
>  
>  #define pthread_barrier_init(barrier, attr, count) \
> @@ -139,6 +143,48 @@
>  	return 0;
>  }
>  
> +static inline int
> +pthread_mutex_init(pthread_mutex_t *mutex,
> +		   __rte_unused pthread_mutexattr_t *attr)
> +{
> +	*mutex = CreateMutex(NULL, FALSE, NULL);
> +	if (*mutex == NULL) {
> +		RTE_LOG_WIN32_ERR("CreateMutex()");
> +		return -1;
> +	}
> +	return 0;
> +}
> +
> +static inline int
> +pthread_mutex_lock(pthread_mutex_t *mutex)
> +{
> +	if (WaitForSingleObject(*mutex, INFINITE) != WAIT_OBJECT_0) {
> +		RTE_LOG_WIN32_ERR("WaitForSingleObject()");
> +		return -1;

A relevant error code must be returned according to POSIX. Searching the
code for pthread_mutex_lock() calls, I can see that hinic PMD checks for
EOWNERDEAD (corresponding to WAIT_OBJECT_ABANDONED in Windows) and failsafe
PMD supplies return value of pthread_mutex_unlock() to strerror(), i.e. it
should be an errno. Same applies to other functions.

> +	}
> +	return 0;
> +}
> +
> +static inline int
> +pthread_mutex_unlock(pthread_mutex_t *mutex)
> +{
> +	if (!ReleaseMutex(*mutex)) {
> +		RTE_LOG_WIN32_ERR("ReleaseMutex()");
> +		return -1;
> +	}
> +	return 0;
> +}
> +
> +static inline int
> +pthread_mutex_destroy(pthread_mutex_t *mutex)
> +{
> +	if (!CloseHandle(*mutex)) {
> +		RTE_LOG_WIN32_ERR("CloseHandle()");
> +		return -1;
> +	}
> +	return 0;
> +}
> +
>  #ifdef __cplusplus
>  }
>  #endif
Suanming Mou Sept. 28, 2020, 2:30 a.m. UTC | #2
Hi Dmitry,

> -----Original Message-----
> From: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> Sent: Sunday, September 27, 2020 11:57 PM
> To: Suanming Mou <suanmingm@nvidia.com>
> Cc: Narcisa Ana Maria Vasile <navasile@linux.microsoft.com>; Dmitry Malloy
> <dmitrym@microsoft.com>; Pallavi Kadam <pallavi.kadam@intel.com>;
> dev@dpdk.org
> Subject: Re: [PATCH 1/2] eal/windows: add pthread mutex lock
> 
> Hi Suanming,
> 
> There's a remark in patch 2/2 and cover letter:
> 
> > If no lock contention
> > with the added rte flow level mutex, the mutex only does the atomic
> > increasing in pthread_mutex_lock() and decreasing in
> > pthread_mutex_unlock(). No futex() syscall will be involved.
> 
> Is this property important? To get the described behavior on Windows, you
> should've used CRITICAL_SECTION (preferably wrapped in a struct). Mutexes are
> kernel objects on Windows and always require syscalls. Otherwise, if mutexes
> are sufficient, see a comment inline.

The description was valid only for the standard posix pthread functions. Good to know that there are similar functions on Windows.
I will prefer to change it to CRICTIAL_SECTION functions, in this case the pthread wrap functions on Windows will also have less impact with the current applications.
Thank you very much for the information.

> 
> > Add pthread mutex lock as it is needed for the thread safe rte flow
> > functions.
> >
> > Signed-off-by: Suanming Mou <suanmingm@nvidia.com>
> > ---
> >  lib/librte_eal/windows/include/pthread.h | 46
> > ++++++++++++++++++++++++++++++++
> >  1 file changed, 46 insertions(+)
> >
> > diff --git a/lib/librte_eal/windows/include/pthread.h
> > b/lib/librte_eal/windows/include/pthread.h
> > index 99013dc..4e2e0b3 100644
> > --- a/lib/librte_eal/windows/include/pthread.h
> > +++ b/lib/librte_eal/windows/include/pthread.h
> > @@ -28,6 +28,10 @@
> >  /* defining pthread_attr_t type on Windows since there is no in
> > Microsoft libc*/  typedef void *pthread_attr_t;
> >
> > +typedef void *pthread_mutexattr_t;
> > +
> > +typedef HANDLE pthread_mutex_t;
> > +
> >  typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
> >
> >  #define pthread_barrier_init(barrier, attr, count) \ @@ -139,6
> > +143,48 @@
> >  	return 0;
> >  }
> >
> > +static inline int
> > +pthread_mutex_init(pthread_mutex_t *mutex,
> > +		   __rte_unused pthread_mutexattr_t *attr) {
> > +	*mutex = CreateMutex(NULL, FALSE, NULL);
> > +	if (*mutex == NULL) {
> > +		RTE_LOG_WIN32_ERR("CreateMutex()");
> > +		return -1;
> > +	}
> > +	return 0;
> > +}
> > +
> > +static inline int
> > +pthread_mutex_lock(pthread_mutex_t *mutex) {
> > +	if (WaitForSingleObject(*mutex, INFINITE) != WAIT_OBJECT_0) {
> > +		RTE_LOG_WIN32_ERR("WaitForSingleObject()");
> > +		return -1;
> 
> A relevant error code must be returned according to POSIX. Searching the code
> for pthread_mutex_lock() calls, I can see that hinic PMD checks for
> EOWNERDEAD (corresponding to WAIT_OBJECT_ABANDONED in Windows) and
> failsafe PMD supplies return value of pthread_mutex_unlock() to strerror(), i.e. it
> should be an errno. Same applies to other functions.

These PMDs should not be valid on Windows now, or the build will be failed as no pthread_mutex on Windows.
I guess we will have a much general solution with the posix APIs support on Windows?
Now the wrap functions solution is much like a WA to fix the build.

> 
> > +	}
> > +	return 0;
> > +}
> > +
> > +static inline int
> > +pthread_mutex_unlock(pthread_mutex_t *mutex) {
> > +	if (!ReleaseMutex(*mutex)) {
> > +		RTE_LOG_WIN32_ERR("ReleaseMutex()");
> > +		return -1;
> > +	}
> > +	return 0;
> > +}
> > +
> > +static inline int
> > +pthread_mutex_destroy(pthread_mutex_t *mutex) {
> > +	if (!CloseHandle(*mutex)) {
> > +		RTE_LOG_WIN32_ERR("CloseHandle()");
> > +		return -1;
> > +	}
> > +	return 0;
> > +}
> > +
> >  #ifdef __cplusplus
> >  }
> >  #endif

Patch
diff mbox series

diff --git a/lib/librte_eal/windows/include/pthread.h b/lib/librte_eal/windows/include/pthread.h
index 99013dc..4e2e0b3 100644
--- a/lib/librte_eal/windows/include/pthread.h
+++ b/lib/librte_eal/windows/include/pthread.h
@@ -28,6 +28,10 @@ 
 /* defining pthread_attr_t type on Windows since there is no in Microsoft libc*/
 typedef void *pthread_attr_t;
 
+typedef void *pthread_mutexattr_t;
+
+typedef HANDLE pthread_mutex_t;
+
 typedef SYNCHRONIZATION_BARRIER pthread_barrier_t;
 
 #define pthread_barrier_init(barrier, attr, count) \
@@ -139,6 +143,48 @@ 
 	return 0;
 }
 
+static inline int
+pthread_mutex_init(pthread_mutex_t *mutex,
+		   __rte_unused pthread_mutexattr_t *attr)
+{
+	*mutex = CreateMutex(NULL, FALSE, NULL);
+	if (*mutex == NULL) {
+		RTE_LOG_WIN32_ERR("CreateMutex()");
+		return -1;
+	}
+	return 0;
+}
+
+static inline int
+pthread_mutex_lock(pthread_mutex_t *mutex)
+{
+	if (WaitForSingleObject(*mutex, INFINITE) != WAIT_OBJECT_0) {
+		RTE_LOG_WIN32_ERR("WaitForSingleObject()");
+		return -1;
+	}
+	return 0;
+}
+
+static inline int
+pthread_mutex_unlock(pthread_mutex_t *mutex)
+{
+	if (!ReleaseMutex(*mutex)) {
+		RTE_LOG_WIN32_ERR("ReleaseMutex()");
+		return -1;
+	}
+	return 0;
+}
+
+static inline int
+pthread_mutex_destroy(pthread_mutex_t *mutex)
+{
+	if (!CloseHandle(*mutex)) {
+		RTE_LOG_WIN32_ERR("CloseHandle()");
+		return -1;
+	}
+	return 0;
+}
+
 #ifdef __cplusplus
 }
 #endif