RCU lock-free stack: don't hold read lock across retry
[urcu.git] / urcu / rcuwfstack.h
CommitLineData
a5eb5ffc
MD
1/*
2 * rcuwfstack.h
3 *
4 * Userspace RCU library - RCU Stack with Wait-Free push, Blocking pop.
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
4a2f213c
MD
23#include <assert.h>
24
a5eb5ffc
MD
25#if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
26#error "Dynamic loader LGPL wrappers not implemented yet"
27#endif
28
4a2f213c
MD
29#define RCU_WF_STACK_END ((void *)0x1UL)
30
a5eb5ffc
MD
31struct rcu_wfs_node {
32 struct rcu_wfs_node *next;
33};
34
35struct rcu_wfs_stack {
36 struct rcu_wfs_node *head;
a5eb5ffc
MD
37};
38
39void rcu_wfs_node_init(struct rcu_wfs_node *node)
40{
41 node->next = NULL;
42}
43
44void rcu_wfs_init(struct rcu_wfs_stack *s)
45{
4a2f213c 46 s->head = RCU_WF_STACK_END;
a5eb5ffc
MD
47}
48
49void rcu_wfs_push(struct rcu_wfs_stack *s, struct rcu_wfs_node *node)
50{
51 struct rcu_wfs_node *old_head;
52
4a2f213c 53 assert(node->next == NULL);
a5eb5ffc
MD
54 /*
55 * uatomic_xchg() implicit memory barrier orders earlier stores to node
56 * (setting it to NULL) before publication.
57 */
58 old_head = uatomic_xchg(&s->head, node);
59 /*
60 * At this point, dequeuers see a NULL node->next, they should busy-wait
61 * until node->next is set to old_head.
62 */
63 STORE_SHARED(node->next, old_head);
64}
65
66/*
4a2f213c
MD
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
71 * from.
72 *
a5eb5ffc
MD
73 * Returns NULL if stack is empty.
74 *
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.
78 *
79 * TODO: implement adaptative busy-wait and wait/wakeup scheme rather than busy
80 * loops. Better for UP.
81 */
82struct rcu_wfs_node *
83rcu_wfs_pop(struct rcu_wfs_stack *s)
84{
a5eb5ffc 85 for (;;) {
4a2f213c 86 struct rcu_wfs_node *head;
a5eb5ffc 87
4a2f213c
MD
88 rcu_read_lock();
89 head = rcu_dereference(s->head);
90 if (head != RCU_WF_STACK_END) {
a5eb5ffc
MD
91 struct rcu_wfs_node *next = rcu_dereference(head->next);
92
93 /* Retry while head is being set by push(). */
4a2f213c
MD
94 if (!next) {
95 rcu_read_unlock();
a5eb5ffc 96 continue;
4a2f213c 97 }
a5eb5ffc
MD
98 if (uatomic_cmpxchg(&s->head, head, next) == head) {
99 rcu_read_unlock();
100 return head;
101 } else {
102 /* Concurrent modification. Retry. */
4a2f213c 103 rcu_read_unlock();
a5eb5ffc
MD
104 continue;
105 }
106 } else {
107 /* Empty stack */
108 rcu_read_unlock();
109 return NULL;
110 }
111 }
112}
This page took 0.026385 seconds and 4 git commands to generate.