]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/c++11/futex.cc
libstdc++: Use FUTEX_CLOCK_REALTIME for futex wait
[thirdparty/gcc.git] / libstdc++-v3 / src / c++11 / futex.cc
CommitLineData
eae801ba
TR
1// futex -*- C++ -*-
2
8d9254fc 3// Copyright (C) 2015-2020 Free Software Foundation, Inc.
eae801ba
TR
4//
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)
9// any later version.
10
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.
15
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/>.
24
eae801ba 25#include <bits/atomic_futex.h>
8ba7f29e 26#ifdef _GLIBCXX_HAS_GTHREADS
1e3919ac 27#if defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
eae801ba
TR
28#include <chrono>
29#include <climits>
30#include <syscall.h>
31#include <unistd.h>
32#include <sys/time.h>
33#include <errno.h>
34#include <debug/debug.h>
35
36// Constants for the wait/wake futex syscall operations
37const unsigned futex_wait_op = 0;
5bad23ce
MC
38const unsigned futex_wait_bitset_op = 9;
39const unsigned futex_clock_realtime_flag = 256;
40const unsigned futex_bitset_match_any = ~0;
eae801ba
TR
41const unsigned futex_wake_op = 1;
42
5bad23ce
MC
43namespace
44{
45 std::atomic<bool> futex_clock_realtime_unavailable;
46}
47
eae801ba
TR
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
52 bool
53 __atomic_futex_unsigned_base::_M_futex_wait_until(unsigned *__addr,
54 unsigned __val,
55 bool __has_timeout, chrono::seconds __s, chrono::nanoseconds __ns)
56 {
57 if (!__has_timeout)
58 {
59 // Ignore whether we actually succeeded to block because at worst,
60 // we will fall back to spin-waiting. The only thing we could do
61 // here on errors is abort.
62 int ret __attribute__((unused));
55089c2b 63 ret = syscall (SYS_futex, __addr, futex_wait_op, __val, nullptr);
9fbd2e55 64 __glibcxx_assert(ret == 0 || errno == EINTR || errno == EAGAIN);
eae801ba
TR
65 return true;
66 }
67 else
68 {
5bad23ce
MC
69 if (!futex_clock_realtime_unavailable.load(std::memory_order_relaxed))
70 {
71 struct timespec rt;
72 rt.tv_sec = __s.count();
73 rt.tv_nsec = __ns.count();
74 if (syscall (SYS_futex, __addr,
75 futex_wait_bitset_op | futex_clock_realtime_flag,
76 __val, &rt, nullptr, futex_bitset_match_any) == -1)
77 {
78 __glibcxx_assert(errno == EINTR || errno == EAGAIN
79 || errno == ETIMEDOUT || errno == ENOSYS);
80 if (errno == ETIMEDOUT)
81 return false;
82 if (errno == ENOSYS)
83 {
84 futex_clock_realtime_unavailable.store(true,
85 std::memory_order_relaxed);
86 // Fall through to legacy implementation if the system
87 // call is unavailable.
88 }
89 else
90 return true;
91 }
92 else
93 return true;
94 }
95
96 // We only get to here if futex_clock_realtime_unavailable was
97 // true or has just been set to true.
eae801ba
TR
98 struct timeval tv;
99 gettimeofday (&tv, NULL);
100 // Convert the absolute timeout value to a relative timeout
101 struct timespec rt;
102 rt.tv_sec = __s.count() - tv.tv_sec;
103 rt.tv_nsec = __ns.count() - tv.tv_usec * 1000;
104 if (rt.tv_nsec < 0)
105 {
106 rt.tv_nsec += 1000000000;
107 --rt.tv_sec;
108 }
109 // Did we already time out?
110 if (rt.tv_sec < 0)
111 return false;
112
113 if (syscall (SYS_futex, __addr, futex_wait_op, __val, &rt) == -1)
114 {
9fbd2e55
JW
115 __glibcxx_assert(errno == EINTR || errno == EAGAIN
116 || errno == ETIMEDOUT);
eae801ba
TR
117 if (errno == ETIMEDOUT)
118 return false;
119 }
120 return true;
121 }
122 }
123
124 void
125 __atomic_futex_unsigned_base::_M_futex_notify_all(unsigned* __addr)
126 {
127 // This syscall can fail for various reasons, including in situations
128 // in which there is no real error. Thus, we don't bother checking
129 // the error codes. See the futex documentation and glibc for background.
130 syscall (SYS_futex, __addr, futex_wake_op, INT_MAX);
131 }
132
8dcf3d3c 133_GLIBCXX_END_NAMESPACE_VERSION
eae801ba 134}
8ba7f29e
JW
135#endif // defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
136#endif // _GLIBCXX_HAS_GTHREADS