Fix: lfstack reversed empty/non-empty return value
[urcu.git] / urcu / static / lfstack.h
index fe9e8abe1fe266eeae3d5a3a8fd91187fb8b8d17..4799bc4dc0f05e33a0a343cb53e9e8c9efee1836 100644 (file)
@@ -36,6 +36,8 @@
 extern "C" {
 #endif
 
+#define CDS_LFS_END                    NULL
+
 /*
  * Lock-free stack.
  *
@@ -73,15 +75,24 @@ void _cds_lfs_init(struct cds_lfs_stack *s)
 {
         int ret;
 
-       s->head = NULL;
+       s->head = CDS_LFS_END;
        ret = pthread_mutex_init(&s->lock, NULL);
        assert(!ret);
 }
 
+/*
+ * ___cds_lfs_init: initialize lock-free stack.
+ */
+static inline
+void ___cds_lfs_init(struct __cds_lfs_stack *s)
+{
+       s->head = CDS_LFS_END;
+}
+
 static inline
 bool ___cds_lfs_empty_head(struct cds_lfs_head *head)
 {
-       return head == NULL;
+       return head == CDS_LFS_END;
 }
 
 /*
@@ -90,9 +101,9 @@ bool ___cds_lfs_empty_head(struct cds_lfs_head *head)
  * No memory barrier is issued. No mutual exclusion is required.
  */
 static inline
-bool _cds_lfs_empty(struct cds_lfs_stack *s)
+bool _cds_lfs_empty(cds_lfs_stack_ptr_t s)
 {
-       return ___cds_lfs_empty_head(CMM_LOAD_SHARED(s->head));
+       return ___cds_lfs_empty_head(CMM_LOAD_SHARED(s._s->head));
 }
 
 /*
@@ -118,17 +129,18 @@ bool _cds_lfs_empty(struct cds_lfs_stack *s)
  * always performing an exclusive cacheline access, rather than doing
  * non-exclusive followed by exclusive cacheline access (which would be
  * required if we first read the old head value). This design decision
- * might be revisited after more throrough benchmarking on various
+ * might be revisited after more thorough benchmarking on various
  * platforms.
  *
  * Returns 0 if the stack was empty prior to adding the node.
  * Returns non-zero otherwise.
  */
 static inline
-bool _cds_lfs_push(struct cds_lfs_stack *s,
+bool _cds_lfs_push(cds_lfs_stack_ptr_t u_s,
                  struct cds_lfs_node *node)
 {
-       struct cds_lfs_head *head = NULL;
+       struct __cds_lfs_stack *s = u_s._s;
+       struct cds_lfs_head *head = CDS_LFS_END;
        struct cds_lfs_head *new_head =
                caa_container_of(node, struct cds_lfs_head, node);
 
@@ -148,7 +160,7 @@ bool _cds_lfs_push(struct cds_lfs_stack *s,
                if (old_head == head)
                        break;
        }
-       return ___cds_lfs_empty_head(head);
+       return !___cds_lfs_empty_head(head);
 }
 
 /*
@@ -168,8 +180,10 @@ bool _cds_lfs_push(struct cds_lfs_stack *s,
  *    __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
  */
 static inline
-struct cds_lfs_node *___cds_lfs_pop(struct cds_lfs_stack *s)
+struct cds_lfs_node *___cds_lfs_pop(cds_lfs_stack_ptr_t u_s)
 {
+       struct __cds_lfs_stack *s = u_s._s;
+
        for (;;) {
                struct cds_lfs_head *head, *next_head;
                struct cds_lfs_node *next;
@@ -211,8 +225,10 @@ struct cds_lfs_node *___cds_lfs_pop(struct cds_lfs_stack *s)
  *    __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
  */
 static inline
-struct cds_lfs_head *___cds_lfs_pop_all(struct cds_lfs_stack *s)
+struct cds_lfs_head *___cds_lfs_pop_all(cds_lfs_stack_ptr_t u_s)
 {
+       struct __cds_lfs_stack *s = u_s._s;
+
        /*
         * Implicit memory barrier after uatomic_xchg() matches implicit
         * memory barrier before uatomic_cmpxchg() in cds_lfs_push. It
@@ -223,7 +239,7 @@ struct cds_lfs_head *___cds_lfs_pop_all(struct cds_lfs_stack *s)
         * taking care to order writes to each node prior to the full
         * memory barrier after this uatomic_xchg().
         */
-       return uatomic_xchg(&s->head, NULL);
+       return uatomic_xchg(&s->head, CDS_LFS_END);
 }
 
 /*
This page took 0.023885 seconds and 4 git commands to generate.