4 * Userspace RCU library - RCU Stack with Wait-Free push, Blocking pop.
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
26 #error "Dynamic loader LGPL wrappers not implemented yet"
29 #define RCU_WF_STACK_END ((void *)0x1UL)
32 struct rcu_wfs_node
*next
;
35 struct rcu_wfs_stack
{
36 struct rcu_wfs_node
*head
;
39 void rcu_wfs_node_init(struct rcu_wfs_node
*node
)
44 void rcu_wfs_init(struct rcu_wfs_stack
*s
)
46 s
->head
= RCU_WF_STACK_END
;
49 void rcu_wfs_push(struct rcu_wfs_stack
*s
, struct rcu_wfs_node
*node
)
51 struct rcu_wfs_node
*old_head
;
53 assert(node
->next
== NULL
);
55 * uatomic_xchg() implicit memory barrier orders earlier stores to node
56 * (setting it to NULL) before publication.
58 old_head
= uatomic_xchg(&s
->head
, node
);
60 * At this point, dequeuers see a NULL node->next, they should busy-wait
61 * until node->next is set to old_head.
63 STORE_SHARED(node
->next
, old_head
);
67 * The caller must wait for a grace period before:
68 * - freeing the returned node.
69 * - modifying the ->next pointer of the returned node. (be careful with unions)
70 * - passing the returned node back to push() on the same stack they got it
73 * Returns NULL if stack is empty.
75 * cmpxchg is protected from ABA races by holding a RCU read lock between
76 * s->head read and cmpxchg modifying s->head and requiring that dequeuers wait
77 * for a grace period before freeing the returned node.
79 * TODO: implement adaptative busy-wait and wait/wakeup scheme rather than busy
80 * loops. Better for UP.
83 rcu_wfs_pop(struct rcu_wfs_stack
*s
)
86 struct rcu_wfs_node
*head
;
89 head
= rcu_dereference(s
->head
);
90 if (head
!= RCU_WF_STACK_END
) {
91 struct rcu_wfs_node
*next
= rcu_dereference(head
->next
);
93 /* Retry while head is being set by push(). */
98 if (uatomic_cmpxchg(&s
->head
, head
, next
) == head
) {
102 /* Concurrent modification. Retry. */
This page took 0.03308 seconds and 5 git commands to generate.