rculfstack: LGPL-ize
[urcu.git] / urcu / rculfstack-static.h
CommitLineData
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
29#include <urcu/uatomic_arch.h>
30/* A urcu implementation header should be already included. */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36void _rcu_lfs_node_init(struct rcu_lfs_node *node)
37{
38}
39
40void _rcu_lfs_init(struct rcu_lfs_stack *s)
41{
42 s->head = NULL;
43}
44
45void _rcu_lfs_push(struct rcu_lfs_stack *s, struct rcu_lfs_node *node)
46{
47 for (;;) {
48 struct rcu_lfs_node *head;
49
50 rcu_read_lock();
51 head = rcu_dereference(s->head);
52 node->next = head;
53 /*
54 * uatomic_cmpxchg() implicit memory barrier orders earlier
55 * stores to node before publication.
56 */
57 if (uatomic_cmpxchg(&s->head, head, node) == head) {
58 rcu_read_unlock();
59 return;
60 } else {
61 /* Failure to prepend. Retry. */
62 rcu_read_unlock();
63 continue;
64 }
65 }
66}
67
68/*
69 * The caller must wait for a grace period to pass before freeing the returned
70 * node or modifying the rcu_lfs_node structure.
71 * Returns NULL if stack is empty.
72 */
73struct rcu_lfs_node *
74_rcu_lfs_pop(struct rcu_lfs_stack *s)
75{
76 for (;;) {
77 struct rcu_lfs_node *head;
78
79 rcu_read_lock();
80 head = rcu_dereference(s->head);
81 if (head) {
82 struct rcu_lfs_node *next = rcu_dereference(head->next);
83
84 if (uatomic_cmpxchg(&s->head, head, next) == head) {
85 rcu_read_unlock();
86 return head;
87 } else {
88 /* Concurrent modification. Retry. */
89 rcu_read_unlock();
90 continue;
91 }
92 } else {
93 /* Empty stack */
94 rcu_read_unlock();
95 return NULL;
96 }
97 }
98}
99
100#ifdef __cplusplus
101}
102#endif
103
104#endif /* _URCU_RCULFSTACK_STATIC_H */
This page took 0.0251 seconds and 4 git commands to generate.