2 * Copyright © 2015 Intel
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:
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
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
27 #if defined(HAVE_LINUX_FUTEX_H)
32 #include <linux/futex.h>
33 #include <sys/syscall.h>
36 static inline long sys_futex(void *addr1
, int op
, int val1
, const struct timespec
*timeout
, void *addr2
, int val3
)
38 return syscall(SYS_futex
, addr1
, op
, val1
, timeout
, addr2
, val3
);
41 static inline int futex_wake(uint32_t *addr
, int count
)
43 return sys_futex(addr
, FUTEX_WAKE
, count
, NULL
, NULL
, 0);
46 static inline int futex_wait(uint32_t *addr
, int32_t value
, const struct timespec
*timeout
)
48 /* FUTEX_WAIT_BITSET with FUTEX_BITSET_MATCH_ANY is equivalent to
49 * FUTEX_WAIT, except that it treats the timeout as absolute. */
50 return sys_futex(addr
, FUTEX_WAIT_BITSET
, value
, timeout
, NULL
,
51 FUTEX_BITSET_MATCH_ANY
);
54 #elif defined(__FreeBSD__)
59 #include <sys/types.h>
63 static inline int futex_wake(uint32_t *addr
, int count
)
65 assert(count
== (int)(uint32_t)count
); /* Check that bits weren't discarded */
66 return _umtx_op(addr
, UMTX_OP_WAKE
, (uint32_t)count
, NULL
, NULL
) == -1 ? errno
: 0;
69 static inline int futex_wait(uint32_t *addr
, int32_t value
, struct timespec
*timeout
)
71 void *uaddr
= NULL
, *uaddr2
= NULL
;
72 struct _umtx_time tmo
= {
73 ._flags
= UMTX_ABSTIME
,
74 ._clockid
= CLOCK_MONOTONIC
77 assert(value
== (int)(uint32_t)value
); /* Check that bits weren't discarded */
79 if (timeout
!= NULL
) {
80 tmo
._timeout
= *timeout
;
81 uaddr
= (void *)(uintptr_t)sizeof(tmo
);
82 uaddr2
= (void *)&tmo
;
85 return _umtx_op(addr
, UMTX_OP_WAIT_UINT
, (uint32_t)value
, uaddr
, uaddr2
) == -1 ? errno
: 0;
88 #elif defined(__OpenBSD__)
91 #include <sys/futex.h>
93 static inline int futex_wake(uint32_t *addr
, int count
)
95 return futex(addr
, FUTEX_WAKE
, count
, NULL
, NULL
);
98 static inline int futex_wait(uint32_t *addr
, int32_t value
, const struct timespec
*timeout
)
100 struct timespec tsrel
, tsnow
;
101 clock_gettime(CLOCK_MONOTONIC
, &tsnow
);
102 timespecsub(timeout
, &tsrel
, &tsrel
);
103 return futex(addr
, FUTEX_WAIT
, value
, &tsrel
, NULL
);
108 #endif /* UTIL_FUTEX_H */