vppinfra: refactor use of CLIB_MEMORY_BARRIER ()

All instances of test_and_set locks used the following sequence
to release the locks:

CLIB_MEMORY_BARRIER ();
p->lock = 0; // p is a generic struct with a TAS lock

Use clib_atomic_release to generate more efficient assembly code.

Type: refactor

Change-Id: Idca3a38b1cf43578108bdd1afe83b6ebc17a4c68
Signed-off-by: Jason Zhang <jason.zhang2@arm.com>
Reviewed-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
Reviewed-by: Lijian Zhang <Lijian.Zhang@arm.com>
diff --git a/src/plugins/map/map.h b/src/plugins/map/map.h
index d96c7b8..6dc5232 100644
--- a/src/plugins/map/map.h
+++ b/src/plugins/map/map.h
@@ -503,7 +503,7 @@
 map_ip4_reass_free(map_ip4_reass_t *r, u32 **pi_to_drop);
 
 #define map_ip4_reass_lock() while (clib_atomic_test_and_set (map_main.ip4_reass_lock)) { CLIB_PAUSE (); }
-#define map_ip4_reass_unlock() do {CLIB_MEMORY_BARRIER(); *map_main.ip4_reass_lock = 0;} while(0)
+#define map_ip4_reass_unlock() clib_atomic_release (map_main.ip4_reass_lock)
 
 static_always_inline void
 map_ip4_reass_get_fragments(map_ip4_reass_t *r, u32 **pi)
@@ -528,7 +528,7 @@
 map_ip6_reass_free(map_ip6_reass_t *r, u32 **pi_to_drop);
 
 #define map_ip6_reass_lock() while (clib_atomic_test_and_set (map_main.ip6_reass_lock)) { CLIB_PAUSE (); }
-#define map_ip6_reass_unlock() do {CLIB_MEMORY_BARRIER(); *map_main.ip6_reass_lock = 0;} while(0)
+#define map_ip6_reass_unlock() clib_atomic_release (map_main.ip6_reass_lock)
 
 int
 map_ip6_reass_add_fragment(map_ip6_reass_t *r, u32 pi,
diff --git a/src/svm/ssvm.h b/src/svm/ssvm.h
index 6c67945..60e0cc6 100644
--- a/src/svm/ssvm.h
+++ b/src/svm/ssvm.h
@@ -128,8 +128,7 @@
     {
       h->owner_pid = 0;
       h->tag = 0;
-      CLIB_MEMORY_BARRIER ();
-      h->lock = 0;
+      clib_atomic_release (&h->lock);
     }
 }
 
@@ -137,8 +136,7 @@
 ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
 {
   h->tag = 0;
-  CLIB_MEMORY_BARRIER ();
-  h->lock = 0;
+  clib_atomic_release (&h->lock);
 }
 
 static inline void *
diff --git a/src/vnet/classify/vnet_classify.c b/src/vnet/classify/vnet_classify.c
index 64549a6..f5e4949 100755
--- a/src/vnet/classify/vnet_classify.c
+++ b/src/vnet/classify/vnet_classify.c
@@ -640,8 +640,7 @@
   vnet_classify_entry_free (t, v, old_log2_pages);
 
 unlock:
-  CLIB_MEMORY_BARRIER ();
-  t->writer_lock[0] = 0;
+  clib_atomic_release (&t->writer_lock[0]);
   return rv;
 }
 
diff --git a/src/vnet/dns/dns.h b/src/vnet/dns/dns.h
index 19de69c..d5029e5 100644
--- a/src/vnet/dns/dns.h
+++ b/src/vnet/dns/dns.h
@@ -206,8 +206,7 @@
 {
   if (dm->cache_lock)
     {
-      CLIB_MEMORY_BARRIER ();
-      *dm->cache_lock = 0;
+      clib_atomic_release (dm->cache_lock);
     }
 }
 
diff --git a/src/vppinfra/elog.c b/src/vppinfra/elog.c
index 489ccf4..caddf6f 100644
--- a/src/vppinfra/elog.c
+++ b/src/vppinfra/elog.c
@@ -56,8 +56,7 @@
 {
   if (PREDICT_FALSE (em->lock != 0))
     {
-      CLIB_MEMORY_BARRIER ();
-      *em->lock = 0;
+      clib_atomic_release (em->lock);
     }
 }
 
diff --git a/src/vppinfra/lock.h b/src/vppinfra/lock.h
index 337c5a3..59ab0e3 100644
--- a/src/vppinfra/lock.h
+++ b/src/vppinfra/lock.h
@@ -89,9 +89,8 @@
 clib_spinlock_unlock (clib_spinlock_t * p)
 {
   CLIB_LOCK_DBG_CLEAR (p);
-  /* Make sure all writes are complete before releasing the lock */
-  CLIB_MEMORY_BARRIER ();
-  (*p)->lock = 0;
+  /* Make sure all reads/writes are complete before releasing the lock */
+  clib_atomic_release (&(*p)->lock);
 }
 
 static_always_inline void
@@ -147,9 +146,7 @@
       while (clib_atomic_test_and_set (&(*p)->writer_lock))
 	CLIB_PAUSE ();
     }
-  CLIB_MEMORY_BARRIER ();
-  (*p)->n_readers_lock = 0;
-
+  clib_atomic_release (&(*p)->n_readers_lock);
   CLIB_LOCK_DBG (p);
 }
 
@@ -165,12 +162,9 @@
   (*p)->n_readers -= 1;
   if ((*p)->n_readers == 0)
     {
-      CLIB_MEMORY_BARRIER ();
-      (*p)->writer_lock = 0;
+      clib_atomic_release (&(*p)->writer_lock);
     }
-
-  CLIB_MEMORY_BARRIER ();
-  (*p)->n_readers_lock = 0;
+  clib_atomic_release (&(*p)->n_readers_lock);
 }
 
 always_inline void
@@ -185,8 +179,7 @@
 clib_rwlock_writer_unlock (clib_rwlock_t * p)
 {
   CLIB_LOCK_DBG_CLEAR (p);
-  CLIB_MEMORY_BARRIER ();
-  (*p)->writer_lock = 0;
+  clib_atomic_release (&(*p)->writer_lock);
 }
 
 #endif
diff --git a/src/vppinfra/mheap.c b/src/vppinfra/mheap.c
index 2769838..b5566bd 100644
--- a/src/vppinfra/mheap.c
+++ b/src/vppinfra/mheap.c
@@ -82,8 +82,7 @@
       if (--h->recursion_count == 0)
 	{
 	  h->owner_cpu = ~0;
-	  CLIB_MEMORY_BARRIER ();
-	  h->lock = 0;
+	  clib_atomic_release (&h->lock);
 	}
     }
 }