Commit 8a7112c6 authored by Michael Lee's avatar Michael Lee Committed by Facebook Github Bot

Fix up and namespace clock_gettime and clock_getres for MacOS

Summary:
Resolve two issues with folly's clock_gettime() shim on Apple platforms (iOS, macOS, etc.):

When detecting whether folly should define clock_gettime() on Apple platforms, don't assume clock_gettime() is declared in Apple's headers just because MAC_OS_X_VERSION_10_12/__IPHONE_10_0 is defined. Someone might use a newer macOS or iOS SDK (which defines MAC_OS_X_VERSION_10_12/__IPHONE_10_0), but not have clock_gettime() (because _DARWIN_FEATURE_CLOCK_GETTIME isn't set).

When defining a shim for clock_gettime(), don't rely on Apple's clock_gettime() declaration. Instead, shadow Apple's declaration with our own. This prevents folly from inheriting Apple's availability and visibility attributes for clock_gettime().

Reviewed By: yfeldblum

Differential Revision: D5312574

fbshipit-source-id: f16634ce92ee6c299613d3db22f0537bd31ac14d
parent 4c7a736d
...@@ -43,6 +43,10 @@ static void duration_to_ts( ...@@ -43,6 +43,10 @@ static void duration_to_ts(
#include <mach/thread_act.h> #include <mach/thread_act.h>
#include <mach/vm_map.h> #include <mach/vm_map.h>
namespace folly {
namespace portability {
namespace time {
static std::chrono::nanoseconds time_value_to_ns(time_value_t t) { static std::chrono::nanoseconds time_value_to_ns(time_value_t t) {
return std::chrono::seconds(t.seconds) + return std::chrono::seconds(t.seconds) +
std::chrono::microseconds(t.microseconds); std::chrono::microseconds(t.microseconds);
...@@ -136,6 +140,10 @@ int clock_getres(clockid_t clk_id, struct timespec* ts) { ...@@ -136,6 +140,10 @@ int clock_getres(clockid_t clk_id, struct timespec* ts) {
return 0; return 0;
} }
} // namespace time
} // namespace portability
} // namespace folly
#elif defined(_WIN32) #elif defined(_WIN32)
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
...@@ -144,6 +152,10 @@ int clock_getres(clockid_t clk_id, struct timespec* ts) { ...@@ -144,6 +152,10 @@ int clock_getres(clockid_t clk_id, struct timespec* ts) {
#include <folly/portability/Windows.h> #include <folly/portability/Windows.h>
namespace folly {
namespace portability {
namespace time {
using unsigned_nanos = std::chrono::duration<uint64_t, std::nano>; using unsigned_nanos = std::chrono::duration<uint64_t, std::nano>;
static unsigned_nanos filetimeToUnsignedNanos(FILETIME ft) { static unsigned_nanos filetimeToUnsignedNanos(FILETIME ft) {
...@@ -272,6 +284,10 @@ extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) { ...@@ -272,6 +284,10 @@ extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
return -1; return -1;
} }
} }
} // namespace time
} // namespace portability
} // namespace folly
#else #else
#error No clock_gettime(3) compatibility wrapper available for this platform. #error No clock_gettime(3) compatibility wrapper available for this platform.
#endif #endif
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <time.h> #include <time.h>
#include <folly/portability/Config.h> #include <folly/portability/Config.h>
#include <folly/Portability.h>
// OSX is a pain. The XCode 8 SDK always declares clock_gettime // OSX is a pain. The XCode 8 SDK always declares clock_gettime
// even if the target OS version doesn't support it, so you get // even if the target OS version doesn't support it, so you get
...@@ -27,15 +28,10 @@ ...@@ -27,15 +28,10 @@
// solve that by pretending we have it here in the header and // solve that by pretending we have it here in the header and
// then enable our implementation on the source side so that // then enable our implementation on the source side so that
// gets linked in instead. // gets linked in instead.
#if __MACH__ && \ #if __MACH__ && FOLLY_HAVE_CLOCK_GETTIME && \
(MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 || \ (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 || \
__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0) __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0)
#ifdef FOLLY_HAVE_CLOCK_GETTIME
#undef FOLLY_HAVE_CLOCK_GETTIME
#endif
#define FOLLY_HAVE_CLOCK_GETTIME 1
#define FOLLY_FORCE_CLOCK_GETTIME_DEFINITION 1 #define FOLLY_FORCE_CLOCK_GETTIME_DEFINITION 1
#endif #endif
...@@ -47,10 +43,25 @@ ...@@ -47,10 +43,25 @@
#define CLOCK_MONOTONIC 1 #define CLOCK_MONOTONIC 1
#define CLOCK_PROCESS_CPUTIME_ID 2 #define CLOCK_PROCESS_CPUTIME_ID 2
#define CLOCK_THREAD_CPUTIME_ID 3 #define CLOCK_THREAD_CPUTIME_ID 3
#endif
#if (!FOLLY_HAVE_CLOCK_GETTIME || FOLLY_FORCE_CLOCK_GETTIME_DEFINITION) && (defined(__MACH__) || defined(_WIN32))
namespace folly {
namespace portability {
namespace time {
typedef uint8_t clockid_t; typedef uint8_t clockid_t;
extern "C" int clock_gettime(clockid_t clk_id, struct timespec* ts); int clock_gettime(clockid_t clk_id, struct timespec* ts);
extern "C" int clock_getres(clockid_t clk_id, struct timespec* ts); int clock_getres(clockid_t clk_id, struct timespec* ts);
}
}
}
FOLLY_PUSH_WARNING
#if __CLANG_PREREQ(3, 0)
FOLLY_GCC_DISABLE_WARNING("-Wheader-hygiene")
#endif
/* using override */ using namespace folly::portability::time;
FOLLY_POP_WARNING
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment