1 #ifndef _URCU_RCULFQUEUE_STATIC_H
2 #define _URCU_RCULFQUEUE_STATIC_H
7 * Userspace RCU library - Lock-Free RCU Queue
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See rculfqueue.h for linking
12 * dynamically with the userspace rcu library.
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <urcu/urcu_ref.h>
30 #include <urcu/uatomic_arch.h>
32 /* A urcu implementation header should be already included. */
39 * Lock-free RCU queue using reference counting. Enqueue and dequeue operations
40 * hold a RCU read lock to deal with cmpxchg ABA problem. This implementation
41 * keeps a dummy head node to ensure we can always update the queue locklessly.
42 * Given that this is a queue, the dummy head node must always advance as we
43 * dequeue entries. Therefore, we keep a reference count on each entry we are
44 * dequeueing, so they can be kept as dummy head node until the next dequeue, at
45 * which point their reference count will be decremented.
48 #define URCU_LFQ_PERMANENT_REF 128
50 void _cds_lfq_node_init_rcu(struct cds_lfq_node_rcu
*node
)
53 urcu_ref_init(&node
->ref
);
56 void _cds_lfq_init_rcu(struct cds_lfq_queue_rcu
*q
)
58 _cds_lfq_node_init_rcu(&q
->init
);
59 /* Make sure the initial node is never freed. */
60 urcu_ref_set(&q
->init
.ref
, URCU_LFQ_PERMANENT_REF
);
61 q
->head
= q
->tail
= &q
->init
;
64 void _cds_lfq_enqueue_rcu(struct cds_lfq_queue_rcu
*q
, struct cds_lfq_node_rcu
*node
)
66 urcu_ref_get(&node
->ref
);
69 * uatomic_cmpxchg() implicit memory barrier orders earlier stores to
70 * node before publication.
74 struct cds_lfq_node_rcu
*tail
, *next
;
77 tail
= rcu_dereference(q
->tail
);
79 * Typically expect tail->next to be NULL.
81 next
= uatomic_cmpxchg(&tail
->next
, NULL
, node
);
84 * Tail was at the end of queue, we successfully
86 * Now move tail (another enqueue might beat
87 * us to it, that's fine).
89 (void) uatomic_cmpxchg(&q
->tail
, tail
, node
);
94 * Failure to append to current tail. Help moving tail
97 (void) uatomic_cmpxchg(&q
->tail
, tail
, next
);
105 * The entry returned by dequeue must be taken care of by doing a urcu_ref_put,
106 * which calls the release primitive when the reference count drops to zero. A
107 * grace period must be waited after execution of the release callback before
108 * performing the actual memory reclamation or modifying the cds_lfq_node_rcu
110 * In other words, the entry lfq node returned by dequeue must not be
111 * modified/re-used/freed until the reference count reaches zero and a grace
112 * period has elapsed (after the refcount reached 0).
114 struct cds_lfq_node_rcu
*
115 _cds_lfq_dequeue_rcu(struct cds_lfq_queue_rcu
*q
, void (*release
)(struct urcu_ref
*))
118 struct cds_lfq_node_rcu
*head
, *next
;
121 head
= rcu_dereference(q
->head
);
122 next
= rcu_dereference(head
->next
);
124 if (uatomic_cmpxchg(&q
->head
, head
, next
) == head
) {
126 urcu_ref_put(&head
->ref
, release
);
129 /* Concurrently pushed, retry */
145 #endif /* _URCU_RCULFQUEUE_STATIC_H */
This page took 0.032362 seconds and 4 git commands to generate.