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 | ||
bd0662db | 46 | static inline |
16aa9ee8 | 47 | void _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, struct cds_lfs_node_rcu *node) |
b6a37eac | 48 | { |
16aa9ee8 | 49 | struct cds_lfs_node_rcu *head = NULL; |
75202035 | 50 | |
b6a37eac | 51 | for (;;) { |
16aa9ee8 | 52 | struct cds_lfs_node_rcu *old_head = head; |
b6a37eac | 53 | |
b6a37eac MD |
54 | node->next = head; |
55 | /* | |
56 | * uatomic_cmpxchg() implicit memory barrier orders earlier | |
57 | * stores to node before publication. | |
58 | */ | |
75202035 MD |
59 | head = uatomic_cmpxchg(&s->head, old_head, node); |
60 | if (old_head == head) | |
61 | break; | |
b6a37eac MD |
62 | } |
63 | } | |
64 | ||
65 | /* | |
d9b52143 MD |
66 | * Should be called under rcu read-side lock. |
67 | * | |
b6a37eac | 68 | * The caller must wait for a grace period to pass before freeing the returned |
16aa9ee8 | 69 | * node or modifying the cds_lfs_node_rcu structure. |
b6a37eac MD |
70 | * Returns NULL if stack is empty. |
71 | */ | |
bd0662db | 72 | static inline |
16aa9ee8 DG |
73 | struct cds_lfs_node_rcu * |
74 | _cds_lfs_pop_rcu(struct cds_lfs_stack_rcu *s) | |
b6a37eac MD |
75 | { |
76 | for (;;) { | |
16aa9ee8 | 77 | struct cds_lfs_node_rcu *head; |
b6a37eac | 78 | |
4fc06b7f MD |
79 | head = rcu_dereference(s->head); |
80 | if (head) { | |
16aa9ee8 | 81 | struct cds_lfs_node_rcu *next = rcu_dereference(head->next); |
b6a37eac | 82 | |
4fc06b7f | 83 | if (uatomic_cmpxchg(&s->head, head, next) == head) { |
4fc06b7f MD |
84 | return head; |
85 | } else { | |
86 | /* Concurrent modification. Retry. */ | |
4fc06b7f MD |
87 | continue; |
88 | } | |
89 | } else { | |
90 | /* Empty stack */ | |
4fc06b7f MD |
91 | return NULL; |
92 | } | |
b6a37eac MD |
93 | } |
94 | } | |
95 | ||
96 | #ifdef __cplusplus | |
97 | } | |
98 | #endif | |
99 | ||
100 | #endif /* _URCU_RCULFSTACK_STATIC_H */ |