xshmfence_futex.h revision fbc03982
1/*
2 * Copyright © 2013 Keith Packard
3 * Copyright © 2013 Jung-uk Kim <jkim@FreeBSD.org>
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission.  The copyright holders make no representations
12 * about the suitability of this software for any purpose.  It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#ifndef _XSHMFENCE_FUTEX_H_
25#define _XSHMFENCE_FUTEX_H_
26
27#include <errno.h>
28
29#ifdef HAVE_UMTX
30
31#include <sys/types.h>
32#include <sys/umtx.h>
33#include <limits.h>
34
35static inline int sys_futex(void *addr, int op, int32_t val)
36{
37	return _umtx_op(addr, op, (uint32_t)val, NULL, NULL) == -1 ? errno : 0;
38}
39
40static inline int futex_wake(int32_t *addr) {
41	return sys_futex(addr, UMTX_OP_WAKE, INT_MAX);
42}
43
44static inline int futex_wait(int32_t *addr, int32_t value) {
45	return sys_futex(addr, UMTX_OP_WAIT_UINT, value);
46}
47
48#else
49
50#include <stdint.h>
51#include <values.h>
52#include <linux/futex.h>
53#include <sys/time.h>
54#include <sys/syscall.h>
55
56static inline long sys_futex(void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3)
57{
58	return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
59}
60
61static inline int futex_wake(int32_t *addr) {
62	return sys_futex(addr, FUTEX_WAKE, MAXINT, NULL, NULL, 0);
63}
64
65static inline int futex_wait(int32_t *addr, int32_t value) {
66	return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
67}
68
69#endif
70
71#define barrier() __asm__ __volatile__("": : :"memory")
72
73static inline void atomic_store(int32_t *f, int32_t v)
74{
75	barrier();
76	*f = v;
77	barrier();
78}
79
80static inline int32_t atomic_fetch(int32_t *a)
81{
82	int32_t v;
83	barrier();
84	v = *a;
85	barrier();
86	return v;
87}
88
89struct xshmfence {
90    int32_t     v;
91};
92
93#define xshmfence_init(fd)
94
95#endif /* _XSHMFENCE_FUTEX_H_ */
96