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 
sys_futex(void * addr1,int op,int val1,const struct timespec * timeout,void * addr2,int val3)37 static inline long sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2, int val3)
38 {
39    return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
40 }
41 
futex_wake(uint32_t * addr,int count)42 static inline int futex_wake(uint32_t *addr, int count)
43 {
44    return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
45 }
46 
futex_wait(uint32_t * addr,int32_t value,const struct timespec * timeout)47 static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
48 {
49    /* FUTEX_WAIT_BITSET with FUTEX_BITSET_MATCH_ANY is equivalent to
50     * FUTEX_WAIT, except that it treats the timeout as absolute. */
51    return sys_futex(addr, FUTEX_WAIT_BITSET, value, timeout, NULL,
52                     FUTEX_BITSET_MATCH_ANY);
53 }
54 
55 #elif defined(__FreeBSD__)
56 #define UTIL_FUTEX_SUPPORTED 1
57 
58 #include <assert.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <sys/types.h>
62 #include <sys/umtx.h>
63 #include <sys/time.h>
64 
futex_wake(uint32_t * addr,int count)65 static inline int futex_wake(uint32_t *addr, int count)
66 {
67    assert(count == (int)(uint32_t)count); /* Check that bits weren't discarded */
68    return _umtx_op(addr, UMTX_OP_WAKE, (uint32_t)count, NULL, NULL) == -1 ? errno : 0;
69 }
70 
futex_wait(uint32_t * addr,int32_t value,struct timespec * timeout)71 static inline int futex_wait(uint32_t *addr, int32_t value, struct timespec *timeout)
72 {
73    void *uaddr = NULL, *uaddr2 = NULL;
74    struct _umtx_time tmo = {
75       ._flags = UMTX_ABSTIME,
76       ._clockid = CLOCK_MONOTONIC
77    };
78 
79    assert(value == (int)(uint32_t)value); /* Check that bits weren't discarded */
80 
81    if (timeout != NULL) {
82       tmo._timeout = *timeout;
83       uaddr = (void *)(uintptr_t)sizeof(tmo);
84       uaddr2 = (void *)&tmo;
85    }
86 
87    return _umtx_op(addr, UMTX_OP_WAIT_UINT, (uint32_t)value, uaddr, uaddr2) == -1 ? errno : 0;
88 }
89 #elif defined(__DragonFly__)
90 #define UTIL_FUTEX_SUPPORTED 1
91 
92 #include <errno.h>
93 #include <unistd.h>
94 #include <time.h>
95 
futex_wake(uint32_t * ptr,int count)96 static inline int futex_wake(uint32_t *ptr, int count)
97 {
98    return umtx_wakeup((volatile const int*)ptr, count);
99 }
100 
futex_wait(uint32_t * ptr,int value,struct timespec * timeout)101 static inline int futex_wait(uint32_t *ptr, int value, struct timespec *timeout)
102 {
103    int timo = 0;
104    int ret;
105    if (timeout != NULL) {
106       timo = timeout->tv_sec * 1000*1000 + timeout->tv_nsec / 1000; /* XXX check overflow? */
107       if (timo < 0)
108          timo = 1 << 26; /* about 1 minute or so */  // 0x7fffffff; /* XXX ~35 minutes */
109       if (timo == 0 && timeout->tv_nsec > 0)
110          timo = 1; /* cap up to one 1us */
111    }
112    ret = umtx_sleep((volatile const int*)ptr, value, timo);
113    if (ret == 0)
114       return 0;
115    /* errno mangling for compat */
116    if (errno == EBUSY)
117       return EWOULDBLOCK;
118    if (errno == EWOULDBLOCK)
119       return ETIMEDOUT;
120    return errno;
121 }
122 
123 #elif defined(__OpenBSD__)
124 #define UTIL_FUTEX_SUPPORTED 1
125 
126 #include <sys/time.h>
127 #include <sys/futex.h>
128 
futex_wake(uint32_t * addr,int count)129 static inline int futex_wake(uint32_t *addr, int count)
130 {
131    return futex(addr, FUTEX_WAKE, count, NULL, NULL);
132 }
133 
futex_wait(uint32_t * addr,int32_t value,const struct timespec * timeout)134 static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
135 {
136    struct timespec tsnow, tsrel;
137 
138    if (timeout == NULL)
139       return futex(addr, FUTEX_WAIT, value, NULL, NULL);
140 
141    clock_gettime(CLOCK_MONOTONIC, &tsnow);
142    if (timespeccmp(&tsnow, timeout, <))
143       timespecsub(timeout, &tsnow, &tsrel);
144    else
145       timespecclear(&tsrel);
146    return futex(addr, FUTEX_WAIT, value, &tsrel, NULL);
147 }
148 
149 #else
150 #define UTIL_FUTEX_SUPPORTED 0
151 #endif
152 
153 #endif /* UTIL_FUTEX_H */
154