Drivers and libraries that place a pthread_mutex_t in shared memory to coordinate primary and secondary DPDK processes must initialize the mutex with PTHREAD_PROCESS_SHARED, otherwise behaviour is undefined. Add an internal helper so this is done in one place rather than copied into every driver.
Errors from the attribute calls are ignored: on platforms that do not support cross-process mutex sharing the mutex still works within a single process. Bugzilla ID: 662 Cc: [email protected] Signed-off-by: Stephen Hemminger <[email protected]> --- lib/eal/include/rte_thread.h | 15 +++++++++++++++ lib/eal/unix/rte_thread.c | 12 ++++++++++++ lib/eal/windows/rte_thread.c | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h index 8da9d4d3fb..27d5b9c594 100644 --- a/lib/eal/include/rte_thread.h +++ b/lib/eal/include/rte_thread.h @@ -3,6 +3,7 @@ * Copyright (C) 2022 Microsoft Corporation */ +#include <pthread.h> #include <stdint.h> #include <rte_os.h> @@ -463,6 +464,20 @@ int rte_thread_value_set(rte_thread_key key, const void *value); */ void *rte_thread_value_get(rte_thread_key key); +/** + * Initialize a pthread mutex for use in shared memory accessible by + * multiple DPDK processes. + * + * Sets PTHREAD_PROCESS_SHARED so the mutex can synchronize threads + * across DPDK primary and secondary processes when the mutex resides + * in shared memory (e.g. hugepage memory). + * + * @param mutex + * The mutex to initialize. + */ +__rte_internal +void rte_thread_mutex_init_shared(pthread_mutex_t *mutex); + #ifdef __cplusplus } #endif diff --git a/lib/eal/unix/rte_thread.c b/lib/eal/unix/rte_thread.c index 950c0848ba..f3a6bd2ad3 100644 --- a/lib/eal/unix/rte_thread.c +++ b/lib/eal/unix/rte_thread.c @@ -419,3 +419,15 @@ rte_thread_get_affinity_by_id(rte_thread_t thread_id, return pthread_getaffinity_np((pthread_t)thread_id.opaque_id, sizeof(*cpuset), cpuset); } + +RTE_EXPORT_INTERNAL_SYMBOL(rte_thread_mutex_init_shared) +void +rte_thread_mutex_init_shared(pthread_mutex_t *mutex) +{ + pthread_mutexattr_t attr; + + pthread_mutexattr_init(&attr); + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(mutex, &attr); + pthread_mutexattr_destroy(&attr); +} diff --git a/lib/eal/windows/rte_thread.c b/lib/eal/windows/rte_thread.c index 85e5a57346..19bb9ed61d 100644 --- a/lib/eal/windows/rte_thread.c +++ b/lib/eal/windows/rte_thread.c @@ -621,3 +621,13 @@ rte_thread_get_affinity_by_id(rte_thread_t thread_id, } return ret; } + +/* Note: Windows does not have PTHREAD_PROCESS_SHARED + * and DPDK on Windows only supports single process model. + */ +RTE_EXPORT_INTERNAL_SYMBOL(rte_thread_mutex_init_shared) +void +rte_thread_mutex_init_shared(pthread_mutex_t *mutex) +{ + pthread_mutex_init(mutex, NULL); +} -- 2.53.0

