2015-05-27 Uros Bizjak <ubizjak@gmail.com>
[official-gcc.git] / libgomp / config / linux / x86 / futex.h
blob2592217a89033115215375c95b2e4022e6dfbae6
1 /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* Provide target-specific access to the futex system call. */
28 #ifdef __x86_64__
29 # ifndef SYS_futex
30 # define SYS_futex 202
31 # endif
33 static inline void
34 futex_wait (int *addr, int val)
36 register long r10 __asm__("%r10");
37 long res;
39 r10 = 0;
40 __asm volatile ("syscall"
41 : "=a" (res)
42 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
43 "d" (val), "r" (r10)
44 : "r11", "rcx", "memory");
45 if (__builtin_expect (res == -ENOSYS, 0))
47 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
48 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
49 r10 = 0;
50 __asm volatile ("syscall"
51 : "=a" (res)
52 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
53 "d" (val), "r" (r10)
54 : "r11", "rcx", "memory");
58 static inline void
59 futex_wake (int *addr, int count)
61 long res;
63 __asm volatile ("syscall"
64 : "=a" (res)
65 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
66 "d" (count)
67 : "r11", "rcx", "memory");
68 if (__builtin_expect (res == -ENOSYS, 0))
70 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
71 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
72 __asm volatile ("syscall"
73 : "=a" (res)
74 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
75 "d" (count)
76 : "r11", "rcx", "memory");
79 #else
80 # ifndef SYS_futex
81 # define SYS_futex 240
82 # endif
84 static inline long
85 sys_futex0 (int *addr, int op, int val)
87 long res;
89 __asm volatile ("int $0x80"
90 : "=a" (res)
91 : "0"(SYS_futex), "b" (addr), "c"(op),
92 "d"(val), "S"(0)
93 : "memory");
94 return res;
97 static inline void
98 futex_wait (int *addr, int val)
100 long res = sys_futex0 (addr, gomp_futex_wait, val);
101 if (__builtin_expect (res == -ENOSYS, 0))
103 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
104 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
105 sys_futex0 (addr, gomp_futex_wait, val);
109 static inline void
110 futex_wake (int *addr, int count)
112 long res = sys_futex0 (addr, gomp_futex_wake, count);
113 if (__builtin_expect (res == -ENOSYS, 0))
115 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
116 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
117 sys_futex0 (addr, gomp_futex_wake, count);
121 #endif /* __x86_64__ */
123 static inline void
124 cpu_relax (void)
126 __builtin_ia32_pause ();