From f0f7dbdd9ba9bff3860d8aea63a3e01b158351de Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 17 Sep 2009 11:11:42 -0400 Subject: [PATCH] qsbr: portability fixes: use unsigned long for the gp counters. unsigned wraps properly in C (modulo ULONG_MAX + 1), IOW ULONG_MAX + 2 _is_ defined and is 1. Rewrite rcu_gp_ongoing test to work in the unsigned case: a - b < 0 becomes a - b > ULONG_MAX / 2 This test actually means a - b > 0x7f....f which is exactly what we want to test for in the first place, but in a portable way. Signed-off-by: Pierre Habouzit Signed-off-by: Mathieu Desnoyers --- urcu-qsbr-static.h | 11 ++++++----- urcu-qsbr.c | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/urcu-qsbr-static.h b/urcu-qsbr-static.h index 8d8aa3f..3d2ec8f 100644 --- a/urcu-qsbr-static.h +++ b/urcu-qsbr-static.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -165,18 +166,18 @@ static inline void reader_barrier() * Using a int rather than a char to eliminate false register dependencies * causing stalls on some architectures. */ -extern long urcu_gp_ctr; +extern unsigned long urcu_gp_ctr; -extern long __thread rcu_reader_qs_gp; +extern unsigned long __thread rcu_reader_qs_gp; -static inline int rcu_gp_ongoing(long *value) +static inline int rcu_gp_ongoing(unsigned long *value) { - long reader_gp; + unsigned long reader_gp; if (value == NULL) return 0; reader_gp = LOAD_SHARED(*value); - return reader_gp && (reader_gp - urcu_gp_ctr < 0); + return reader_gp && (reader_gp - urcu_gp_ctr > ULONG_MAX / 2); } static inline void _rcu_read_lock(void) diff --git a/urcu-qsbr.c b/urcu-qsbr.c index 1955277..c0e643d 100644 --- a/urcu-qsbr.c +++ b/urcu-qsbr.c @@ -42,20 +42,20 @@ pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; /* * Global grace period counter. */ -long urcu_gp_ctr = 1; +unsigned long urcu_gp_ctr = 1; /* * Written to only by each individual reader. Read by both the reader and the * writers. */ -long __thread rcu_reader_qs_gp; +unsigned long __thread rcu_reader_qs_gp; /* Thread IDs of registered readers */ #define INIT_NUM_THREADS 4 struct reader_registry { pthread_t tid; - long *rcu_reader_qs_gp; + unsigned long *rcu_reader_qs_gp; }; #ifdef DEBUG_YIELD @@ -139,7 +139,7 @@ static void wait_for_quiescent_state(void) void synchronize_rcu(void) { - long was_online; + unsigned long was_online; was_online = rcu_reader_qs_gp; -- 2.34.1