Fix: common: uninitialized lttng::ctl:error field
[lttng-tools.git] / src / common / futex.cpp
CommitLineData
099e26bd 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa 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
c9e313bc 15#include <common/common.hpp>
099e26bd 16
c9e313bc 17#include "futex.hpp"
099e26bd
DG
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 */
41void futex_wait_update(int32_t *futex, int active)
42{
43 if (active) {
44 uatomic_set(futex, 1);
549731b7
MD
45 if (futex_async(futex, FUTEX_WAKE,
46 INT_MAX, NULL, NULL, 0) < 0) {
47 PERROR("futex_async");
48 abort();
49 }
0fdd1e2c
DG
50 } else {
51 uatomic_set(futex, 0);
52 }
53
54 DBG("Futex wait update active %d", active);
55}
56
099e26bd
DG
57/*
58 * Prepare futex.
59 */
60void futex_nto1_prepare(int32_t *futex)
61{
b45947b9 62 uatomic_set(futex, -1);
099e26bd
DG
63 cmm_smp_mb();
64
65 DBG("Futex n to 1 prepare done");
66}
67
68/*
69 * Wait futex.
70 */
71void futex_nto1_wait(int32_t *futex)
72{
b45947b9 73 cmm_smp_mb();
099e26bd 74
b45947b9
JG
75 if (uatomic_read(futex) != -1)
76 goto end;
77 while (futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
549731b7
MD
78 switch (errno) {
79 case EWOULDBLOCK:
80 /* Value already changed. */
b45947b9 81 goto end;
549731b7
MD
82 case EINTR:
83 /* Retry if interrupted by signal. */
84 break; /* Get out of switch. */
85 default:
86 /* Unexpected error. */
b45947b9 87 PERROR("futex_async");
549731b7
MD
88 abort();
89 }
099e26bd 90 }
b45947b9 91end:
099e26bd
DG
92 DBG("Futex n to 1 wait done");
93}
94
95/*
96 * Wake 1 futex.
97 */
98void futex_nto1_wake(int32_t *futex)
99{
b45947b9
JG
100 if (caa_unlikely(uatomic_read(futex) != -1))
101 goto end;
102 uatomic_set(futex, 0);
103 if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) {
104 PERROR("futex_async");
105 abort();
099e26bd 106 }
b45947b9 107end:
099e26bd
DG
108 DBG("Futex n to 1 wake done");
109}
This page took 0.075274 seconds and 4 git commands to generate.