7 * Userspace RCU library wait/wakeup management
9 * Copyright (c) 2012 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>
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) \
66 .head = CDS_WFS_END, \
67 .lock = PTHREAD_MUTEX_INITIALIZER, \
71 #define DECLARE_URCU_WAIT_QUEUE(name) \
72 struct urcu_wait_queue name
74 #define DEFINE_URCU_WAIT_QUEUE(name) \
75 struct urcu_wait_queue name = URCU_WAIT_QUEUE_HEAD_INIT(name)
78 struct cds_wfs_head
*head
;
82 * Add ourself atomically to a wait queue. Return 0 if queue was
83 * previously empty, else return 1.
84 * A full memory barrier is issued before being added to the wait queue.
87 bool urcu_wait_add(struct urcu_wait_queue
*queue
,
88 struct urcu_wait_node
*node
)
90 return cds_wfs_push(&queue
->stack
, &node
->node
);
94 * Atomically move all waiters from wait queue into our local struct
98 void urcu_move_waiters(struct urcu_waiters
*waiters
,
99 struct urcu_wait_queue
*queue
)
101 waiters
->head
= __cds_wfs_pop_all(&queue
->stack
);
105 void urcu_wait_set_state(struct urcu_wait_node
*node
,
106 enum urcu_wait_state state
)
112 void urcu_wait_node_init(struct urcu_wait_node
*node
,
113 enum urcu_wait_state state
)
115 urcu_wait_set_state(node
, state
);
116 cds_wfs_node_init(&node
->node
);
120 * Note: urcu_adaptative_wake_up needs "value" to stay allocated
121 * throughout its execution. In this scheme, the waiter owns the node
122 * memory, and we only allow it to free this memory when it receives the
123 * URCU_WAIT_TEARDOWN flag.
126 void urcu_adaptative_wake_up(struct urcu_wait_node
*wait
)
129 assert(uatomic_read(&wait
->state
) == URCU_WAIT_WAITING
);
130 uatomic_set(&wait
->state
, URCU_WAIT_WAKEUP
);
131 if (!(uatomic_read(&wait
->state
) & URCU_WAIT_RUNNING
)) {
132 if (futex_noasync(&wait
->state
, FUTEX_WAKE
, 1,
136 /* Allow teardown of struct urcu_wait memory. */
137 uatomic_or(&wait
->state
, URCU_WAIT_TEARDOWN
);
141 * Caller must initialize "value" to URCU_WAIT_WAITING before passing its
142 * memory to waker thread.
145 void urcu_adaptative_busy_wait(struct urcu_wait_node
*wait
)
149 /* Load and test condition before read state */
151 for (i
= 0; i
< URCU_WAIT_ATTEMPTS
; i
++) {
152 if (uatomic_read(&wait
->state
) != URCU_WAIT_WAITING
)
153 goto skip_futex_wait
;
156 while (futex_noasync(&wait
->state
, FUTEX_WAIT
, URCU_WAIT_WAITING
,
160 /* Value already changed. */
161 goto skip_futex_wait
;
163 /* Retry if interrupted by signal. */
164 break; /* Get out of switch. */
166 /* Unexpected error. */
172 /* Tell waker thread than we are running. */
173 uatomic_or(&wait
->state
, URCU_WAIT_RUNNING
);
176 * Wait until waker thread lets us know it's ok to tear down
177 * memory allocated for struct urcu_wait.
179 for (i
= 0; i
< URCU_WAIT_ATTEMPTS
; i
++) {
180 if (uatomic_read(&wait
->state
) & URCU_WAIT_TEARDOWN
)
184 while (!(uatomic_read(&wait
->state
) & URCU_WAIT_TEARDOWN
))
186 assert(uatomic_read(&wait
->state
) & URCU_WAIT_TEARDOWN
);
190 void urcu_wake_all_waiters(struct urcu_waiters
*waiters
)
192 struct cds_wfs_node
*iter
, *iter_n
;
194 /* Wake all waiters in our stack head */
195 cds_wfs_for_each_blocking_safe(waiters
->head
, iter
, iter_n
) {
196 struct urcu_wait_node
*wait_node
=
197 caa_container_of(iter
, struct urcu_wait_node
, node
);
199 /* Don't wake already running threads */
200 if (wait_node
->state
& URCU_WAIT_RUNNING
)
202 urcu_adaptative_wake_up(wait_node
);
206 #endif /* _URCU_WAIT_H */
This page took 0.038387 seconds and 4 git commands to generate.