From ab179a1705ee5d662c6a7964645077980096e134 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 17 Sep 2009 08:38:06 -0400 Subject: [PATCH] qsbr: micro optimization of the gp use. Follow-up on 1a80f3: since now `0' for a gp counter means "offline", just be sure that the global GP counter is never zero. To achieve that, let its first value ever be one, and increment it 2 by 2 like previously done. Note that it internaly uses the fact that signed integers wraps (which the previous code already assumed anyways) but this is undefined behaviour in C. Using unsigned longs would probably be more portable. This way, setting the cpu online, marking a quiescent state is just a matter of copying the global gp counter instead of incrementing it by one. This saves one CPU instruction, and supposedly even register use for architectures providing memory to memory copy operands. Signed-off-by: Pierre Habouzit Signed-off-by: Mathieu Desnoyers --- urcu-qsbr-static.h | 6 +++--- urcu-qsbr.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/urcu-qsbr-static.h b/urcu-qsbr-static.h index b18affe..8d8aa3f 100644 --- a/urcu-qsbr-static.h +++ b/urcu-qsbr-static.h @@ -176,7 +176,7 @@ static inline int rcu_gp_ongoing(long *value) if (value == NULL) return 0; reader_gp = LOAD_SHARED(*value); - return (reader_gp & 1) && (reader_gp - urcu_gp_ctr < 0); + return reader_gp && (reader_gp - urcu_gp_ctr < 0); } static inline void _rcu_read_lock(void) @@ -191,7 +191,7 @@ static inline void _rcu_read_unlock(void) static inline void _rcu_quiescent_state(void) { smp_mb(); - _STORE_SHARED(rcu_reader_qs_gp, _LOAD_SHARED(urcu_gp_ctr) + 1); + _STORE_SHARED(rcu_reader_qs_gp, _LOAD_SHARED(urcu_gp_ctr)); smp_mb(); } @@ -203,7 +203,7 @@ static inline void _rcu_thread_offline(void) static inline void _rcu_thread_online(void) { - _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr) + 1); + _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr)); smp_mb(); } diff --git a/urcu-qsbr.c b/urcu-qsbr.c index 3f21be3..1955277 100644 --- a/urcu-qsbr.c +++ b/urcu-qsbr.c @@ -42,7 +42,7 @@ pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; /* * Global grace period counter. */ -long urcu_gp_ctr = 0; +long urcu_gp_ctr = 1; /* * Written to only by each individual reader. Read by both the reader and the @@ -139,9 +139,9 @@ static void wait_for_quiescent_state(void) void synchronize_rcu(void) { - int was_online; + long was_online; - was_online = rcu_reader_qs_gp & 1; + was_online = rcu_reader_qs_gp; /* * Mark the writer thread offline to make sure we don't wait for @@ -158,7 +158,7 @@ void synchronize_rcu(void) internal_urcu_unlock(); if (was_online) - _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr) + 1); + _STORE_SHARED(rcu_reader_qs_gp, LOAD_SHARED(urcu_gp_ctr)); smp_mb(); } -- 2.34.1