X-Git-Url: https://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=urcu%2Frculfstack.h;fp=urcu%2Frculfstack.h;h=f43c9d8c1e679c344bdb289b445fdd99ed0c466d;hp=0000000000000000000000000000000000000000;hb=453629a9317adef5b96c3d55e4dcd98db680997a;hpb=e90ad78457151a8faec9d77934b292f458082386 diff --git a/urcu/rculfstack.h b/urcu/rculfstack.h new file mode 100644 index 0000000..f43c9d8 --- /dev/null +++ b/urcu/rculfstack.h @@ -0,0 +1,88 @@ +/* + * rculfstack.h + * + * Userspace RCU library - Lock-Free RCU Stack + * + * Copyright 2010 - Mathieu Desnoyers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE)) +#error "Dynamic loader LGPL wrappers not implemented yet" +#endif + +struct rcu_lfs_node { + struct rcu_lfs_node *next; +}; + +struct rcu_lfs_stack { + struct rcu_lfs_node *head; +}; + +void rcu_lfs_node_init(struct rcu_lfs_node *node) +{ +} + +void rcu_lfs_init(struct rcu_lfs_stack *s) +{ + s->head = NULL; +} + +void rcu_lfs_push(struct rcu_lfs_stack *s, struct rcu_lfs_node *node) +{ + rcu_read_lock(); + for (;;) { + struct rcu_lfs_node *head = rcu_dereference(s->head); + + node->next = head; + /* + * uatomic_cmpxchg() implicit memory barrier orders earlier + * stores to node before publication. + */ + if (uatomic_cmpxchg(&s->head, head, node) == head) { + rcu_read_unlock(); + return; + } else { + /* Failure to prepend. Retry. */ + continue; + } + } +} + +struct rcu_lfs_node * +rcu_lfs_pop(struct rcu_lfs_stack *s) +{ + rcu_read_lock(); + for (;;) { + struct rcu_lfs_node *head = rcu_dereference(s->head); + + if (head) { + struct rcu_lfs_node *next = rcu_dereference(head->next); + + if (uatomic_cmpxchg(&s->head, head, next) == head) { + rcu_read_unlock(); + return head; + } else { + /* Concurrent modification. Retry. */ + continue; + } + } else { + /* Empty stack */ + rcu_read_unlock(); + return NULL; + } + } +}