2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
13 #include <urcu/futex.h>
15 #include <common/common.h>
20 * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
21 * "nto1" added to all function signature.
23 * Please see wait_gp()/update_counter_and_wait() calls in urcu.c in the urcu
24 * git tree for a detail example of this scheme being used. futex_async() is
25 * the urcu wrapper over the futex() sycall.
27 * There is also a formal verification available in the git tree.
29 * branch: formal-model
30 * commit id: 2a8044f3493046fcc8c67016902dc7beec6f026a
32 * Ref: git://git.lttng.org/userspace-rcu.git
36 * Update futex according to active or not. This scheme is used to wake every
37 * libust waiting on the shared memory map futex hence the INT_MAX used in the
38 * futex() call. If active, we set the value and wake everyone else we indicate
39 * that we are gone (cleanup() case).
42 void futex_wait_update(int32_t *futex
, int active
)
45 uatomic_set(futex
, 1);
46 if (futex_async(futex
, FUTEX_WAKE
,
47 INT_MAX
, NULL
, NULL
, 0) < 0) {
48 PERROR("futex_async");
52 uatomic_set(futex
, 0);
55 DBG("Futex wait update active %d", active
);
62 void futex_nto1_prepare(int32_t *futex
)
64 uatomic_set(futex
, -1);
67 DBG("Futex n to 1 prepare done");
74 void futex_nto1_wait(int32_t *futex
)
78 if (uatomic_read(futex
) != -1)
80 while (futex_async(futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
83 /* Value already changed. */
86 /* Retry if interrupted by signal. */
87 break; /* Get out of switch. */
89 /* Unexpected error. */
90 PERROR("futex_async");
95 DBG("Futex n to 1 wait done");
102 void futex_nto1_wake(int32_t *futex
)
104 if (caa_unlikely(uatomic_read(futex
) != -1))
106 uatomic_set(futex
, 0);
107 if (futex_async(futex
, FUTEX_WAKE
, 1, NULL
, NULL
, 0) < 0) {
108 PERROR("futex_async");
112 DBG("Futex n to 1 wake done");
This page took 0.032362 seconds and 4 git commands to generate.