1 /*
2 * Copyright © 2015 Intel
3 * Copyright © 2022 Yonggang Luo
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "util/futex.h"
26
27 #if UTIL_FUTEX_SUPPORTED
28
29 #if defined(HAVE_LINUX_FUTEX_H)
30
31 #include <limits.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include <linux/futex.h>
35 #include <sys/syscall.h>
36
37 #ifndef SYS_futex
38 #define SYS_futex SYS_futex_time64
39 #endif
40
sys_futex(void * addr1,int op,int val1,const struct timespec * timeout,void * addr2,int val3)41 static inline long sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2, int val3)
42 {
43 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
44 }
45
futex_wake(uint32_t * addr,int count)46 int futex_wake(uint32_t *addr, int count)
47 {
48 return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
49 }
50
futex_wait(uint32_t * addr,int32_t value,const struct timespec * timeout)51 int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
52 {
53 /* FUTEX_WAIT_BITSET with FUTEX_BITSET_MATCH_ANY is equivalent to
54 * FUTEX_WAIT, except that it treats the timeout as absolute. */
55 return sys_futex(addr, FUTEX_WAIT_BITSET, value, timeout, NULL,
56 FUTEX_BITSET_MATCH_ANY);
57 }
58
59 #elif defined(__FreeBSD__)
60
61 #include <assert.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <sys/types.h>
65 #include <sys/umtx.h>
66
futex_wake(uint32_t * addr,int count)67 int futex_wake(uint32_t *addr, int count)
68 {
69 assert(count == (int)(uint32_t)count); /* Check that bits weren't discarded */
70 return _umtx_op(addr, UMTX_OP_WAKE, (uint32_t)count, NULL, NULL) == -1 ? errno : 0;
71 }
72
futex_wait(uint32_t * addr,int32_t value,const struct timespec * timeout)73 int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
74 {
75 void *uaddr = NULL, *uaddr2 = NULL;
76 struct _umtx_time tmo = {
77 ._flags = UMTX_ABSTIME,
78 ._clockid = CLOCK_MONOTONIC
79 };
80
81 assert(value == (int)(uint32_t)value); /* Check that bits weren't discarded */
82
83 if (timeout != NULL) {
84 tmo._timeout = *timeout;
85 uaddr = (void *)(uintptr_t)sizeof(tmo);
86 uaddr2 = (void *)&tmo;
87 }
88
89 return _umtx_op(addr, UMTX_OP_WAIT_UINT, (uint32_t)value, uaddr, uaddr2) == -1 ? errno : 0;
90 }
91
92 #elif defined(__OpenBSD__)
93
94 #include <sys/futex.h>
95 #include <sys/time.h>
96
futex_wake(uint32_t * addr,int count)97 int futex_wake(uint32_t *addr, int count)
98 {
99 return futex(addr, FUTEX_WAKE, count, NULL, NULL);
100 }
101
futex_wait(uint32_t * addr,int32_t value,const struct timespec * timeout)102 int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
103 {
104 struct timespec tsnow, tsrel;
105
106 if (timeout == NULL)
107 return futex(addr, FUTEX_WAIT, value, NULL, NULL);
108
109 clock_gettime(CLOCK_MONOTONIC, &tsnow);
110 if (timespeccmp(&tsnow, timeout, <))
111 timespecsub(timeout, &tsnow, &tsrel);
112 else
113 timespecclear(&tsrel);
114 return futex(addr, FUTEX_WAIT, value, &tsrel, NULL);
115 }
116
117 #elif defined(_WIN32) && !defined(WINDOWS_NO_FUTEX)
118
119 #include <windows.h>
120 #include <stdint.h>
121 #include <limits.h>
122 #include <assert.h>
123 #include <errno.h>
124
futex_wake(uint32_t * addr,int count)125 int futex_wake(uint32_t *addr, int count)
126 {
127 /* All current callers fall into one of these buckets, and we'll get the semantics
128 * wrong if someone tries to be more clever.
129 */
130 assert(count == 1 || count == INT32_MAX);
131 if (count == 1)
132 WakeByAddressSingle(addr);
133 else
134 WakeByAddressAll(addr);
135 return count;
136 }
137
futex_wait(uint32_t * addr,int32_t value,const struct timespec * timeout)138 int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
139 {
140 DWORD timeout_ms = INFINITE;
141 if (timeout != NULL) {
142 struct timespec tsnow;
143 timespec_get(&tsnow, TIME_UTC);
144
145 timeout_ms = (timeout->tv_sec - tsnow.tv_nsec) * 1000 +
146 (timeout->tv_nsec - tsnow.tv_nsec) / 1000000;
147 }
148
149 if (WaitOnAddress(addr, &value, sizeof(value), timeout_ms))
150 return 0;
151 return GetLastError() == ERROR_TIMEOUT ? ETIMEDOUT : -1;
152 }
153
154 #else
155 #error UTIL_FUTEX_SUPPORTED is not implemented but the header told it is supported on this platform
156 #endif
157
158 #endif /* UTIL_FUTEX_SUPPORTED */
159