[ovs-dev] [PATCH v2 1/2] ovs-thread: Support spinlock for non pthread spinlock platform

wenxu at ucloud.cn wenxu at ucloud.cn
Fri Jul 9 08:38:49 UTC 2021


From: wenxu <wenxu at ucloud.cn>

Add spinlock for non pthread_spinlock platform. It using the
mutex lock. And always busy trylock to acquire the lock.

Signed-off-by: wenxu <wenxu at ucloud.cn>
---
 include/openvswitch/thread.h |  4 ++--
 lib/ovs-thread.c             | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/include/openvswitch/thread.h b/include/openvswitch/thread.h
index acc8229..bd46879 100644
--- a/include/openvswitch/thread.h
+++ b/include/openvswitch/thread.h
@@ -38,6 +38,8 @@ struct OVS_LOCKABLE ovs_spin {
     pthread_spinlock_t lock;
     const char *where;          /* NULL if and only if uninitialized. */
 };
+#else
+#define ovs_spin ovs_mutex
 #endif
 
 /* "struct ovs_mutex" initializer. */
@@ -78,7 +80,6 @@ int ovs_mutex_trylock_at(const struct ovs_mutex *mutex, const char *where)
 void ovs_mutex_cond_wait(pthread_cond_t *, const struct ovs_mutex *mutex)
     OVS_REQUIRES(mutex);
 
-#ifdef HAVE_PTHREAD_SPIN_LOCK
 void ovs_spin_init(const struct ovs_spin *);
 void ovs_spin_destroy(const struct ovs_spin *);
 void ovs_spin_unlock(const struct ovs_spin *spin) OVS_RELEASES(spin);
@@ -91,7 +92,6 @@ int ovs_spin_trylock_at(const struct ovs_spin *spin, const char *where)
     OVS_TRY_LOCK(0, spin);
 #define ovs_spin_trylock(spin) \
         ovs_spin_trylock_at(spin, OVS_SOURCE_LOCATOR)
-#endif
 
 /* Convenient once-only execution.
  *
diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c
index b686e45..86ef4f1 100644
--- a/lib/ovs-thread.c
+++ b/lib/ovs-thread.c
@@ -77,6 +77,11 @@ LOCK_FUNCTION(rwlock, rdlock);
 LOCK_FUNCTION(rwlock, wrlock);
 #ifdef HAVE_PTHREAD_SPIN_LOCK
 LOCK_FUNCTION(spin, lock);
+#else
+void ovs_spin_lock_at(const struct ovs_spin *lock, const char *where)
+{
+   while (ovs_mutex_trylock_at(lock, where));
+}
 #endif
 
 #define TRY_LOCK_FUNCTION(TYPE, FUN) \
@@ -108,6 +113,11 @@ TRY_LOCK_FUNCTION(rwlock, tryrdlock);
 TRY_LOCK_FUNCTION(rwlock, trywrlock);
 #ifdef HAVE_PTHREAD_SPIN_LOCK
 TRY_LOCK_FUNCTION(spin, trylock);
+#else
+int ovs_spin_trylock_at(const struct ovs_spin *spin, const char *where)
+{
+    return ovs_mutex_trylock_at(spin, where);
+}
 #endif
 
 #define UNLOCK_FUNCTION(TYPE, FUN, WHERE) \
@@ -134,6 +144,15 @@ UNLOCK_FUNCTION(rwlock, destroy, NULL);
 #ifdef HAVE_PTHREAD_SPIN_LOCK
 UNLOCK_FUNCTION(spin, unlock, "<unlocked>");
 UNLOCK_FUNCTION(spin, destroy, NULL);
+#else
+void ovs_spin_unlock(const struct ovs_spin *lock)
+{
+    ovs_mutex_unlock(lock);
+}
+void ovs_spin_destroy(const struct ovs_spin *lock)
+{
+    ovs_mutex_destroy(lock);
+}
 #endif
 
 #define XPTHREAD_FUNC1(FUNCTION, PARAM1)                \
@@ -297,6 +316,12 @@ ovs_spin_init(const struct ovs_spin *spin)
 {
     ovs_spin_init__(spin, PTHREAD_PROCESS_PRIVATE);
 }
+#else
+void
+ovs_spin_init(const struct ovs_spin *spin)
+{
+    ovs_mutex_init(spin);
+}
 #endif
 
 /* Initializes the 'barrier'.  'size' is the number of threads
-- 
1.8.3.1



More information about the dev mailing list