]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgomp/config/linux/x86/futex.h
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libgomp / config / linux / x86 / futex.h
CommitLineData
748086b7 1/* Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc.
953ff289
DN
2 Contributed by Richard Henderson <rth@redhat.com>.
3
4 This file is part of the GNU OpenMP Library (libgomp).
5
6 Libgomp is free software; you can redistribute it and/or modify it
748086b7
JJ
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
953ff289
DN
10
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
748086b7 13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
953ff289
DN
14 more details.
15
748086b7
JJ
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
953ff289
DN
24
25/* Provide target-specific access to the futex system call. */
26
953ff289
DN
27#ifdef __LP64__
28# ifndef SYS_futex
29# define SYS_futex 202
30# endif
31
32static inline void
33futex_wait (int *addr, int val)
34{
a68ab351 35 register long r10 __asm__("%r10");
953ff289
DN
36 long res;
37
a68ab351 38 r10 = 0;
953ff289
DN
39 __asm volatile ("syscall"
40 : "=a" (res)
a68ab351
JJ
41 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
42 "d" (val), "r" (r10)
953ff289 43 : "r11", "rcx", "memory");
a68ab351
JJ
44 if (__builtin_expect (res == -ENOSYS, 0))
45 {
46 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
47 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
48 r10 = 0;
49 __asm volatile ("syscall"
50 : "=a" (res)
51 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
52 "d" (val), "r" (r10)
53 : "r11", "rcx", "memory");
54 }
953ff289
DN
55}
56
57static inline void
58futex_wake (int *addr, int count)
59{
60 long res;
61
62 __asm volatile ("syscall"
63 : "=a" (res)
a68ab351
JJ
64 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
65 "d" (count)
953ff289 66 : "r11", "rcx", "memory");
a68ab351
JJ
67 if (__builtin_expect (res == -ENOSYS, 0))
68 {
69 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
70 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
71 __asm volatile ("syscall"
72 : "=a" (res)
73 : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
74 "d" (count)
75 : "r11", "rcx", "memory");
76 }
953ff289
DN
77}
78#else
79# ifndef SYS_futex
80# define SYS_futex 240
81# endif
82
83# ifdef __PIC__
84
a68ab351 85static inline long
953ff289
DN
86sys_futex0 (int *addr, int op, int val)
87{
88 long res;
89
90 __asm volatile ("xchgl\t%%ebx, %2\n\t"
91 "int\t$0x80\n\t"
92 "xchgl\t%%ebx, %2"
93 : "=a" (res)
94 : "0"(SYS_futex), "r" (addr), "c"(op),
95 "d"(val), "S"(0)
96 : "memory");
a68ab351 97 return res;
953ff289
DN
98}
99
100# else
101
a68ab351 102static inline long
953ff289
DN
103sys_futex0 (int *addr, int op, int val)
104{
105 long res;
106
107 __asm volatile ("int $0x80"
108 : "=a" (res)
109 : "0"(SYS_futex), "b" (addr), "c"(op),
110 "d"(val), "S"(0)
111 : "memory");
a68ab351 112 return res;
953ff289
DN
113}
114
115# endif /* __PIC__ */
116
117static inline void
118futex_wait (int *addr, int val)
119{
a68ab351
JJ
120 long res = sys_futex0 (addr, gomp_futex_wait, val);
121 if (__builtin_expect (res == -ENOSYS, 0))
122 {
123 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
124 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
125 sys_futex0 (addr, gomp_futex_wait, val);
126 }
953ff289
DN
127}
128
129static inline void
130futex_wake (int *addr, int count)
131{
a68ab351
JJ
132 long res = sys_futex0 (addr, gomp_futex_wake, count);
133 if (__builtin_expect (res == -ENOSYS, 0))
134 {
135 gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
136 gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
137 sys_futex0 (addr, gomp_futex_wake, count);
138 }
953ff289
DN
139}
140
141#endif /* __LP64__ */
a68ab351
JJ
142
143static inline void
144cpu_relax (void)
145{
146 __asm volatile ("rep; nop" : : : "memory");
147}
148
149static inline void
150atomic_write_barrier (void)
151{
152 __sync_synchronize ();
153}