4 * Userspace RCU library - RCU Queue with Wait-Free Enqueue/Blocking Dequeue
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 * RCU queue with wait-free enqueue/blocking dequeue using reference counting.
32 * Enqueue and dequeue operations hold a RCU read lock to deal with cmpxchg ABA
33 * problem. This implementation keeps a dummy head node to ensure we can always
34 * update the queue locklessly. Given that this is a queue, the dummy head node
35 * must always advance as we dequeue entries. Therefore, we keep a reference
36 * count on each entry we are dequeueing, so they can be kept as dummy head node
37 * until the next dequeue, at which point their reference count will be
41 #define URCU_WFQ_PERMANENT_REF 128
44 struct rcu_wfq_node
*next
;
48 struct rcu_wfq_queue
{
49 struct rcu_wfq_node
*head
, *tail
;
50 struct rcu_wfq_node init
; /* Dummy initialization node */
53 void rcu_wfq_node_init(struct rcu_wfq_node
*node
)
56 urcu_ref_init(&node
->ref
);
59 void rcu_wfq_init(struct rcu_wfq_queue
*q
)
61 rcu_wfq_node_init(&q
->init
);
62 /* Make sure the initial node is never freed. */
63 urcu_ref_set(&q
->init
.ref
, URCU_WFQ_PERMANENT_REF
);
65 q
->head
= q
->tail
= &q
->init
;
68 void rcu_wfq_enqueue(struct rcu_wfq_queue
*q
, struct rcu_wfq_node
*node
)
70 struct rcu_wfq_node
*old_tail
;
72 urcu_ref_get(&node
->ref
);
74 * uatomic_xchg() implicit memory barrier orders earlier stores to node
75 * (setting it to NULL and incrementing the refcount) before
78 old_tail
= uatomic_xchg(&q
->tail
, node
);
80 * At this point, dequeuers see a NULL old_tail->next, which indicates
81 * end of queue. The following store will append "node" to the queue
82 * from a dequeuer perspective.
84 STORE_SHARED(old_tail
->next
, node
);
88 * The entry returned by dequeue must be taken care of by doing a urcu_ref_put,
89 * which calls the release primitive when the reference count drops to zero. A
90 * grace period must be waited before performing the actual memory reclamation
91 * in the release primitive. The wfq node returned by dequeue must not be
92 * modified/re-used/freed until the reference count reaches zero and a grace
93 * period has elapsed (after the refcount reached 0).
95 * TODO: implement adaptative busy-wait and wait/wakeup scheme rather than busy
96 * loops. Better for UP.
99 rcu_wfq_dequeue_blocking(struct rcu_wfq_queue
*q
,
100 void (*release
)(struct urcu_ref
*))
103 struct rcu_wfq_node
*head
, *next
;
106 head
= rcu_dereference(q
->head
);
107 next
= rcu_dereference(head
->next
);
109 if (uatomic_cmpxchg(&q
->head
, head
, next
) == head
) {
111 urcu_ref_put(&head
->ref
, release
);
114 /* Concurrently pushed, retry */
This page took 0.036896 seconds and 5 git commands to generate.