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 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
0fdd1e2c | 20 | #include <limits.h> |
099e26bd DG |
21 | #include <sys/syscall.h> |
22 | #include <unistd.h> | |
23 | #include <urcu.h> | |
24 | #include <urcu/futex.h> | |
25 | ||
90e535ef | 26 | #include <common/common.h> |
099e26bd DG |
27 | |
28 | #include "futex.h" | |
29 | ||
30 | /* | |
31 | * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the | |
32 | * "nto1" added to all function signature. | |
33 | * | |
34 | * Please see wait_gp()/update_counter_and_wait() calls in urcu.c in the urcu | |
35 | * git tree for a detail example of this scheme being used. futex_async() is | |
36 | * the urcu wrapper over the futex() sycall. | |
37 | * | |
38 | * There is also a formal verification available in the git tree. | |
39 | * | |
40 | * branch: formal-model | |
41 | * commit id: 2a8044f3493046fcc8c67016902dc7beec6f026a | |
42 | * | |
43 | * Ref: git://git.lttng.org/userspace-rcu.git | |
44 | */ | |
45 | ||
0fdd1e2c DG |
46 | /* |
47 | * Update futex according to active or not. This scheme is used to wake every | |
48 | * libust waiting on the shared memory map futex hence the INT_MAX used in the | |
49 | * futex() call. If active, we set the value and wake everyone else we indicate | |
50 | * that we are gone (cleanup() case). | |
51 | */ | |
90e535ef | 52 | LTTNG_HIDDEN |
0fdd1e2c DG |
53 | void futex_wait_update(int32_t *futex, int active) |
54 | { | |
55 | if (active) { | |
56 | uatomic_set(futex, 1); | |
549731b7 MD |
57 | if (futex_async(futex, FUTEX_WAKE, |
58 | INT_MAX, NULL, NULL, 0) < 0) { | |
59 | PERROR("futex_async"); | |
60 | abort(); | |
61 | } | |
0fdd1e2c DG |
62 | } else { |
63 | uatomic_set(futex, 0); | |
64 | } | |
65 | ||
66 | DBG("Futex wait update active %d", active); | |
67 | } | |
68 | ||
099e26bd DG |
69 | /* |
70 | * Prepare futex. | |
71 | */ | |
90e535ef | 72 | LTTNG_HIDDEN |
099e26bd DG |
73 | void futex_nto1_prepare(int32_t *futex) |
74 | { | |
75 | uatomic_set(futex, -1); | |
76 | cmm_smp_mb(); | |
77 | ||
78 | DBG("Futex n to 1 prepare done"); | |
79 | } | |
80 | ||
81 | /* | |
82 | * Wait futex. | |
83 | */ | |
90e535ef | 84 | LTTNG_HIDDEN |
099e26bd DG |
85 | void futex_nto1_wait(int32_t *futex) |
86 | { | |
87 | cmm_smp_mb(); | |
88 | ||
549731b7 MD |
89 | if (uatomic_read(futex) != -1) |
90 | goto end; | |
91 | while (futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { | |
92 | switch (errno) { | |
93 | case EWOULDBLOCK: | |
94 | /* Value already changed. */ | |
95 | goto end; | |
96 | case EINTR: | |
97 | /* Retry if interrupted by signal. */ | |
98 | break; /* Get out of switch. */ | |
99 | default: | |
100 | /* Unexpected error. */ | |
101 | PERROR("futex_async"); | |
102 | abort(); | |
103 | } | |
099e26bd | 104 | } |
549731b7 | 105 | end: |
099e26bd DG |
106 | DBG("Futex n to 1 wait done"); |
107 | } | |
108 | ||
109 | /* | |
110 | * Wake 1 futex. | |
111 | */ | |
90e535ef | 112 | LTTNG_HIDDEN |
099e26bd DG |
113 | void futex_nto1_wake(int32_t *futex) |
114 | { | |
549731b7 MD |
115 | if (caa_unlikely(uatomic_read(futex) != -1)) |
116 | goto end; | |
117 | uatomic_set(futex, 0); | |
118 | if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) { | |
119 | PERROR("futex_async"); | |
120 | abort(); | |
099e26bd | 121 | } |
549731b7 | 122 | end: |
099e26bd DG |
123 | DBG("Futex n to 1 wake done"); |
124 | } |