01c2092610350155247b87cadd6149aa3b26b4b1
1 #ifndef _URCU_RCULFQUEUE_H
2 #define _URCU_RCULFQUEUE_H
7 * Userspace RCU library - Lock-Free RCU Queue
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <urcu/urcu_ref.h>
33 #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
34 #error "Dynamic loader LGPL wrappers not implemented yet"
38 * Lock-free RCU queue using reference counting. Enqueue and dequeue operations
39 * hold a RCU read lock to deal with cmpxchg ABA problem. This implementation
40 * keeps a dummy head node to ensure we can always update the queue locklessly.
41 * Given that this is a queue, the dummy head node must always advance as we
42 * dequeue entries. Therefore, we keep a reference count on each entry we are
43 * dequeueing, so they can be kept as dummy head node until the next dequeue, at
44 * which point their reference count will be decremented.
47 #define URCU_LFQ_PERMANENT_REF 128
50 struct rcu_lfq_node
*next
;
54 struct rcu_lfq_queue
{
55 struct rcu_lfq_node
*head
, *tail
;
56 struct rcu_lfq_node init
; /* Dummy initialization node */
59 void rcu_lfq_node_init(struct rcu_lfq_node
*node
)
62 urcu_ref_init(&node
->ref
);
65 void rcu_lfq_init(struct rcu_lfq_queue
*q
)
67 rcu_lfq_node_init(&q
->init
);
68 /* Make sure the initial node is never freed. */
69 urcu_ref_set(&q
->init
.ref
, URCU_LFQ_PERMANENT_REF
);
70 q
->head
= q
->tail
= &q
->init
;
73 void rcu_lfq_enqueue(struct rcu_lfq_queue
*q
, struct rcu_lfq_node
*node
)
75 urcu_ref_get(&node
->ref
);
78 * uatomic_cmpxchg() implicit memory barrier orders earlier stores to
79 * node before publication.
83 struct rcu_lfq_node
*tail
, *next
;
86 tail
= rcu_dereference(q
->tail
);
88 * Typically expect tail->next to be NULL.
90 next
= uatomic_cmpxchg(&tail
->next
, NULL
, node
);
93 * Tail was at the end of queue, we successfully
95 * Now move tail (another enqueue might beat
96 * us to it, that's fine).
98 uatomic_cmpxchg(&q
->tail
, tail
, node
);
103 * Failure to append to current tail. Help moving tail
106 uatomic_cmpxchg(&q
->tail
, tail
, next
);
114 * The entry returned by dequeue must be taken care of by doing a urcu_ref_put,
115 * which calls the release primitive when the reference count drops to zero. A
116 * grace period must be waited before performing the actual memory reclamation
117 * in the release primitive.
118 * The entry lfq node returned by dequeue must not be modified/re-used/freed
119 * until the reference count reaches zero and a grace period has elapsed (after
120 * the refcount reached 0).
122 struct rcu_lfq_node
*
123 rcu_lfq_dequeue(struct rcu_lfq_queue
*q
, void (*release
)(struct urcu_ref
*))
126 struct rcu_lfq_node
*head
, *next
;
129 head
= rcu_dereference(q
->head
);
130 next
= rcu_dereference(head
->next
);
132 if (uatomic_cmpxchg(&q
->head
, head
, next
) == head
) {
134 urcu_ref_put(&head
->ref
, release
);
137 /* Concurrently pushed, retry */
153 #endif /* _URCU_RCULFQUEUE_H */
This page took 0.063216 seconds and 4 git commands to generate.