1 #ifndef _URCU_WFSTACK_STATIC_H
2 #define _URCU_WFSTACK_STATIC_H
7 * Userspace RCU library - Stack with Wait-Free push, Blocking pop.
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See wfstack.h for linking
10 * dynamically with the userspace rcu library.
12 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <urcu/compiler.h>
32 #include <urcu/uatomic_arch.h>
38 #define WF_STACK_END ((void *)0x1UL)
39 #define WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */
40 #define WFS_WAIT 10 /* Wait 10 ms if being set */
42 void _wfs_node_init(struct wfs_node
*node
)
47 void _wfs_init(struct wfs_stack
*s
)
51 s
->head
= WF_STACK_END
;
52 ret
= pthread_mutex_init(&s
->lock
, NULL
);
56 void _wfs_push(struct wfs_stack
*s
, struct wfs_node
*node
)
58 struct wfs_node
*old_head
;
60 assert(node
->next
== NULL
);
62 * uatomic_xchg() implicit memory barrier orders earlier stores to node
63 * (setting it to NULL) before publication.
65 old_head
= uatomic_xchg(&s
->head
, node
);
67 * At this point, dequeuers see a NULL node->next, they should busy-wait
68 * until node->next is set to old_head.
70 STORE_SHARED(node
->next
, old_head
);
74 * Returns NULL if stack is empty.
77 ___wfs_pop_blocking(struct wfs_stack
*s
)
79 struct wfs_node
*head
, *next
;
83 head
= LOAD_SHARED(s
->head
);
84 if (head
== WF_STACK_END
)
87 * Adaptative busy-looping waiting for push to complete.
89 while ((next
= LOAD_SHARED(head
->next
)) == NULL
) {
90 if (++attempt
>= WFS_ADAPT_ATTEMPTS
) {
91 poll(NULL
, 0, WFS_WAIT
); /* Wait for 10ms */
96 if (uatomic_cmpxchg(&s
->head
, head
, next
) == head
)
99 goto retry
; /* Concurrent modification. Retry. */
103 _wfs_pop_blocking(struct wfs_stack
*s
)
105 struct wfs_node
*retnode
;
108 ret
= pthread_mutex_lock(&s
->lock
);
110 retnode
= ___wfs_pop_blocking(s
);
111 ret
= pthread_mutex_unlock(&s
->lock
);
120 #endif /* _URCU_WFSTACK_STATIC_H */
This page took 0.031038 seconds and 4 git commands to generate.