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
23 #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
24 #error "Dynamic loader LGPL wrappers not implemented yet"
28 struct rcu_wfs_node
*next
;
31 struct rcu_wfs_stack
{
32 struct rcu_wfs_node
*head
;
33 struct rcu_wfs_node end
;
36 void rcu_wfs_node_init(struct rcu_wfs_node
*node
)
41 void rcu_wfs_init(struct rcu_wfs_stack
*s
)
44 rcu_wfs_node_init(&s
->end
);
47 void rcu_wfs_push(struct rcu_wfs_stack
*s
, struct rcu_wfs_node
*node
)
49 struct rcu_wfs_node
*old_head
;
52 * uatomic_xchg() implicit memory barrier orders earlier stores to node
53 * (setting it to NULL) before publication.
55 old_head
= uatomic_xchg(&s
->head
, node
);
57 * At this point, dequeuers see a NULL node->next, they should busy-wait
58 * until node->next is set to old_head.
60 STORE_SHARED(node
->next
, old_head
);
64 * The caller must wait for a grace period before freeing the returned node.
65 * Returns NULL if stack is empty.
67 * cmpxchg is protected from ABA races by holding a RCU read lock between
68 * s->head read and cmpxchg modifying s->head and requiring that dequeuers wait
69 * for a grace period before freeing the returned node.
71 * TODO: implement adaptative busy-wait and wait/wakeup scheme rather than busy
72 * loops. Better for UP.
75 rcu_wfs_pop(struct rcu_wfs_stack
*s
)
79 struct rcu_wfs_node
*head
= rcu_dereference(s
->head
);
81 if (head
!= &s
->end
) {
82 struct rcu_wfs_node
*next
= rcu_dereference(head
->next
);
84 /* Retry while head is being set by push(). */
88 if (uatomic_cmpxchg(&s
->head
, head
, next
) == head
) {
92 /* Concurrent modification. Retry. */
This page took 0.033812 seconds and 5 git commands to generate.