| 1 | #ifndef _URCU_RCULFSTACK_H |
| 2 | #define _URCU_RCULFSTACK_H |
| 3 | |
| 4 | /* |
| 5 | * rculfstack.h |
| 6 | * |
| 7 | * Userspace RCU library - Lock-Free RCU Stack |
| 8 | * |
| 9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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 |
| 24 | */ |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE)) |
| 31 | #error "Dynamic loader LGPL wrappers not implemented yet" |
| 32 | #endif |
| 33 | |
| 34 | struct rcu_lfs_node { |
| 35 | struct rcu_lfs_node *next; |
| 36 | }; |
| 37 | |
| 38 | struct rcu_lfs_stack { |
| 39 | struct rcu_lfs_node *head; |
| 40 | }; |
| 41 | |
| 42 | void rcu_lfs_node_init(struct rcu_lfs_node *node) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void rcu_lfs_init(struct rcu_lfs_stack *s) |
| 47 | { |
| 48 | s->head = NULL; |
| 49 | } |
| 50 | |
| 51 | void rcu_lfs_push(struct rcu_lfs_stack *s, struct rcu_lfs_node *node) |
| 52 | { |
| 53 | for (;;) { |
| 54 | struct rcu_lfs_node *head; |
| 55 | |
| 56 | rcu_read_lock(); |
| 57 | head = rcu_dereference(s->head); |
| 58 | node->next = head; |
| 59 | /* |
| 60 | * uatomic_cmpxchg() implicit memory barrier orders earlier |
| 61 | * stores to node before publication. |
| 62 | */ |
| 63 | if (uatomic_cmpxchg(&s->head, head, node) == head) { |
| 64 | rcu_read_unlock(); |
| 65 | return; |
| 66 | } else { |
| 67 | /* Failure to prepend. Retry. */ |
| 68 | rcu_read_unlock(); |
| 69 | continue; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * The caller must wait for a grace period to pass before freeing the returned |
| 76 | * node. |
| 77 | * Returns NULL if stack is empty. |
| 78 | */ |
| 79 | struct rcu_lfs_node * |
| 80 | rcu_lfs_pop(struct rcu_lfs_stack *s) |
| 81 | { |
| 82 | for (;;) { |
| 83 | struct rcu_lfs_node *head; |
| 84 | |
| 85 | rcu_read_lock(); |
| 86 | head = rcu_dereference(s->head); |
| 87 | if (head) { |
| 88 | struct rcu_lfs_node *next = rcu_dereference(head->next); |
| 89 | |
| 90 | if (uatomic_cmpxchg(&s->head, head, next) == head) { |
| 91 | rcu_read_unlock(); |
| 92 | return head; |
| 93 | } else { |
| 94 | /* Concurrent modification. Retry. */ |
| 95 | rcu_read_unlock(); |
| 96 | continue; |
| 97 | } |
| 98 | } else { |
| 99 | /* Empty stack */ |
| 100 | rcu_read_unlock(); |
| 101 | return NULL; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #ifdef __cplusplus |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | #endif /* _URCU_RCULFSTACK_H */ |