futex.h revision 01e04c3f
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
29#include <limits.h>
30#include <stdint.h>
31#include <unistd.h>
32#include <linux/futex.h>
33#include <sys/syscall.h>
34#include <sys/time.h>
35
36static inline long sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2, int val3)
37{
38   return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
39}
40
41static inline int futex_wake(uint32_t *addr, int count)
42{
43   return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
44}
45
46static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
47{
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);
52}
53
54#endif
55
56#endif /* UTIL_FUTEX_H */
57