Commit | Line | Data |
---|---|---|
b6a37eac MD |
1 | #ifndef _URCU_RCULFSTACK_STATIC_H |
2 | #define _URCU_RCULFSTACK_STATIC_H | |
3 | ||
4 | /* | |
5 | * rculfstack-static.h | |
6 | * | |
7 | * Userspace RCU library - Lock-Free RCU Stack | |
8 | * | |
9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
10 | * | |
11 | * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See rculfstack.h for linking | |
12 | * dynamically with the userspace rcu library. | |
13 | * | |
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. | |
18 | * | |
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. | |
23 | * | |
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 | |
27 | */ | |
28 | ||
a2e7bf9c | 29 | #include <urcu/uatomic.h> |
b6a37eac MD |
30 | |
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
bd0662db | 35 | static inline |
16aa9ee8 | 36 | void _cds_lfs_node_init_rcu(struct cds_lfs_node_rcu *node) |
b6a37eac MD |
37 | { |
38 | } | |
39 | ||
bd0662db | 40 | static inline |
16aa9ee8 | 41 | void _cds_lfs_init_rcu(struct cds_lfs_stack_rcu *s) |
b6a37eac MD |
42 | { |
43 | s->head = NULL; | |
44 | } | |
45 | ||
79dba879 MD |
46 | /* |
47 | * Lock-free stack push is not subject to ABA problem, so no need to | |
48 | * take the RCU read-side lock. Even if "head" changes between two | |
49 | * uatomic_cmpxchg() invocations here (being popped, and then pushed | |
50 | * again by one or more concurrent threads), the second | |
51 | * uatomic_cmpxchg() invocation only cares about pushing a new entry at | |
52 | * the head of the stack, ensuring consistency by making sure the new | |
53 | * node->next is the same pointer value as the value replaced as head. | |
54 | * It does not care about the content of the actual next node, so it can | |
55 | * very well be reallocated between the two uatomic_cmpxchg(). | |
56 | * | |
57 | * We take the approach of expecting the stack to be usually empty, so | |
58 | * we first try an initial uatomic_cmpxchg() on a NULL old_head, and | |
59 | * retry if the old head was non-NULL (the value read by the first | |
60 | * uatomic_cmpxchg() is used as old head for the following loop). The | |
61 | * upside of this scheme is to minimize the amount of cacheline traffic, | |
62 | * always performing an exclusive cacheline access, rather than doing | |
63 | * non-exclusive followed by exclusive cacheline access (which would be | |
64 | * required if we first read the old head value). This design decision | |
65 | * might be revisited after more throrough benchmarking on various | |
66 | * platforms. | |
67 | */ | |
bd0662db | 68 | static inline |
16aa9ee8 | 69 | void _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, struct cds_lfs_node_rcu *node) |
b6a37eac | 70 | { |
16aa9ee8 | 71 | struct cds_lfs_node_rcu *head = NULL; |
75202035 | 72 | |
b6a37eac | 73 | for (;;) { |
16aa9ee8 | 74 | struct cds_lfs_node_rcu *old_head = head; |
b6a37eac | 75 | |
b6a37eac MD |
76 | node->next = head; |
77 | /* | |
78 | * uatomic_cmpxchg() implicit memory barrier orders earlier | |
79 | * stores to node before publication. | |
80 | */ | |
75202035 MD |
81 | head = uatomic_cmpxchg(&s->head, old_head, node); |
82 | if (old_head == head) | |
83 | break; | |
b6a37eac MD |
84 | } |
85 | } | |
86 | ||
87 | /* | |
7618919a | 88 | * Acts as a RCU reader. |
d9b52143 | 89 | * |
b6a37eac | 90 | * The caller must wait for a grace period to pass before freeing the returned |
16aa9ee8 | 91 | * node or modifying the cds_lfs_node_rcu structure. |
b6a37eac MD |
92 | * Returns NULL if stack is empty. |
93 | */ | |
bd0662db | 94 | static inline |
16aa9ee8 DG |
95 | struct cds_lfs_node_rcu * |
96 | _cds_lfs_pop_rcu(struct cds_lfs_stack_rcu *s) | |
b6a37eac MD |
97 | { |
98 | for (;;) { | |
16aa9ee8 | 99 | struct cds_lfs_node_rcu *head; |
b6a37eac | 100 | |
7618919a | 101 | rcu_read_lock(); |
4fc06b7f MD |
102 | head = rcu_dereference(s->head); |
103 | if (head) { | |
16aa9ee8 | 104 | struct cds_lfs_node_rcu *next = rcu_dereference(head->next); |
b6a37eac | 105 | |
4fc06b7f | 106 | if (uatomic_cmpxchg(&s->head, head, next) == head) { |
7618919a | 107 | rcu_read_unlock(); |
4fc06b7f MD |
108 | return head; |
109 | } else { | |
110 | /* Concurrent modification. Retry. */ | |
7618919a | 111 | rcu_read_unlock(); |
4fc06b7f MD |
112 | continue; |
113 | } | |
114 | } else { | |
115 | /* Empty stack */ | |
7618919a | 116 | rcu_read_unlock(); |
4fc06b7f MD |
117 | return NULL; |
118 | } | |
b6a37eac MD |
119 | } |
120 | } | |
121 | ||
122 | #ifdef __cplusplus | |
123 | } | |
124 | #endif | |
125 | ||
126 | #endif /* _URCU_RCULFSTACK_STATIC_H */ |