Add test_cmpxchg to benchmark cmpxchg vs non-atomic ops
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 3 Sep 2011 15:59:53 +0000 (11:59 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 3 Sep 2011 15:59:53 +0000 (11:59 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
rculfhash.c

index 1720afb61669e971968fc2cfcd994db3e6b7b8be..e418b2ab8c7aca51ec9ec5be48e92dbc92175a53 100644 (file)
 #include <stdio.h>
 #include <pthread.h>
 
+#ifndef CDS_HT_NOATOMIC
+# define test_cmpxchg(ptr, old, _new)  uatomic_cmpxchg(ptr, old, _new)
+#else
+# define test_cmpxchg(ptr, old, _new)          \
+({                                             \
+       __typeof__(*(ptr)) __readptr = *(ptr);  \
+       (__readptr == (old)) ? ({               \
+               *(ptr) = (_new);                \
+               (old);                          \
+       }) : __readptr;                         \
+})
+#endif
+
 #ifdef DEBUG
 #define dbg_printf(fmt, args...)     printf("[debug rculfhash] " fmt, ## args)
 #else
@@ -405,7 +418,7 @@ unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
                old2 = old1;
                if (old2 >= v)
                        return old2;
-       } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
+       } while ((old1 = test_cmpxchg(ptr, old2, v)) != old2);
        return v;
 }
 
@@ -438,7 +451,7 @@ void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node
                        new_next = flag_dummy(clear_flag(next));
                else
                        new_next = clear_flag(next);
-               (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
+               (void) test_cmpxchg(&iter_prev->p.next, iter, new_next);
        }
 }
 
@@ -504,7 +517,7 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
                        new_node = flag_dummy(node);
                else
                        new_node = node;
-               if (uatomic_cmpxchg(&iter_prev->p.next, iter,
+               if (test_cmpxchg(&iter_prev->p.next, iter,
                                    new_node) != iter)
                        continue;       /* retry */
                else
@@ -515,7 +528,7 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
                        new_next = flag_dummy(clear_flag(next));
                else
                        new_next = clear_flag(next);
-               (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
+               (void) test_cmpxchg(&iter_prev->p.next, iter, new_next);
                /* retry */
        }
 gc_end:
@@ -544,7 +557,7 @@ int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t,
                if (unlikely(is_removed(next)))
                        goto end;
                assert(!is_dummy(next));
-               old = uatomic_cmpxchg(&node->p.next, next,
+               old = test_cmpxchg(&node->p.next, next,
                                      flag_removed(next));
        } while (old != next);
 
This page took 0.02715 seconds and 4 git commands to generate.