[ovs-dev] [PATCH v2 2/3] lib/ovs-thread: Use atomic_count.

Jarno Rajahalme jrajahalme at nicira.com
Fri Aug 29 21:10:43 UTC 2014


barrier->count is used as a simple counter and is not expected the
synchronize the state of any other variable, so we can use atomic_count,
which uses relaxed atomics.

Ditto for the 'next_id' within ovsthread_wrapper().

Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>
---
v2: Do not seq_read again after seq_change, as we know the value changed.

 lib/ovs-thread.c |   24 ++++++++++++------------
 lib/ovs-thread.h |    2 +-
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c
index 6d352ea..955c1c1 100644
--- a/lib/ovs-thread.c
+++ b/lib/ovs-thread.c
@@ -266,7 +266,7 @@ void
 ovs_barrier_init(struct ovs_barrier *barrier, uint32_t size)
 {
     barrier->size = size;
-    atomic_init(&barrier->count, 0);
+    atomic_count_init(&barrier->count, 0);
     barrier->seq = seq_create();
 }
 
@@ -289,19 +289,19 @@ ovs_barrier_block(struct ovs_barrier *barrier)
     uint64_t seq = seq_read(barrier->seq);
     uint32_t orig;
 
-    atomic_add(&barrier->count, 1, &orig);
+    orig = atomic_count_inc(&barrier->count);
     if (orig + 1 == barrier->size) {
-        atomic_store(&barrier->count, 0);
+        atomic_count_set(&barrier->count, 0);
         /* seq_change() serves as a release barrier against the other threads,
          * so the zeroed count is visible to them as they continue. */
         seq_change(barrier->seq);
-    }
-
-    /* To prevent thread from waking up by other event,
-     * keeps waiting for the change of 'barrier->seq'. */
-    while (seq == seq_read(barrier->seq)) {
-        seq_wait(barrier->seq, seq);
-        poll_block();
+    } else {
+        /* To prevent thread from waking up by other event,
+         * keeps waiting for the change of 'barrier->seq'. */
+        while (seq == seq_read(barrier->seq)) {
+            seq_wait(barrier->seq, seq);
+            poll_block();
+        }
     }
 }
 
@@ -316,13 +316,13 @@ struct ovsthread_aux {
 static void *
 ovsthread_wrapper(void *aux_)
 {
-    static atomic_uint next_id = ATOMIC_VAR_INIT(1);
+    static atomic_count next_id = ATOMIC_COUNT_INIT(1);
 
     struct ovsthread_aux *auxp = aux_;
     struct ovsthread_aux aux;
     unsigned int id;
 
-    atomic_add(&next_id, 1, &id);
+    id = atomic_count_inc(&next_id);
     *ovsthread_id_get() = id;
 
     aux = *auxp;
diff --git a/lib/ovs-thread.h b/lib/ovs-thread.h
index b2ac56e..962e867 100644
--- a/lib/ovs-thread.h
+++ b/lib/ovs-thread.h
@@ -34,7 +34,7 @@ struct OVS_LOCKABLE ovs_mutex {
 /* Poll-block()-able barrier similar to pthread_barrier_t. */
 struct ovs_barrier {
     uint32_t size;            /* Number of threads to wait. */
-    atomic_uint32_t count;    /* Number of threads already hit the barrier. */
+    atomic_count count;       /* Number of threads already hit the barrier. */
     struct seq *seq;
 };
 
-- 
1.7.10.4




More information about the dev mailing list