4 * Userspace RCU library - Lock-Free RCU Queue
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 #include <urcu/urcu_ref.h>
26 #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
27 #error "Dynamic loader LGPL wrappers not implemented yet"
31 * Lock-free RCU queue using reference counting. Enqueue and dequeue operations
32 * hold a RCU read lock to deal with cmpxchg ABA problem. This implementation
33 * keeps a dummy head node to ensure we can always update the queue locklessly.
34 * Given that this is a queue, the dummy head node must always advance as we
35 * dequeue entries. Therefore, we keep a reference count on each entry we are
36 * dequeueing, so they can be kept as dummy head node until the next dequeue, at
37 * which point their reference count will be decremented.
40 #define URCU_LFQ_PERMANENT_REF 128
43 struct rcu_lfq_node
*next
;
47 struct rcu_lfq_queue
{
48 struct rcu_lfq_node
*head
, *tail
;
49 struct rcu_lfq_node init
; /* Dummy initialization node */
52 void rcu_lfq_node_init(struct rcu_lfq_node
*node
)
55 urcu_ref_init(&node
->ref
);
58 void rcu_lfq_init(struct rcu_lfq_queue
*q
)
60 rcu_lfq_node_init(&q
->init
);
61 /* Make sure the initial node is never freed. */
62 urcu_ref_set(&q
->init
.ref
, URCU_LFQ_PERMANENT_REF
);
63 q
->head
= q
->tail
= &q
->init
;
66 void rcu_lfq_enqueue(struct rcu_lfq_queue
*q
, struct rcu_lfq_node
*node
)
68 urcu_ref_get(&node
->ref
);
71 * uatomic_cmpxchg() implicit memory barrier orders earlier stores to
72 * node before publication.
77 struct rcu_lfq_node
*tail
= rcu_dereference(q
->tail
);
78 struct rcu_lfq_node
*next
;
81 * Typically expect tail to be NULL.
83 next
= uatomic_cmpxchg(&tail
->next
, NULL
, node
);
86 * Tail was at the end of queue, we successfully
88 * Now move tail (another enqueue might beat
89 * us to it, that's fine).
91 uatomic_cmpxchg(&q
->tail
, tail
, node
);
96 * Failure to append to current tail. Help moving tail
99 uatomic_cmpxchg(&q
->tail
, tail
, next
);
106 * The entry returned by dequeue must be taken care of by doing a urcu_ref_put,
107 * which calls the release primitive when the reference count drops to zero. A
108 * grace period must be waited before performing the actual memory reclamation
109 * in the release primitive.
110 * The entry lfq node returned by dequeue must no be re-used before the
111 * reference count reaches zero.
113 struct rcu_lfq_node
*
114 rcu_lfq_dequeue(struct rcu_lfq_queue
*q
, void (*release
)(struct urcu_ref
*))
118 struct rcu_lfq_node
*head
= rcu_dereference(q
->head
);
119 struct rcu_lfq_node
*next
= rcu_dereference(head
->next
);
122 if (uatomic_cmpxchg(&q
->head
, head
, next
) == head
) {
124 urcu_ref_put(&head
->ref
, release
);
127 /* Concurrently pushed, retry */
This page took 0.037718 seconds and 4 git commands to generate.