Move dynamic-type to libcommon
[lttng-ust.git] / src / common / compat / futex.h
CommitLineData
10544ee8 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-or-later
10544ee8 3 *
c0c0989a 4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10544ee8 5 *
c0c0989a 6 * Userspace RCU - sys_futex/compat_futex header.
10544ee8
MD
7 */
8
58f84478
MJ
9#ifndef _UST_COMMON_COMPAT_FUTEX_H
10#define _UST_COMMON_COMPAT_FUTEX_H
c0c0989a 11
10544ee8
MD
12#include <errno.h>
13#include <stdint.h>
14#include <time.h>
15#include <sys/syscall.h>
16
17#ifdef __cplusplus
18extern "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
38extern int lttng_ust_compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
1d18d519
MJ
39 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
40 __attribute__((visibility("hidden")));
ddabe860 41
10544ee8 42extern int lttng_ust_compat_futex_async(int32_t *uaddr, int op, int32_t val,
1d18d519
MJ
43 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
44 __attribute__((visibility("hidden")));
10544ee8
MD
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
53static 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
60static 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
83static 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
101static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
e2a195a6
MJ
102 const struct timespec *timeout,
103 int32_t *uaddr2 __attribute__((unused)),
104 int32_t val3 __attribute__((unused)))
10544ee8
MD
105{
106 int umtx_op;
107 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
108 struct _umtx_time umtx_timeout = {
109 ._flags = UMTX_ABSTIME,
110 ._clockid = CLOCK_MONOTONIC,
111 };
112
113 switch (op) {
114 case FUTEX_WAIT:
115 /* On FreeBSD, a "u_int" is a 32-bit integer. */
116 umtx_op = UMTX_OP_WAIT_UINT;
117 if (timeout != NULL) {
118 umtx_timeout._timeout = *timeout;
119 umtx_uaddr = (void *) sizeof(umtx_timeout);
120 umtx_uaddr2 = (void *) &umtx_timeout;
121 }
122 break;
123 case FUTEX_WAKE:
124 umtx_op = UMTX_OP_WAKE;
125 break;
126 default:
127 errno = EINVAL;
128 return -1;
129 }
130
131 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
132 umtx_uaddr2);
133}
134
135static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val,
136 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
137{
a3bdb11c 138 return lttng_ust_futex_async(uaddr, op, val, timeout, uaddr2, val3);
10544ee8
MD
139}
140
141#elif defined(__CYGWIN__)
142
143/*
144 * The futex_noasync compat code uses a weak symbol to share state across
145 * different shared object which is not possible on Windows with the
146 * Portable Executable format. Use the async compat code for both cases.
147 */
148static 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_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
152}
153
154static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
155 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
156{
157 return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
158}
159
160#else
161
162static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val,
163 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
164{
165 return lttng_ust_compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3);
166}
167
168static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val,
169 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
170{
171 return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
172}
173
174#endif
175
176#ifdef __cplusplus
177}
178#endif
179
58f84478 180#endif /* _UST_COMMON_COMPAT_FUTEX_H */
This page took 0.03205 seconds and 4 git commands to generate.