Commit | Line | Data |
---|---|---|
099e26bd | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
099e26bd | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
099e26bd | 6 | * |
099e26bd DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
0fdd1e2c | 10 | #include <limits.h> |
099e26bd DG |
11 | #include <unistd.h> |
12 | #include <urcu.h> | |
13 | #include <urcu/futex.h> | |
14 | ||
90e535ef | 15 | #include <common/common.h> |
099e26bd DG |
16 | |
17 | #include "futex.h" | |
18 | ||
19 | /* | |
20 | * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the | |
21 | * "nto1" added to all function signature. | |
22 | * | |
b45947b9 JG |
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. | |
26 | * | |
27 | * There is also a formal verification available in the git tree. | |
28 | * | |
29 | * branch: formal-model | |
30 | * commit id: 2a8044f3493046fcc8c67016902dc7beec6f026a | |
31 | * | |
32 | * Ref: git://git.lttng.org/userspace-rcu.git | |
099e26bd DG |
33 | */ |
34 | ||
0fdd1e2c DG |
35 | /* |
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). | |
40 | */ | |
90e535ef | 41 | LTTNG_HIDDEN |
0fdd1e2c DG |
42 | void futex_wait_update(int32_t *futex, int active) |
43 | { | |
44 | if (active) { | |
45 | uatomic_set(futex, 1); | |
549731b7 MD |
46 | if (futex_async(futex, FUTEX_WAKE, |
47 | INT_MAX, NULL, NULL, 0) < 0) { | |
48 | PERROR("futex_async"); | |
49 | abort(); | |
50 | } | |
0fdd1e2c DG |
51 | } else { |
52 | uatomic_set(futex, 0); | |
53 | } | |
54 | ||
55 | DBG("Futex wait update active %d", active); | |
56 | } | |
57 | ||
099e26bd DG |
58 | /* |
59 | * Prepare futex. | |
60 | */ | |
90e535ef | 61 | LTTNG_HIDDEN |
099e26bd DG |
62 | void futex_nto1_prepare(int32_t *futex) |
63 | { | |
b45947b9 | 64 | uatomic_set(futex, -1); |
099e26bd DG |
65 | cmm_smp_mb(); |
66 | ||
67 | DBG("Futex n to 1 prepare done"); | |
68 | } | |
69 | ||
70 | /* | |
71 | * Wait futex. | |
72 | */ | |
90e535ef | 73 | LTTNG_HIDDEN |
099e26bd DG |
74 | void futex_nto1_wait(int32_t *futex) |
75 | { | |
b45947b9 | 76 | cmm_smp_mb(); |
099e26bd | 77 | |
b45947b9 JG |
78 | if (uatomic_read(futex) != -1) |
79 | goto end; | |
80 | while (futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { | |
549731b7 MD |
81 | switch (errno) { |
82 | case EWOULDBLOCK: | |
83 | /* Value already changed. */ | |
b45947b9 | 84 | goto end; |
549731b7 MD |
85 | case EINTR: |
86 | /* Retry if interrupted by signal. */ | |
87 | break; /* Get out of switch. */ | |
88 | default: | |
89 | /* Unexpected error. */ | |
b45947b9 | 90 | PERROR("futex_async"); |
549731b7 MD |
91 | abort(); |
92 | } | |
099e26bd | 93 | } |
b45947b9 | 94 | end: |
099e26bd DG |
95 | DBG("Futex n to 1 wait done"); |
96 | } | |
97 | ||
98 | /* | |
99 | * Wake 1 futex. | |
100 | */ | |
90e535ef | 101 | LTTNG_HIDDEN |
099e26bd DG |
102 | void futex_nto1_wake(int32_t *futex) |
103 | { | |
b45947b9 JG |
104 | if (caa_unlikely(uatomic_read(futex) != -1)) |
105 | goto end; | |
106 | uatomic_set(futex, 0); | |
107 | if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) { | |
108 | PERROR("futex_async"); | |
109 | abort(); | |
099e26bd | 110 | } |
b45947b9 | 111 | end: |
099e26bd DG |
112 | DBG("Futex n to 1 wake done"); |
113 | } |