Skip to content

Commit babd3ae

Browse files
committed
[libc++] Remove workarounds for the lack of clock_gettime on older macOS platforms
This increases the Mac OS requirement for building libc++ to 10.12. Note that it doesn't change whether the *headers* still support older platforms -- it's only that macOS >= 10.12 is required to build the dylib from sources. Differential Revision: https://reviews.llvm.org/D74489
1 parent 81cca98 commit babd3ae

File tree

4 files changed

+55
-118
lines changed

4 files changed

+55
-118
lines changed

libcxx/docs/index.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,46 @@ reasons, but some of the major ones are:
8585
Platform and Compiler Support
8686
-----------------------------
8787

88-
libc++ is known to work on the following platforms, using gcc and
89-
clang.
90-
Note that functionality provided by ``<atomic>`` is only functional with clang
91-
and GCC.
88+
For using the libc++ headers
89+
############################
90+
The libc++ headers are known to work on the following platforms, using GCC and
91+
Clang. Note that functionality provided by ``<atomic>`` is only functional with
92+
Clang and GCC.
93+
94+
============ ==================== ============
95+
OS Arch Compilers
96+
============ ==================== ============
97+
macOS 10.9+ i386, x86_64 Clang, GCC
98+
FreeBSD 10+ i386, x86_64, ARM Clang, GCC
99+
Linux i386, x86_64 Clang, GCC
100+
============ ==================== ============
101+
102+
The following minimum compiler versions are required:
103+
104+
* Clang 4.0 and above
105+
* GCC 5.0 and above.
106+
107+
The C++03 dialect is only supported with Clang.
108+
109+
For building the libc++ library
110+
###############################
111+
Building the libc++ library (static or shared) requires some features from
112+
the operating system. As such, it has its own set of (slightly different)
113+
system requirements.
92114

93115
============ ==================== ============ ========================
94116
OS Arch Compilers ABI Library
95117
============ ==================== ============ ========================
96-
macOS i386, x86_64 Clang, GCC libc++abi
118+
macOS 10.12+ i386, x86_64 Clang, GCC libc++abi
97119
FreeBSD 10+ i386, x86_64, ARM Clang, GCC libcxxrt, libc++abi
98120
Linux i386, x86_64 Clang, GCC libc++abi
99121
============ ==================== ============ ========================
100122

101-
The following minimum compiler versions are strongly recommended.
123+
The following minimum compiler versions are required:
102124

103125
* Clang 4.0 and above
104126
* GCC 5.0 and above.
105127

106-
The C++03 dialect is only supported for Clang compilers.
107128

108129
C++ Dialect Support
109130
---------------------

libcxx/src/chrono.cpp

Lines changed: 23 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "chrono"
1010
#include "cerrno" // errno
1111
#include "system_error" // __throw_system_error
12-
#include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
12+
#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
1313
#include "include/apple_availability.h"
1414

1515
#if __has_include(<unistd.h>)
@@ -21,28 +21,20 @@
2121
#endif
2222

2323
#if defined(_LIBCPP_WIN32API)
24-
#define WIN32_LEAN_AND_MEAN
25-
#define VC_EXTRA_LEAN
26-
#include <windows.h>
27-
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
28-
#include <winapifamily.h>
29-
#endif
24+
# define WIN32_LEAN_AND_MEAN
25+
# define VC_EXTRA_LEAN
26+
# include <windows.h>
27+
# if _WIN32_WINNT >= _WIN32_WINNT_WIN8
28+
# include <winapifamily.h>
29+
# endif
3030
#else
31-
#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
32-
#include <sys/time.h> // for gettimeofday and timeval
33-
#endif
31+
# if !defined(CLOCK_REALTIME)
32+
# include <sys/time.h> // for gettimeofday and timeval
33+
# endif // !defined(CLOCK_REALTIME)
3434
#endif // defined(_LIBCPP_WIN32API)
3535

36-
#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
37-
#if __APPLE__
38-
#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
39-
#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
40-
#error "Monotonic clock not implemented"
41-
#endif
42-
#endif
43-
4436
#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
45-
#pragma comment(lib, "rt")
37+
# pragma comment(lib, "rt")
4638
#endif
4739

4840
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -82,7 +74,7 @@ system_clock::now() _NOEXCEPT
8274
static_cast<__int64>(ft.dwLowDateTime)};
8375
return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
8476
#else
85-
#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
77+
#if defined(CLOCK_REALTIME)
8678
struct timespec tp;
8779
if (0 != clock_gettime(CLOCK_REALTIME, &tp))
8880
__throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
@@ -91,7 +83,7 @@ system_clock::now() _NOEXCEPT
9183
timeval tv;
9284
gettimeofday(&tv, 0);
9385
return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
94-
#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
86+
#endif // CLOCK_REALTIME
9587
#endif
9688
}
9789

@@ -118,8 +110,15 @@ const bool steady_clock::is_steady;
118110

119111
#if defined(__APPLE__)
120112

121-
// Darwin libc versions >= 1133 provide ns precision via CLOCK_MONOTONIC_RAW
122-
#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
113+
#if !defined(CLOCK_MONOTONIC_RAW)
114+
# error "Building libc++ on Apple platforms requires CLOCK_MONOTONIC_RAW"
115+
#endif
116+
117+
// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
118+
// mach_absolute_time are able to time functions in the nanosecond range.
119+
// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
120+
// also counts cycles when the system is asleep. Thus, it is the only
121+
// acceptable implementation of steady_clock.
123122
steady_clock::time_point
124123
steady_clock::now() _NOEXCEPT
125124
{
@@ -129,60 +128,6 @@ steady_clock::now() _NOEXCEPT
129128
return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
130129
}
131130

132-
#else
133-
// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
134-
// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
135-
// are run time constants supplied by the OS. This clock has no relationship
136-
// to the Gregorian calendar. It's main use is as a high resolution timer.
137-
138-
// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
139-
// for that case as an optimization.
140-
141-
static
142-
steady_clock::rep
143-
steady_simplified()
144-
{
145-
return static_cast<steady_clock::rep>(mach_absolute_time());
146-
}
147-
148-
static
149-
double
150-
compute_steady_factor()
151-
{
152-
mach_timebase_info_data_t MachInfo;
153-
mach_timebase_info(&MachInfo);
154-
return static_cast<double>(MachInfo.numer) / MachInfo.denom;
155-
}
156-
157-
static
158-
steady_clock::rep
159-
steady_full()
160-
{
161-
static const double factor = compute_steady_factor();
162-
return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
163-
}
164-
165-
typedef steady_clock::rep (*FP)();
166-
167-
static
168-
FP
169-
init_steady_clock()
170-
{
171-
mach_timebase_info_data_t MachInfo;
172-
mach_timebase_info(&MachInfo);
173-
if (MachInfo.numer == MachInfo.denom)
174-
return &steady_simplified;
175-
return &steady_full;
176-
}
177-
178-
steady_clock::time_point
179-
steady_clock::now() _NOEXCEPT
180-
{
181-
static FP fp = init_steady_clock();
182-
return time_point(duration(fp()));
183-
}
184-
#endif // defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
185-
186131
#elif defined(_LIBCPP_WIN32API)
187132

188133
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
@@ -210,13 +155,6 @@ steady_clock::now() _NOEXCEPT
210155

211156
#elif defined(CLOCK_MONOTONIC)
212157

213-
// On Apple platforms only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
214-
// mach_absolute_time are able to time functions in the nanosecond range.
215-
// Thus, they are the only acceptable implementations of steady_clock.
216-
#ifdef __APPLE__
217-
#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
218-
#endif
219-
220158
steady_clock::time_point
221159
steady_clock::now() _NOEXCEPT
222160
{
@@ -227,7 +165,7 @@ steady_clock::now() _NOEXCEPT
227165
}
228166

229167
#else
230-
#error "Monotonic clock not implemented"
168+
# error "Monotonic clock not implemented"
231169
#endif
232170

233171
#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK

libcxx/src/filesystem/operations.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@
3636
#define _LIBCPP_USE_COPYFILE
3737
#endif
3838

39-
#if !defined(__APPLE__) && _POSIX_TIMERS > 0
40-
#define _LIBCPP_USE_CLOCK_GETTIME
41-
#endif
42-
43-
#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
39+
#if !defined(CLOCK_REALTIME)
4440
#include <sys/time.h> // for gettimeofday and timeval
45-
#endif // !defined(CLOCK_REALTIME)
41+
#endif // !defined(CLOCK_REALTIME)
4642

4743
#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
4844
#pragma comment(lib, "rt")
@@ -490,7 +486,7 @@ const bool _FilesystemClock::is_steady;
490486

491487
_FilesystemClock::time_point _FilesystemClock::now() noexcept {
492488
typedef chrono::duration<rep> __secs;
493-
#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
489+
#if defined(CLOCK_REALTIME)
494490
typedef chrono::duration<rep, nano> __nsecs;
495491
struct timespec tp;
496492
if (0 != clock_gettime(CLOCK_REALTIME, &tp))
@@ -502,7 +498,7 @@ _FilesystemClock::time_point _FilesystemClock::now() noexcept {
502498
timeval tv;
503499
gettimeofday(&tv, 0);
504500
return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
505-
#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
501+
#endif // CLOCK_REALTIME
506502
}
507503

508504
filesystem_error::~filesystem_error() {}

libcxx/src/include/apple_availability.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@
2828
#endif
2929
#endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
3030

31-
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
32-
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
33-
#define _LIBCPP_USE_CLOCK_GETTIME
34-
#endif
35-
#elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
36-
#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 100000
37-
#define _LIBCPP_USE_CLOCK_GETTIME
38-
#endif
39-
#elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
40-
#if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 100000
41-
#define _LIBCPP_USE_CLOCK_GETTIME
42-
#endif
43-
#elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
44-
#if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 30000
45-
#define _LIBCPP_USE_CLOCK_GETTIME
46-
#endif
47-
#endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
48-
4931
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
5032
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500
5133
#define _LIBCPP_USE_ULOCK

0 commit comments

Comments
 (0)