| 1 |
/* |
| 2 |
* Copyright © 2013 Keith Packard |
| 3 |
* Copyright © 2013 Jung-uk Kim <[email protected]> |
| 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 |
|
| 35 |
static 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 |
|
| 40 |
static inline int futex_wake(int32_t *addr) { |
| 41 |
return sys_futex(addr, UMTX_OP_WAKE, INT_MAX); |
| 42 |
} |
| 43 |
|
| 44 |
static 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 |
|
| 56 |
#ifndef SYS_futex |
| 57 |
#define SYS_futex SYS_futex_time64 |
| 58 |
#endif |
| 59 |
|
| 60 |
static inline long sys_futex(void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3) |
| 61 |
{ |
| 62 |
return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3); |
| 63 |
} |
| 64 |
|
| 65 |
static inline int futex_wake(int32_t *addr) { |
| 66 |
return sys_futex(addr, FUTEX_WAKE, MAXINT, NULL, NULL, 0); |
| 67 |
} |
| 68 |
|
| 69 |
static inline int futex_wait(int32_t *addr, int32_t value) { |
| 70 |
return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0); |
| 71 |
} |
| 72 |
|
| 73 |
#endif |
| 74 |
|
| 75 |
#define barrier() __asm__ __volatile__("": : :"memory") |
| 76 |
|
| 77 |
static inline void atomic_store(int32_t *f, int32_t v) |
| 78 |
{ |
| 79 |
barrier(); |
| 80 |
*f = v; |
| 81 |
barrier(); |
| 82 |
} |
| 83 |
|
| 84 |
static inline int32_t atomic_fetch(int32_t *a) |
| 85 |
{ |
| 86 |
int32_t v; |
| 87 |
barrier(); |
| 88 |
v = *a; |
| 89 |
barrier(); |
| 90 |
return v; |
| 91 |
} |
| 92 |
|
| 93 |
struct xshmfence { |
| 94 |
int32_t v; |
| 95 |
}; |
| 96 |
|
| 97 |
#define xshmfence_init(fd) |
| 98 |
|
| 99 |
#endif /* _XSHMFENCE_FUTEX_H_ */ |