1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
.\" $OpenBSD: futex.2,v 1.5 2019/01/18 05:06:37 cheloha Exp $
.\"
.\" Copyright (c) 2017 Martin Pieuchot
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: January 18 2019 $
.Dt FUTEX 2
.Os
.Sh NAME
.Nm futex
.Nd fast userspace locking primitive
.Sh SYNOPSIS
.In sys/time.h
.In sys/futex.h
.Ft int
.Fo futex
.Fa "volatile uint32_t *uaddr"
.Fa "int op"
.Fa "int val"
.Fa "const struct timespec *timeout"
.Fa "volatile uint32_t *uaddr2"
.Fc
.Sh DESCRIPTION
The
.Fn futex
syscall provides sleep and wakeup primitives related to a particular address.
.Pp
Three
.Fa op
operations are currently supported:
.Bl -tag -width FUTEX_REQUEUE -offset indent
.It Dv FUTEX_WAIT
If
.Fa val
is equal to
.Pf * Fa uaddr ,
the calling thread is blocked on the
.Dq wait channel
identified by
.Fa uaddr
until
.Fa timeout
expires or until another thread issues a
.Dv FUTEX_WAKE
or
.Dv FUTEX_REQUEUE
operation with the same
.Fa uaddr
address.
.Fa uaddr2
is ignored.
.It Dv FUTEX_WAKE
Unblocks
.Fa val
threads sleeping on the
wait channel identified by
.Fa uaddr .
.Fa timeout
and
.Fa uaddr2
are ignored.
.It Dv FUTEX_REQUEUE
Similar to
.Dv FUTEX_WAKE
but also requeue remaining threads from the wait channel
.Fa uaddr
to
.Fa uaddr2 .
In this case, pass
.Fa "uint32_t val2"
as the fourth argument instead of
.Fa timeout .
At most that number of threads is requeued.
.El
.Sh RETURN VALUES
For
.Dv FUTEX_WAKE
and
.Dv FUTEX_REQUEUE ,
.Fn futex
returns the number of woken threads.
.Pp
For
.Dv FUTEX_WAIT ,
.Fn futex
returns zero if woken by a matching
.Dv FUTEX_WAKE
or
.Dv FUTEX_REQUEUE
call, otherwise an error number is returned to indicate the error.
.Sh ERRORS
.Fn futex
will fail if:
.Bl -tag -width Er
.It Bq Er ENOSYS
The
.Fa op
argument is invalid.
.It Bq Er EFAULT
The userspace address
.Fa uaddr
is invalid.
.It Bq Er EAGAIN
The value pointed to by
.Fa uaddr
is not the same as the expected value
.Fa val .
.It Bq Er EINVAL
The
.Fa timeout
specified a second value less than zero,
or a nanosecond value less than zero or greater than or equal to 1000 million.
.It Bq Er ETIMEDOUT
The
.Fa timeout
expired before the thread was woken up
.It Bq Er EINTR
A signal arrived.
.It Bq Er ECANCELED
A signal arrived and
.Fa SA_RESTART
was set.
.El
.Sh SEE ALSO
.Xr sigaction 2 ,
.Xr pthread_cond_wait 3 ,
.Xr pthread_mutex_lock 3 ,
.Xr tsleep 9
.Rs
.%A Ulrich Drepper
.%T Futexes Are Tricky
.%U https://www.akkadia.org/drepper/futex.pdf
.%D November 5, 2011
.Re
.Sh HISTORY
The
.Fn futex
syscall first appeared in Linux 2.5.7 and was added to
.Ox 6.2 .
|