[dpdk-dev] [PATCH 1/2] eal/windows: add pthread mutex lock
Dmitry Kozlyuk
dmitry.kozliuk at gmail.com
Sun Sep 27 17:56:57 CEST 2020
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 at 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
More information about the dev
mailing list