3 // Copyright (C) 2015-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
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.
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/>.
25 #include <bits/atomic_futex.h>
26 #ifdef _GLIBCXX_HAS_GTHREADS
27 #if defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
34 #include <debug/debug.h>
36 // Constants for the wait/wake futex syscall operations
37 const unsigned futex_wait_op
= 0;
38 const unsigned futex_wake_op
= 1;
40 namespace std
_GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 __atomic_futex_unsigned_base::_M_futex_wait_until(unsigned *__addr
,
47 bool __has_timeout
, chrono::seconds __s
, chrono::nanoseconds __ns
)
51 // Ignore whether we actually succeeded to block because at worst,
52 // we will fall back to spin-waiting. The only thing we could do
53 // here on errors is abort.
54 int ret
__attribute__((unused
));
55 ret
= syscall (SYS_futex
, __addr
, futex_wait_op
, __val
, nullptr);
56 __glibcxx_assert(ret
== 0 || errno
== EINTR
|| errno
== EAGAIN
);
62 gettimeofday (&tv
, NULL
);
63 // Convert the absolute timeout value to a relative timeout
65 rt
.tv_sec
= __s
.count() - tv
.tv_sec
;
66 rt
.tv_nsec
= __ns
.count() - tv
.tv_usec
* 1000;
69 rt
.tv_nsec
+= 1000000000;
72 // Did we already time out?
76 if (syscall (SYS_futex
, __addr
, futex_wait_op
, __val
, &rt
) == -1)
78 __glibcxx_assert(errno
== EINTR
|| errno
== EAGAIN
79 || errno
== ETIMEDOUT
);
80 if (errno
== ETIMEDOUT
)
88 __atomic_futex_unsigned_base::_M_futex_notify_all(unsigned* __addr
)
90 // This syscall can fail for various reasons, including in situations
91 // in which there is no real error. Thus, we don't bother checking
92 // the error codes. See the futex documentation and glibc for background.
93 syscall (SYS_futex
, __addr
, futex_wake_op
, INT_MAX
);
96 _GLIBCXX_END_NAMESPACE_VERSION
98 #endif // defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
99 #endif // _GLIBCXX_HAS_GTHREADS