2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <urcu/futex.h>
25 #include <common/common.h>
30 * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
31 * "nto1" added to all function signature.
33 * The code is adapted from the adaptative busy wait/wake_up scheme used in
37 /* Number of busy-loop attempts before waiting on futex. */
38 #define FUTEX_WAIT_ATTEMPTS 1000
40 enum futex_wait_state
{
41 /* FUTEX_WAIT_WAITING is compared directly (futex() compares it). */
42 FUTEX_WAIT_WAITING
= 0,
43 /* non-zero are used as masks. */
44 FUTEX_WAIT_WAKEUP
= (1 << 0),
45 FUTEX_WAIT_RUNNING
= (1 << 1),
46 FUTEX_WAIT_TEARDOWN
= (1 << 2),
50 * Update futex according to active or not. This scheme is used to wake every
51 * libust waiting on the shared memory map futex hence the INT_MAX used in the
52 * futex() call. If active, we set the value and wake everyone else we indicate
53 * that we are gone (cleanup() case).
56 void futex_wait_update(int32_t *futex
, int active
)
59 uatomic_set(futex
, 1);
60 if (futex_async(futex
, FUTEX_WAKE
,
61 INT_MAX
, NULL
, NULL
, 0) < 0) {
62 PERROR("futex_async");
66 uatomic_set(futex
, 0);
69 DBG("Futex wait update active %d", active
);
76 void futex_nto1_prepare(int32_t *futex
)
78 uatomic_set(futex
, FUTEX_WAIT_WAITING
);
81 DBG("Futex n to 1 prepare done");
88 void futex_nto1_wait(int32_t *futex
)
92 /* Load and test condition before read state */
94 for (i
= 0; i
< FUTEX_WAIT_ATTEMPTS
; i
++) {
95 if (uatomic_read(futex
) != FUTEX_WAIT_WAITING
)
99 while (futex_noasync(futex
, FUTEX_WAIT
, FUTEX_WAIT_WAITING
,
103 /* Value already changed. */
104 goto skip_futex_wait
;
106 /* Retry if interrupted by signal. */
107 break; /* Get out of switch. */
109 /* Unexpected error. */
116 /* Tell waker thread than we are running. */
117 uatomic_or(futex
, FUTEX_WAIT_RUNNING
);
120 * Wait until waker thread lets us know it's ok to tear down
121 * memory allocated for the futex.
123 for (i
= 0; i
< FUTEX_WAIT_ATTEMPTS
; i
++) {
124 if (uatomic_read(futex
) & FUTEX_WAIT_TEARDOWN
)
128 while (!(uatomic_read(futex
) & FUTEX_WAIT_TEARDOWN
))
130 assert(uatomic_read(futex
) & FUTEX_WAIT_TEARDOWN
);
131 DBG("Futex n to 1 wait done");
138 void futex_nto1_wake(int32_t *futex
)
141 uatomic_set(futex
, FUTEX_WAIT_WAKEUP
);
142 if (!(uatomic_read(futex
) & FUTEX_WAIT_RUNNING
)) {
143 if (futex_noasync(futex
, FUTEX_WAKE
, 1,
144 NULL
, NULL
, 0) < 0) {
145 PERROR("futex_noasync");
149 /* Allow teardown of futex. */
150 uatomic_or(futex
, FUTEX_WAIT_TEARDOWN
);
151 DBG("Futex n to 1 wake done");
This page took 0.035846 seconds and 5 git commands to generate.