06172d3d1f721bd6a6e4d02c34f67b4291a53d3d
[lttng-ust.git] / liblttng-ust / futex.h
1 #ifndef _LTTNG_UST_FUTEX_H
2 #define _LTTNG_UST_FUTEX_H
3
4 /*
5 * liblttng-ust/futex.h
6 *
7 * Userspace RCU - sys_futex/compat_futex header.
8 *
9 * Copyright 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <errno.h>
27 #include <stdint.h>
28 #include <time.h>
29 #include <sys/syscall.h>
30
31 #include "helper.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define FUTEX_WAIT 0
38 #define FUTEX_WAKE 1
39
40 /*
41 * sys_futex compatibility header.
42 * Use *only* *either of* futex_noasync OR futex_async on a given address.
43 *
44 * futex_noasync cannot be executed in signal handlers, but ensures that
45 * it will be put in a wait queue even in compatibility mode.
46 *
47 * futex_async is signal-handler safe for the wakeup. It uses polling
48 * on the wait-side in compatibility mode.
49 *
50 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
51 * (returns EINTR).
52 */
53
54 LTTNG_HIDDEN
55 extern int lttng_ust_compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
56 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
57 LTTNG_HIDDEN
58 extern int lttng_ust_compat_futex_async(int32_t *uaddr, int op, int32_t val,
59 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
60
61 #if (defined(__linux__) && defined(__NR_futex))
62
63 #include <unistd.h>
64 #include <errno.h>
65 #include <urcu/compiler.h>
66 #include <urcu/arch.h>
67
68 static inline int lttng_ust_futex(int32_t *uaddr, int op, int32_t val,
69 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
70 {
71 return syscall(__NR_futex, uaddr, op, val, timeout,
72 uaddr2, val3);
73 }
74
75 static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val,
76 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
77 {
78 int ret;
79
80 ret = lttng_ust_futex(uaddr, op, val, timeout, uaddr2, val3);
81 if (caa_unlikely(ret < 0 && errno == ENOSYS)) {
82 /*
83 * The fallback on ENOSYS is the async-safe version of
84 * the compat futex implementation, because the
85 * async-safe compat implementation allows being used
86 * concurrently with calls to futex(). Indeed, sys_futex
87 * FUTEX_WAIT, on some architectures (mips and parisc),
88 * within a given process, spuriously return ENOSYS due
89 * to signal restart bugs on some kernel versions.
90 */
91 return lttng_ust_compat_futex_async(uaddr, op, val, timeout,
92 uaddr2, val3);
93 }
94 return ret;
95
96 }
97
98 static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
99 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
100 {
101 int ret;
102
103 ret = lttng_ust_futex(uaddr, op, val, timeout, uaddr2, val3);
104 if (caa_unlikely(ret < 0 && errno == ENOSYS)) {
105 return lttng_ust_compat_futex_async(uaddr, op, val, timeout,
106 uaddr2, val3);
107 }
108 return ret;
109 }
110
111 #elif defined(__FreeBSD__)
112
113 #include <sys/types.h>
114 #include <sys/umtx.h>
115
116 static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
117 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
118 {
119 int umtx_op;
120 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
121 struct _umtx_time umtx_timeout = {
122 ._flags = UMTX_ABSTIME,
123 ._clockid = CLOCK_MONOTONIC,
124 };
125
126 switch (op) {
127 case FUTEX_WAIT:
128 /* On FreeBSD, a "u_int" is a 32-bit integer. */
129 umtx_op = UMTX_OP_WAIT_UINT;
130 if (timeout != NULL) {
131 umtx_timeout._timeout = *timeout;
132 umtx_uaddr = (void *) sizeof(umtx_timeout);
133 umtx_uaddr2 = (void *) &umtx_timeout;
134 }
135 break;
136 case FUTEX_WAKE:
137 umtx_op = UMTX_OP_WAKE;
138 break;
139 default:
140 errno = EINVAL;
141 return -1;
142 }
143
144 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
145 umtx_uaddr2);
146 }
147
148 static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val,
149 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
150 {
151 return lttng_ust_futex_async(uaddr, op, val, timeout, uaddr2, val3);
152 }
153
154 #elif defined(__CYGWIN__)
155
156 /*
157 * The futex_noasync compat code uses a weak symbol to share state across
158 * different shared object which is not possible on Windows with the
159 * Portable Executable format. Use the async compat code for both cases.
160 */
161 static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val,
162 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
163 {
164 return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
165 }
166
167 static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
168 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
169 {
170 return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
171 }
172
173 #else
174
175 static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val,
176 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
177 {
178 return lttng_ust_compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3);
179 }
180
181 static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
182 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
183 {
184 return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
185 }
186
187 #endif
188
189 #ifdef __cplusplus
190 }
191 #endif
192
193 #endif /* _LTTNG_UST_FUTEX_H */
This page took 0.031649 seconds and 3 git commands to generate.