Commit | Line | Data |
---|---|---|
099e26bd DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; only version 2 | |
8 | * of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
0fdd1e2c | 21 | #include <limits.h> |
099e26bd DG |
22 | #include <sys/syscall.h> |
23 | #include <unistd.h> | |
24 | #include <urcu.h> | |
25 | #include <urcu/futex.h> | |
26 | ||
db758600 | 27 | #include <common/error.h> |
099e26bd DG |
28 | |
29 | #include "futex.h" | |
30 | ||
31 | /* | |
32 | * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the | |
33 | * "nto1" added to all function signature. | |
34 | * | |
35 | * Please see wait_gp()/update_counter_and_wait() calls in urcu.c in the urcu | |
36 | * git tree for a detail example of this scheme being used. futex_async() is | |
37 | * the urcu wrapper over the futex() sycall. | |
38 | * | |
39 | * There is also a formal verification available in the git tree. | |
40 | * | |
41 | * branch: formal-model | |
42 | * commit id: 2a8044f3493046fcc8c67016902dc7beec6f026a | |
43 | * | |
44 | * Ref: git://git.lttng.org/userspace-rcu.git | |
45 | */ | |
46 | ||
0fdd1e2c DG |
47 | /* |
48 | * Update futex according to active or not. This scheme is used to wake every | |
49 | * libust waiting on the shared memory map futex hence the INT_MAX used in the | |
50 | * futex() call. If active, we set the value and wake everyone else we indicate | |
51 | * that we are gone (cleanup() case). | |
52 | */ | |
53 | void futex_wait_update(int32_t *futex, int active) | |
54 | { | |
55 | if (active) { | |
56 | uatomic_set(futex, 1); | |
57 | futex_async(futex, FUTEX_WAKE, | |
58 | INT_MAX, NULL, NULL, 0); | |
59 | } else { | |
60 | uatomic_set(futex, 0); | |
61 | } | |
62 | ||
63 | DBG("Futex wait update active %d", active); | |
64 | } | |
65 | ||
099e26bd DG |
66 | /* |
67 | * Prepare futex. | |
68 | */ | |
69 | void futex_nto1_prepare(int32_t *futex) | |
70 | { | |
71 | uatomic_set(futex, -1); | |
72 | cmm_smp_mb(); | |
73 | ||
74 | DBG("Futex n to 1 prepare done"); | |
75 | } | |
76 | ||
77 | /* | |
78 | * Wait futex. | |
79 | */ | |
80 | void futex_nto1_wait(int32_t *futex) | |
81 | { | |
82 | cmm_smp_mb(); | |
83 | ||
84 | if (uatomic_read(futex) == -1) { | |
85 | futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0); | |
86 | } | |
87 | ||
88 | DBG("Futex n to 1 wait done"); | |
89 | } | |
90 | ||
91 | /* | |
92 | * Wake 1 futex. | |
93 | */ | |
94 | void futex_nto1_wake(int32_t *futex) | |
95 | { | |
6e59ae26 | 96 | if (caa_unlikely(uatomic_read(futex) == -1)) { |
099e26bd DG |
97 | uatomic_set(futex, 0); |
98 | futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0); | |
99 | } | |
100 | ||
101 | DBG("Futex n to 1 wake done"); | |
102 | } |