1 // futex -*- C++ -*-
2 
3 // Copyright (C) 2015-2020 Free Software Foundation, Inc.
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 
25 #include <bits/atomic_futex.h>
26 #ifdef _GLIBCXX_HAS_GTHREADS
27 #if defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
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 <ext/numeric_traits.h>
35 #include <debug/debug.h>
36 
37 // Constants for the wait/wake futex syscall operations
38 const unsigned futex_wait_op = 0;
39 const unsigned futex_wake_op = 1;
40 
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 
45   using __gnu_cxx::__int_traits;
46 
47 namespace
48 {
49   // Return the relative duration from (now_s + now_ns) to (abs_s + abs_ns)
50   // as a timespec.
51   struct timespec
relative_timespec(chrono::seconds abs_s,chrono::nanoseconds abs_ns,time_t now_s,long now_ns)52   relative_timespec(chrono::seconds abs_s, chrono::nanoseconds abs_ns,
53 		    time_t now_s, long now_ns)
54   {
55     struct timespec rt;
56 
57     // Did we already time out?
58     if (now_s > abs_s.count())
59       {
60 	rt.tv_sec = -1;
61 	return rt;
62       }
63 
64     const auto rel_s = abs_s.count() - now_s;
65 
66     // Convert the absolute timeout to a relative timeout, without overflow.
67     if (rel_s > __int_traits<time_t>::__max) [[unlikely]]
68       {
69 	rt.tv_sec = __int_traits<time_t>::__max;
70 	rt.tv_nsec = 999999999;
71       }
72     else
73       {
74 	rt.tv_sec = rel_s;
75 	rt.tv_nsec = abs_ns.count() - now_ns;
76 	if (rt.tv_nsec < 0)
77 	  {
78 	    rt.tv_nsec += 1000000000;
79 	    --rt.tv_sec;
80 	  }
81       }
82 
83     return rt;
84   }
85 } // namespace
86 
87   bool
88   __atomic_futex_unsigned_base::
_M_futex_wait_until(unsigned * __addr,unsigned __val,bool __has_timeout,chrono::seconds __s,chrono::nanoseconds __ns)89   _M_futex_wait_until(unsigned *__addr, unsigned __val, bool __has_timeout,
90 		      chrono::seconds __s, chrono::nanoseconds __ns)
91   {
92     if (!__has_timeout)
93       {
94 	// Ignore whether we actually succeeded to block because at worst,
95 	// we will fall back to spin-waiting.  The only thing we could do
96 	// here on errors is abort.
97 	int ret __attribute__((unused));
98 	ret = syscall (SYS_futex, __addr, futex_wait_op, __val, nullptr);
99 	__glibcxx_assert(ret == 0 || errno == EINTR || errno == EAGAIN);
100 	return true;
101       }
102     else
103       {
104 	struct timeval tv;
105 	gettimeofday (&tv, NULL);
106 
107 	// Convert the absolute timeout value to a relative timeout
108 	auto rt = relative_timespec(__s, __ns, tv.tv_sec, tv.tv_usec * 1000);
109 
110 	// Did we already time out?
111 	if (rt.tv_sec < 0)
112 	  return false;
113 
114 	if (syscall (SYS_futex, __addr, futex_wait_op, __val, &rt) == -1)
115 	  {
116 	    __glibcxx_assert(errno == EINTR || errno == EAGAIN
117 			     || errno == ETIMEDOUT);
118 	    if (errno == ETIMEDOUT)
119 	      return false;
120 	  }
121 	return true;
122       }
123   }
124 
125   void
_M_futex_notify_all(unsigned * __addr)126   __atomic_futex_unsigned_base::_M_futex_notify_all(unsigned* __addr)
127   {
128     // This syscall can fail for various reasons, including in situations
129     // in which there is no real error.  Thus, we don't bother checking
130     // the error codes.  See the futex documentation and glibc for background.
131     syscall (SYS_futex, __addr, futex_wake_op, INT_MAX);
132   }
133 
134 _GLIBCXX_END_NAMESPACE_VERSION
135 }
136 #endif // defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
137 #endif // _GLIBCXX_HAS_GTHREADS
138