Commit | Line | Data |
---|---|---|
099e26bd DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
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. | |
099e26bd DG |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
d14d33bf | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
099e26bd DG |
12 | * GNU General Public License for more details. |
13 | * | |
d14d33bf AM |
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. | |
099e26bd DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
6c1c0768 | 20 | #define _LGPL_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 | ||
90e535ef | 27 | #include <common/common.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 | */ | |
90e535ef | 53 | LTTNG_HIDDEN |
0fdd1e2c DG |
54 | void futex_wait_update(int32_t *futex, int active) |
55 | { | |
56 | if (active) { | |
57 | uatomic_set(futex, 1); | |
549731b7 MD |
58 | if (futex_async(futex, FUTEX_WAKE, |
59 | INT_MAX, NULL, NULL, 0) < 0) { | |
60 | PERROR("futex_async"); | |
61 | abort(); | |
62 | } | |
0fdd1e2c DG |
63 | } else { |
64 | uatomic_set(futex, 0); | |
65 | } | |
66 | ||
67 | DBG("Futex wait update active %d", active); | |
68 | } | |
69 | ||
099e26bd DG |
70 | /* |
71 | * Prepare futex. | |
72 | */ | |
90e535ef | 73 | LTTNG_HIDDEN |
099e26bd DG |
74 | void futex_nto1_prepare(int32_t *futex) |
75 | { | |
76 | uatomic_set(futex, -1); | |
77 | cmm_smp_mb(); | |
78 | ||
79 | DBG("Futex n to 1 prepare done"); | |
80 | } | |
81 | ||
82 | /* | |
83 | * Wait futex. | |
84 | */ | |
90e535ef | 85 | LTTNG_HIDDEN |
099e26bd DG |
86 | void futex_nto1_wait(int32_t *futex) |
87 | { | |
88 | cmm_smp_mb(); | |
89 | ||
549731b7 MD |
90 | if (uatomic_read(futex) != -1) |
91 | goto end; | |
92 | while (futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { | |
93 | switch (errno) { | |
94 | case EWOULDBLOCK: | |
95 | /* Value already changed. */ | |
96 | goto end; | |
97 | case EINTR: | |
98 | /* Retry if interrupted by signal. */ | |
99 | break; /* Get out of switch. */ | |
100 | default: | |
101 | /* Unexpected error. */ | |
102 | PERROR("futex_async"); | |
103 | abort(); | |
104 | } | |
099e26bd | 105 | } |
549731b7 | 106 | end: |
099e26bd DG |
107 | DBG("Futex n to 1 wait done"); |
108 | } | |
109 | ||
110 | /* | |
111 | * Wake 1 futex. | |
112 | */ | |
90e535ef | 113 | LTTNG_HIDDEN |
099e26bd DG |
114 | void futex_nto1_wake(int32_t *futex) |
115 | { | |
549731b7 MD |
116 | if (caa_unlikely(uatomic_read(futex) != -1)) |
117 | goto end; | |
118 | uatomic_set(futex, 0); | |
119 | if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) { | |
120 | PERROR("futex_async"); | |
121 | abort(); | |
099e26bd | 122 | } |
549731b7 | 123 | end: |
099e26bd DG |
124 | DBG("Futex n to 1 wake done"); |
125 | } |