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