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