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