Rename all data structure with prefix cds_
[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
16aa9ee8 36void _cds_lfs_node_init_rcu(struct cds_lfs_node_rcu *node)
b6a37eac
MD
37{
38}
39
16aa9ee8 40void _cds_lfs_init_rcu(struct cds_lfs_stack_rcu *s)
b6a37eac
MD
41{
42 s->head = NULL;
43}
44
16aa9ee8 45void _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, struct cds_lfs_node_rcu *node)
b6a37eac 46{
16aa9ee8 47 struct cds_lfs_node_rcu *head = NULL;
75202035 48
b6a37eac 49 for (;;) {
16aa9ee8 50 struct cds_lfs_node_rcu *old_head = head;
b6a37eac 51
b6a37eac
MD
52 node->next = head;
53 /*
54 * uatomic_cmpxchg() implicit memory barrier orders earlier
55 * stores to node before publication.
56 */
75202035
MD
57 head = uatomic_cmpxchg(&s->head, old_head, node);
58 if (old_head == head)
59 break;
b6a37eac
MD
60 }
61}
62
63/*
64 * The caller must wait for a grace period to pass before freeing the returned
16aa9ee8 65 * node or modifying the cds_lfs_node_rcu structure.
b6a37eac
MD
66 * Returns NULL if stack is empty.
67 */
16aa9ee8
DG
68struct cds_lfs_node_rcu *
69_cds_lfs_pop_rcu(struct cds_lfs_stack_rcu *s)
b6a37eac
MD
70{
71 for (;;) {
16aa9ee8 72 struct cds_lfs_node_rcu *head;
b6a37eac 73
4fc06b7f
MD
74 rcu_read_lock();
75 head = rcu_dereference(s->head);
76 if (head) {
16aa9ee8 77 struct cds_lfs_node_rcu *next = rcu_dereference(head->next);
b6a37eac 78
4fc06b7f
MD
79 if (uatomic_cmpxchg(&s->head, head, next) == head) {
80 rcu_read_unlock();
81 return head;
82 } else {
83 /* Concurrent modification. Retry. */
84 rcu_read_unlock();
85 continue;
86 }
87 } else {
88 /* Empty stack */
89 rcu_read_unlock();
90 return NULL;
91 }
b6a37eac
MD
92 }
93}
94
95#ifdef __cplusplus
96}
97#endif
98
99#endif /* _URCU_RCULFSTACK_STATIC_H */
This page took 0.025826 seconds and 4 git commands to generate.