From bbfbadf59aae7952be154d9744f2ff661d0b2faa Mon Sep 17 00:00:00 2001 From: Darek Stojaczyk Date: Sun, 17 Mar 2019 01:57:42 +0100 Subject: [PATCH] vhost: add spdk_vhost_trylock() We'll make use of it inside the vhost device backend code. The function itself is generic enough to be put in the public vhost.h header rather than vhost_internal.h. Change-Id: I60602c61d8bba665dcf9c6d27af2e910c208a7be Signed-off-by: Darek Stojaczyk Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/448226 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Changpeng Liu --- include/spdk/vhost.h | 9 ++++++++- lib/vhost/vhost.c | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/spdk/vhost.h b/include/spdk/vhost.h index 201f6bced..5cfe14a54 100644 --- a/include/spdk/vhost.h +++ b/include/spdk/vhost.h @@ -109,10 +109,17 @@ void spdk_vhost_shutdown_cb(void); struct spdk_vhost_dev; /** - * Lock the global vhost mutex, which synchronizes all the vhost device accesses. + * Lock the global vhost mutex synchronizing all the vhost device accesses. */ void spdk_vhost_lock(void); +/** + * Lock the global vhost mutex synchronizing all the vhost device accesses. + * + * \return 0 if the mutex could be locked immediately, negative errno otherwise. + */ +int spdk_vhost_trylock(void); + /** * Unlock the global vhost mutex. */ diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 49c0aa9cf..55f56d5da 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -1403,6 +1403,12 @@ spdk_vhost_lock(void) pthread_mutex_lock(&g_spdk_vhost_mutex); } +int +spdk_vhost_trylock(void) +{ + return -pthread_mutex_trylock(&g_spdk_vhost_mutex); +} + void spdk_vhost_unlock(void) {