urcu-wait: move wait code into separate file
[urcu.git] / urcu-wait.h
... / ...
CommitLineData
1#ifndef _URCU_WAIT_H
2#define _URCU_WAIT_H
3
4/*
5 * urcu-wait.h
6 *
7 * Userspace RCU library wait/wakeup management
8 *
9 * Copyright (c) 2012 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
26#include <urcu/uatomic.h>
27
28/*
29 * Number of busy-loop attempts before waiting on futex for grace period
30 * batching.
31 */
32#define URCU_WAIT_ATTEMPTS 1000
33
34enum urcu_wait_state {
35 /* URCU_WAIT_WAITING is compared directly (futex compares it). */
36 URCU_WAIT_WAITING = 0,
37 /* non-zero are used as masks. */
38 URCU_WAIT_WAKEUP = (1 << 0),
39 URCU_WAIT_AWAKENED = (1 << 1),
40 URCU_WAIT_TEARDOWN = (1 << 2),
41};
42
43struct urcu_wait {
44 int32_t futex;
45};
46
47static inline
48void urcu_wait_init(struct urcu_wait *wait)
49{
50 wait->futex = URCU_WAIT_WAITING;
51}
52
53/*
54 * Note: urcu_adaptative_wake_up needs "value" to stay allocated
55 * throughout its execution. In this scheme, the waiter owns the futex
56 * memory, and we only allow it to free this memory when it receives the
57 * URCU_WAIT_TEARDOWN flag.
58 */
59static inline
60void urcu_adaptative_wake_up(struct urcu_wait *wait)
61{
62 cmm_smp_mb();
63 assert(uatomic_read(&wait->futex) == URCU_WAIT_WAITING);
64 uatomic_set(&wait->futex, URCU_WAIT_WAKEUP);
65 if (!(uatomic_read(&wait->futex) & URCU_WAIT_AWAKENED))
66 futex_noasync(&wait->futex, FUTEX_WAKE, 1, NULL, NULL, 0);
67 /* Allow teardown of struct urcu_wait memory. */
68 uatomic_or(&wait->futex, URCU_WAIT_TEARDOWN);
69}
70
71/*
72 * Caller must initialize "value" to URCU_WAIT_WAITING before passing its
73 * memory to waker thread.
74 */
75static void urcu_adaptative_busy_wait(struct urcu_wait *wait)
76{
77 unsigned int i;
78
79 /* Load and test condition before read futex */
80 cmm_smp_rmb();
81 for (i = 0; i < URCU_WAIT_ATTEMPTS; i++) {
82 if (uatomic_read(&wait->futex) != URCU_WAIT_WAITING)
83 goto skip_futex_wait;
84 caa_cpu_relax();
85 }
86 futex_noasync(&wait->futex, FUTEX_WAIT,
87 URCU_WAIT_WAITING, NULL, NULL, 0);
88skip_futex_wait:
89
90 /* Tell waker thread than we are awakened. */
91 uatomic_or(&wait->futex, URCU_WAIT_AWAKENED);
92
93 /*
94 * Wait until waker thread lets us know it's ok to tear down
95 * memory allocated for struct urcu_wait.
96 */
97 for (i = 0; i < URCU_WAIT_ATTEMPTS; i++) {
98 if (uatomic_read(&wait->futex) & URCU_WAIT_TEARDOWN)
99 break;
100 caa_cpu_relax();
101 }
102 while (!(uatomic_read(&wait->futex) & URCU_WAIT_TEARDOWN))
103 poll(NULL, 0, 10);
104 assert(uatomic_read(&wait->futex) & URCU_WAIT_TEARDOWN);
105}
106
107#endif /* _URCU_WAIT_H */
This page took 0.022667 seconds and 4 git commands to generate.