wfstack: LGPL-ize
[urcu.git] / urcu / wfstack.h
index b3ee2e7c4585e6e2f735a81ce82b67db3f84a5e2..6cb4971c929c63f02d231ac1fbd6e15550784299 100644 (file)
@@ -2,7 +2,7 @@
 #define _URCU_WFSTACK_H
 
 /*
- * rcuwfstack.h
+ * wfstack.h
  *
  * Userspace RCU library - Stack with Wait-Free push, Blocking pop.
  *
 extern "C" {
 #endif
 
-#if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
-#error "Dynamic loader LGPL wrappers not implemented yet"
-#endif
-
-#define WF_STACK_END                   ((void *)0x1UL)
-#define WFS_ADAPT_ATTEMPTS             10      /* Retry if being set */
-#define WFS_WAIT                       10      /* Wait 10 ms if being set */
-
 struct wfs_node {
        struct wfs_node *next;
 };
@@ -48,79 +40,26 @@ struct wfs_stack {
        pthread_mutex_t lock;
 };
 
-void wfs_node_init(struct wfs_node *node)
-{
-       node->next = NULL;
-}
+#ifdef _LGPL_SOURCE
 
-void wfs_init(struct wfs_stack *s)
-{
-       int ret;
+#include <urcu/wfstack-static.h>
 
-       s->head = WF_STACK_END;
-       ret = pthread_mutex_init(&s->lock, NULL);
-       assert(!ret);
-}
+#define wfs_node_init          _wfs_node_init
+#define wfs_init               _wfs_init
+#define wfs_push               _wfs_push
+#define __wfs_pop_blocking     ___wfs_pop_blocking
+#define wfs_pop_blocking       _wfs_pop_blocking
 
-void wfs_push(struct wfs_stack *s, struct wfs_node *node)
-{
-       struct wfs_node *old_head;
+#else /* !_LGPL_SOURCE */
 
-       assert(node->next == NULL);
-       /*
-        * uatomic_xchg() implicit memory barrier orders earlier stores to node
-        * (setting it to NULL) before publication.
-        */
-       old_head = uatomic_xchg(&s->head, node);
-       /*
-        * At this point, dequeuers see a NULL node->next, they should busy-wait
-        * until node->next is set to old_head.
-        */
-       STORE_SHARED(node->next, old_head);
-}
+extern void wfs_node_init(struct wfs_node *node);
+extern void wfs_init(struct wfs_stack *s);
+extern void wfs_push(struct wfs_stack *s, struct wfs_node *node);
+/* __wfs_pop_blocking: caller ensures mutual exclusion between pops */
+extern struct wfs_node *__wfs_pop_blocking(struct wfs_stack *s);
+extern struct wfs_node *wfs_pop_blocking(struct wfs_stack *s);
 
-/*
- * Returns NULL if stack is empty.
- */
-struct wfs_node *
-__wfs_pop_blocking(struct wfs_stack *s)
-{
-       struct wfs_node *head, *next;
-       int attempt = 0;
-
-retry:
-       head = LOAD_SHARED(s->head);
-       if (head == WF_STACK_END)
-               return NULL;
-       /*
-        * Adaptative busy-looping waiting for push to complete.
-        */
-       while ((next = LOAD_SHARED(head->next)) == NULL) {
-               if (++attempt >= WFS_ADAPT_ATTEMPTS) {
-                       poll(NULL, 0, WFS_WAIT);        /* Wait for 10ms */
-                       attempt = 0;
-               } else
-                       cpu_relax();
-       }
-       if (uatomic_cmpxchg(&s->head, head, next) == head)
-               return head;
-       else
-               goto retry;             /* Concurrent modification. Retry. */
-}
-
-struct wfs_node *
-wfs_pop_blocking(struct wfs_stack *s)
-{
-       struct wfs_node *retnode;
-       int ret;
-
-       ret = pthread_mutex_lock(&s->lock);
-       assert(!ret);
-       retnode = __wfs_pop_blocking(s);
-       ret = pthread_mutex_unlock(&s->lock);
-       assert(!ret);
-       return retnode;
-}
+#endif /* !_LGPL_SOURCE */
 
 #ifdef __cplusplus
 }
This page took 0.02375 seconds and 4 git commands to generate.