Skip to content

Commit

Permalink
lib/ovs-thread: Use atomic_count.
Browse files Browse the repository at this point in the history
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@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
  • Loading branch information
Jarno Rajahalme committed Aug 29, 2014
1 parent ab355e6 commit 6f00886
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions lib/ovs-thread.c
Expand Up @@ -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();
}

Expand All @@ -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();
}
}
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/ovs-thread.h
Expand Up @@ -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;
};

Expand Down

0 comments on commit 6f00886

Please sign in to comment.