| 1 | #ifndef _URCU_FUTEX_H |
| 2 | #define _URCU_FUTEX_H |
| 3 | |
| 4 | /* |
| 5 | * urcu-futex.h |
| 6 | * |
| 7 | * Userspace RCU - sys_futex/compat_futex header. |
| 8 | * |
| 9 | * Copyright 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2.1 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this library; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <urcu/config.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | #define FUTEX_WAIT 0 |
| 34 | #define FUTEX_WAKE 1 |
| 35 | |
| 36 | /* |
| 37 | * sys_futex compatibility header. |
| 38 | * Use *only* *either of* futex_noasync OR futex_async on a given address. |
| 39 | * |
| 40 | * futex_noasync cannot be executed in signal handlers, but ensures that |
| 41 | * it will be put in a wait queue even in compatibility mode. |
| 42 | * |
| 43 | * futex_async is signal-handler safe for the wakeup. It uses polling |
| 44 | * on the wait-side in compatibility mode. |
| 45 | * |
| 46 | * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted |
| 47 | * (returns EINTR). |
| 48 | */ |
| 49 | |
| 50 | extern int compat_futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 51 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); |
| 52 | extern int compat_futex_async(int32_t *uaddr, int op, int32_t val, |
| 53 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); |
| 54 | |
| 55 | #ifdef CONFIG_RCU_HAVE_FUTEX |
| 56 | |
| 57 | #include <unistd.h> |
| 58 | #include <errno.h> |
| 59 | #include <urcu/compiler.h> |
| 60 | #include <urcu/arch.h> |
| 61 | |
| 62 | static inline int futex(int32_t *uaddr, int op, int32_t val, |
| 63 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 64 | { |
| 65 | return syscall(__NR_futex, uaddr, op, val, timeout, |
| 66 | uaddr2, val3); |
| 67 | } |
| 68 | |
| 69 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 70 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 71 | { |
| 72 | int ret; |
| 73 | |
| 74 | ret = futex(uaddr, op, val, timeout, uaddr2, val3); |
| 75 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { |
| 76 | return compat_futex_noasync(uaddr, op, val, timeout, |
| 77 | uaddr2, val3); |
| 78 | } |
| 79 | return ret; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, |
| 84 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 85 | { |
| 86 | int ret; |
| 87 | |
| 88 | ret = futex(uaddr, op, val, timeout, uaddr2, val3); |
| 89 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { |
| 90 | return compat_futex_async(uaddr, op, val, timeout, |
| 91 | uaddr2, val3); |
| 92 | } |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | #else |
| 97 | |
| 98 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 99 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 100 | { |
| 101 | return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3); |
| 102 | } |
| 103 | |
| 104 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, |
| 105 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 106 | { |
| 107 | return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | #ifdef __cplusplus |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | #endif /* _URCU_FUTEX_H */ |