From 6f0088655a776a8aa763e4889a94470ea61a59c9 Mon Sep 17 00:00:00 2001 From: Jarno Rajahalme Date: Fri, 29 Aug 2014 16:15:44 -0700 Subject: [PATCH] lib/ovs-thread: Use atomic_count. 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 Acked-by: Ben Pfaff --- 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 6d352eace..955c1c174 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 b2ac56eb1..962e8679e 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; }; -- 2.20.1