13f26cca99fde2ba59acb01397969d2930fd7ce2
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>
29 * Number of busy-loop attempts before waiting on futex for grace period
32 #define URCU_WAIT_ATTEMPTS 1000
34 enum 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),
48 void urcu_wait_init(struct urcu_wait
*wait
)
50 wait
->futex
= URCU_WAIT_WAITING
;
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.
60 void urcu_adaptative_wake_up(struct urcu_wait
*wait
)
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
);
72 * Caller must initialize "value" to URCU_WAIT_WAITING before passing its
73 * memory to waker thread.
75 static void urcu_adaptative_busy_wait(struct urcu_wait
*wait
)
79 /* Load and test condition before read futex */
81 for (i
= 0; i
< URCU_WAIT_ATTEMPTS
; i
++) {
82 if (uatomic_read(&wait
->futex
) != URCU_WAIT_WAITING
)
86 futex_noasync(&wait
->futex
, FUTEX_WAIT
,
87 URCU_WAIT_WAITING
, NULL
, NULL
, 0);
90 /* Tell waker thread than we are awakened. */
91 uatomic_or(&wait
->futex
, URCU_WAIT_AWAKENED
);
94 * Wait until waker thread lets us know it's ok to tear down
95 * memory allocated for struct urcu_wait.
97 for (i
= 0; i
< URCU_WAIT_ATTEMPTS
; i
++) {
98 if (uatomic_read(&wait
->futex
) & URCU_WAIT_TEARDOWN
)
102 while (!(uatomic_read(&wait
->futex
) & URCU_WAIT_TEARDOWN
))
104 assert(uatomic_read(&wait
->futex
) & URCU_WAIT_TEARDOWN
);
107 #endif /* _URCU_WAIT_H */
This page took 0.031856 seconds and 4 git commands to generate.