1 #ifndef _URCU_RCUWFQUEUE_H
2 #define _URCU_RCUWFQUEUE_H
7 * Userspace RCU library - RCU Queue with Wait-Free Enqueue/Blocking Dequeue
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 * RCU queue with wait-free enqueue/blocking dequeue using reference counting.
39 * Enqueue and dequeue operations hold a RCU read lock to deal with cmpxchg ABA
40 * problem. This implementation keeps a dummy head node to ensure we can always
41 * update the queue locklessly. Given that this is a queue, the dummy head node
42 * must always advance as we dequeue entries. Therefore, we keep a reference
43 * count on each entry we are dequeueing, so they can be kept as dummy head node
44 * until the next dequeue, at which point their reference count will be
48 #define URCU_WFQ_PERMANENT_REF 128
51 struct rcu_wfq_node
*next
;
55 struct rcu_wfq_queue
{
56 struct rcu_wfq_node
*head
, *tail
;
57 struct rcu_wfq_node init
; /* Dummy initialization node */
60 void rcu_wfq_node_init(struct rcu_wfq_node
*node
)
63 urcu_ref_init(&node
->ref
);
66 void rcu_wfq_init(struct rcu_wfq_queue
*q
)
68 rcu_wfq_node_init(&q
->init
);
69 /* Make sure the initial node is never freed. */
70 urcu_ref_set(&q
->init
.ref
, URCU_WFQ_PERMANENT_REF
);
72 q
->head
= q
->tail
= &q
->init
;
75 void rcu_wfq_enqueue(struct rcu_wfq_queue
*q
, struct rcu_wfq_node
*node
)
77 struct rcu_wfq_node
*old_tail
;
79 urcu_ref_get(&node
->ref
);
81 * uatomic_xchg() implicit memory barrier orders earlier stores to node
82 * (setting it to NULL and incrementing the refcount) before
85 old_tail
= uatomic_xchg(&q
->tail
, node
);
87 * At this point, dequeuers see a NULL old_tail->next, which indicates
88 * end of queue. The following store will append "node" to the queue
89 * from a dequeuer perspective.
91 STORE_SHARED(old_tail
->next
, node
);
95 * The entry returned by dequeue must be taken care of by doing a urcu_ref_put,
96 * which calls the release primitive when the reference count drops to zero. A
97 * grace period must be waited before performing the actual memory reclamation
98 * in the release primitive. The wfq node returned by dequeue must not be
99 * modified/re-used/freed until the reference count reaches zero and a grace
100 * period has elapsed (after the refcount reached 0).
102 * No need to go on a waitqueue here, as there is no possible state in which the
103 * list could cause dequeue to busy-loop needlessly while waiting for another
104 * thread to be scheduled. The queue appears empty until tail->next is set by
107 struct rcu_wfq_node
*
108 rcu_wfq_dequeue_blocking(struct rcu_wfq_queue
*q
,
109 void (*release
)(struct urcu_ref
*))
112 struct rcu_wfq_node
*head
, *next
;
115 head
= rcu_dereference(q
->head
);
116 next
= rcu_dereference(head
->next
);
118 if (uatomic_cmpxchg(&q
->head
, head
, next
) == head
) {
120 urcu_ref_put(&head
->ref
, release
);
123 /* Concurrently pushed, retry */
139 #endif /* _URCU_RCUWFQUEUE_H */
This page took 0.034233 seconds and 5 git commands to generate.