1 #ifndef _URCU_WAITQUEUE_LIFO_H
2 #define _URCU_WAITQUEUE_LIFO_H
5 * urcu/waitqueue-lifo.h
7 * Userspace RCU library - wait queue scheme with LIFO semantic
9 * Copyright (c) 2012-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
26 #include <urcu/uatomic.h>
27 #include <urcu/wfstack.h>
28 #include <urcu/futex.h>
31 * Number of busy-loop attempts before waiting on futex for grace period
34 #define URCU_WAIT_ATTEMPTS 1000
36 enum urcu_wait_state
{
37 /* URCU_WAIT_WAITING is compared directly (futex compares it). */
38 URCU_WAIT_WAITING
= 0,
39 /* non-zero are used as masks. */
40 URCU_WAIT_WAKEUP
= (1 << 0),
41 URCU_WAIT_RUNNING
= (1 << 1),
42 URCU_WAIT_TEARDOWN
= (1 << 2),
45 struct urcu_wait_node
{
46 struct cds_wfs_node node
;
47 int32_t state
; /* enum urcu_wait_state */
50 #define URCU_WAIT_NODE_INIT(name, _state) \
53 #define DEFINE_URCU_WAIT_NODE(name, state) \
54 struct urcu_wait_node name = URCU_WAIT_NODE_INIT(name, state)
56 #define DECLARE_URCU_WAIT_NODE(name) \
57 struct urcu_wait_node name
59 struct urcu_wait_queue
{
60 struct __cds_wfs_stack stack
;
63 #define URCU_WAIT_QUEUE_HEAD_INIT(name) \
64 { .stack.head = CDS_WFS_END, }
66 #define DECLARE_URCU_WAIT_QUEUE(name) \
67 struct urcu_wait_queue name
69 #define DEFINE_URCU_WAIT_QUEUE(name) \
70 struct urcu_wait_queue name = URCU_WAIT_QUEUE_HEAD_INIT(name)
73 void urcu_wait_queue_init(struct urcu_wait_queue
*queue
)
75 __cds_wfs_init(&queue
->stack
);
79 struct cds_wfs_head
*head
;
83 * Add ourself atomically to a wait queue. Return 0 if queue was
84 * previously empty, else return 1.
85 * A full memory barrier is issued before being added to the wait queue.
88 bool urcu_wait_add(struct urcu_wait_queue
*queue
,
89 struct urcu_wait_node
*node
)
91 return cds_wfs_push(&queue
->stack
, &node
->node
);
95 * Atomically move all waiters from wait queue into our local struct
99 void urcu_move_waiters(struct urcu_waiters
*waiters
,
100 struct urcu_wait_queue
*queue
)
102 waiters
->head
= __cds_wfs_pop_all(&queue
->stack
);
106 void urcu_wait_set_state(struct urcu_wait_node
*node
,
107 enum urcu_wait_state state
)
113 void urcu_wait_or_state(struct urcu_wait_node
*node
,
114 enum urcu_wait_state state
)
116 uatomic_or(&node
->state
, state
);
120 void urcu_wait_node_init(struct urcu_wait_node
*node
,
121 enum urcu_wait_state state
)
123 urcu_wait_set_state(node
, state
);
124 cds_wfs_node_init(&node
->node
);
128 * Note: urcu_adaptative_wake_up needs "value" to stay allocated
129 * throughout its execution. In this scheme, the waiter owns the node
130 * memory, and we only allow it to free this memory when it receives the
131 * URCU_WAIT_TEARDOWN flag.
134 void urcu_adaptative_wake_up(struct urcu_wait_node
*wait
)
138 * "or" of WAKEUP flag rather than "set" is useful for multiple
139 * concurrent wakeup sources. Note that "WAIT_TEARDOWN" becomes
140 * useless when we use multiple wakeup sources: lifetime of the
141 * "value" should then be handled by the caller.
143 uatomic_or(&wait
->state
, URCU_WAIT_WAKEUP
);
144 if (!(uatomic_read(&wait
->state
) & URCU_WAIT_RUNNING
))
145 futex_noasync(&wait
->state
, FUTEX_WAKE
, 1, NULL
, NULL
, 0);
146 /* Allow teardown of struct urcu_wait memory. */
147 uatomic_or(&wait
->state
, URCU_WAIT_TEARDOWN
);
151 * Caller must initialize "value" to URCU_WAIT_WAITING before passing its
152 * memory to waker thread.
155 void urcu_adaptative_busy_wait(struct urcu_wait_node
*wait
)
159 /* Load and test condition before read state */
161 for (i
= 0; i
< URCU_WAIT_ATTEMPTS
; i
++) {
162 if (uatomic_read(&wait
->state
) != URCU_WAIT_WAITING
)
163 goto skip_futex_wait
;
166 futex_noasync(&wait
->state
, FUTEX_WAIT
,
167 URCU_WAIT_WAITING
, NULL
, NULL
, 0);
170 /* Tell waker thread than we are running. */
171 uatomic_or(&wait
->state
, URCU_WAIT_RUNNING
);
174 * Wait until waker thread lets us know it's ok to tear down
175 * memory allocated for struct urcu_wait.
177 for (i
= 0; i
< URCU_WAIT_ATTEMPTS
; i
++) {
178 if (uatomic_read(&wait
->state
) & URCU_WAIT_TEARDOWN
)
182 while (!(uatomic_read(&wait
->state
) & URCU_WAIT_TEARDOWN
))
184 assert(uatomic_read(&wait
->state
) & URCU_WAIT_TEARDOWN
);
188 * Need mutual exclusion against other wakeup and move waiters
189 * operations. It is provided by the caller.
192 int urcu_dequeue_wake_single(struct urcu_wait_queue
*queue
)
194 struct cds_wfs_node
*node
;
195 struct urcu_wait_node
*wait_node
;
198 node
= __cds_wfs_pop_blocking(&queue
->stack
);
201 wait_node
= caa_container_of(node
, struct urcu_wait_node
, node
);
202 CMM_STORE_SHARED(wait_node
->node
.next
, NULL
);
203 /* Don't wake already running threads */
204 if (!(wait_node
->state
& URCU_WAIT_RUNNING
)) {
205 urcu_adaptative_wake_up(wait_node
);
212 * Need mutual exclusion against other wakeup and move waiters
213 * operations. It is provided by the caller.
216 int urcu_dequeue_wake_n(struct urcu_wait_queue
*queue
, int n
)
223 ret
= urcu_dequeue_wake_single(queue
);
235 int urcu_wake_all_waiters(struct urcu_waiters
*waiters
)
237 struct cds_wfs_node
*iter
, *iter_n
;
240 /* Wake all waiters in our stack head */
241 cds_wfs_for_each_blocking_safe(waiters
->head
, iter
, iter_n
) {
242 struct urcu_wait_node
*wait_node
=
243 caa_container_of(iter
, struct urcu_wait_node
, node
);
245 CMM_STORE_SHARED(wait_node
->node
.next
, NULL
);
246 /* Don't wake already running threads */
247 if (wait_node
->state
& URCU_WAIT_RUNNING
)
249 urcu_adaptative_wake_up(wait_node
);
255 #endif /* _URCU_WAITQUEUE_LIFO_H */
This page took 0.036572 seconds and 5 git commands to generate.