#include <folly/ClockGettimeWrappers.h>
#include <folly/CPortability.h>
#include <folly/Likely.h>
#include <folly/portability/Time.h>
#include <chrono>
#include <ctime>
#ifndef _WIN32
#define _GNU_SOURCE …
#include <dlfcn.h>
#endif
namespace folly {
namespace chrono {
static int64_t clock_gettime_ns_fallback(clockid_t clock) { … }
int (*clock_gettime)(clockid_t, timespec* ts) = …;
int64_t (*clock_gettime_ns)(clockid_t) = …;
#if defined(FOLLY_HAVE_LINUX_VDSO) && !defined(FOLLY_SANITIZE_MEMORY)
namespace {
struct VdsoInitializer {
VdsoInitializer() {
m_handle = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
if (!m_handle) {
return;
}
void* p = dlsym(m_handle, "__vdso_clock_gettime");
if (p) {
folly::chrono::clock_gettime = (int (*)(clockid_t, timespec*))p;
}
p = dlsym(m_handle, "__vdso_clock_gettime_ns");
if (p) {
folly::chrono::clock_gettime_ns = (int64_t(*)(clockid_t))p;
}
}
~VdsoInitializer() {
if (m_handle) {
clock_gettime = &::clock_gettime;
clock_gettime_ns = &clock_gettime_ns_fallback;
dlclose(m_handle);
}
}
private:
void* m_handle;
};
const VdsoInitializer vdso_initializer;
}
#endif
}
}