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