ifdef C, wait-free stack adaptative pop wait
[urcu.git] / urcu / rcuwfstack.h
CommitLineData
c3f74cb2
MD
1#ifndef _URCU_RCUWFSTACK_H
2#define _URCU_RCUWFSTACK_H
3
a5eb5ffc
MD
4/*
5 * rcuwfstack.h
6 *
7 * Userspace RCU library - RCU Stack with Wait-Free push, Blocking pop.
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
4a2f213c
MD
26#include <assert.h>
27
0e2e9380
MD
28#ifdef __cplusplus
29extern "C" {
30#endif
31
a5eb5ffc
MD
32#if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
33#error "Dynamic loader LGPL wrappers not implemented yet"
34#endif
35
7ab587e2 36#define RCU_WF_STACK_END ((void *)0x1UL)
0e2e9380
MD
37#define RCU_WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */
38#define RCU_WFS_WAIT 10 /* Wait 10 ms if being set */
39
40extern int rcu_wfs_futex;
4a2f213c 41
a5eb5ffc
MD
42struct rcu_wfs_node {
43 struct rcu_wfs_node *next;
44};
45
46struct rcu_wfs_stack {
47 struct rcu_wfs_node *head;
a5eb5ffc
MD
48};
49
50void rcu_wfs_node_init(struct rcu_wfs_node *node)
51{
52 node->next = NULL;
53}
54
55void rcu_wfs_init(struct rcu_wfs_stack *s)
56{
4a2f213c 57 s->head = RCU_WF_STACK_END;
a5eb5ffc
MD
58}
59
60void rcu_wfs_push(struct rcu_wfs_stack *s, struct rcu_wfs_node *node)
61{
62 struct rcu_wfs_node *old_head;
63
4a2f213c 64 assert(node->next == NULL);
a5eb5ffc
MD
65 /*
66 * uatomic_xchg() implicit memory barrier orders earlier stores to node
67 * (setting it to NULL) before publication.
68 */
69 old_head = uatomic_xchg(&s->head, node);
70 /*
71 * At this point, dequeuers see a NULL node->next, they should busy-wait
72 * until node->next is set to old_head.
73 */
74 STORE_SHARED(node->next, old_head);
75}
76
77/*
4a2f213c
MD
78 * The caller must wait for a grace period before:
79 * - freeing the returned node.
80 * - modifying the ->next pointer of the returned node. (be careful with unions)
81 * - passing the returned node back to push() on the same stack they got it
82 * from.
83 *
a5eb5ffc
MD
84 * Returns NULL if stack is empty.
85 *
86 * cmpxchg is protected from ABA races by holding a RCU read lock between
87 * s->head read and cmpxchg modifying s->head and requiring that dequeuers wait
88 * for a grace period before freeing the returned node.
a5eb5ffc
MD
89 */
90struct rcu_wfs_node *
7ab587e2 91rcu_wfs_pop_blocking(struct rcu_wfs_stack *s)
a5eb5ffc 92{
0e2e9380
MD
93 int attempt = 0;
94
a5eb5ffc 95 for (;;) {
4a2f213c 96 struct rcu_wfs_node *head;
a5eb5ffc 97
4a2f213c
MD
98 rcu_read_lock();
99 head = rcu_dereference(s->head);
100 if (head != RCU_WF_STACK_END) {
a5eb5ffc
MD
101 struct rcu_wfs_node *next = rcu_dereference(head->next);
102
103 /* Retry while head is being set by push(). */
4a2f213c
MD
104 if (!next) {
105 rcu_read_unlock();
0e2e9380
MD
106 if (++attempt >= RCU_WFS_ADAPT_ATTEMPTS) {
107 /* Sleep for 10ms */
108 poll(NULL, 0, RCU_WFS_WAIT);
109 attempt = 0;
110 }
a5eb5ffc 111 continue;
4a2f213c 112 }
a5eb5ffc
MD
113 if (uatomic_cmpxchg(&s->head, head, next) == head) {
114 rcu_read_unlock();
115 return head;
116 } else {
117 /* Concurrent modification. Retry. */
4a2f213c 118 rcu_read_unlock();
a5eb5ffc
MD
119 continue;
120 }
121 } else {
122 /* Empty stack */
123 rcu_read_unlock();
124 return NULL;
125 }
126 }
127}
c3f74cb2 128
0e2e9380
MD
129#ifdef __cplusplus
130}
131#endif
132
c3f74cb2 133#endif /* _URCU_RCUWFSTACK_H */
This page took 0.027021 seconds and 4 git commands to generate.