time: split off platform units

This commit is contained in:
Danny Robson 2015-07-23 14:13:09 +10:00
parent b6ce75aca6
commit 5d6586636e
4 changed files with 126 additions and 60 deletions

View File

@ -256,13 +256,15 @@ if PLATFORM_LINUX
UTIL_FILES += \ UTIL_FILES += \
backtrace_execinfo.cpp \ backtrace_execinfo.cpp \
io_posix.cpp \ io_posix.cpp \
io_posix.hpp io_posix.hpp \
time_posix.cpp
endif endif
if PLATFORM_WIN32 if PLATFORM_WIN32
UTIL_FILES += \ UTIL_FILES += \
backtrace_null.cpp \ backtrace_null.cpp \
io_win32.cpp io_win32.cpp \
time_win32.cpp
endif endif
############################################################################### ###############################################################################

View File

@ -24,80 +24,40 @@
using namespace util; using namespace util;
// ---------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
static const uint64_t SECOND = 1000000000UL; static const uint64_t SECOND = 1'000'000'000UL;
static const uint64_t MILLISECOND = 1000000UL; static const uint64_t MILLISECOND = 1'000'000UL;
// ----------------------------------------------------------------------------
#ifdef PLATFORM_WIN32
#include <windows.h>
uint64_t
util::nanoseconds (void) {
LARGE_INTEGER freq, count;
QueryPerformanceFrequency (&freq);
QueryPerformanceCounter (&count);
return ((double)count.QuadPart / freq.QuadPart) * SECOND;
}
void ///////////////////////////////////////////////////////////////////////////////
util::sleep (uint64_t ns) {
Sleep (ns / MILLISECOND);
}
#else
#include <ctime>
uint64_t
util::nanoseconds (void) {
struct timespec t;
clock_gettime (CLOCK_MONOTONIC, &t);
CHECK_GT (t.tv_sec, 0);
CHECK_GT (t.tv_nsec, 0);
return static_cast<uint64_t> (t.tv_sec) * SECOND + static_cast<uint64_t> (t.tv_nsec);
}
void
util::sleep (uint64_t ns) {
struct timespec req, rem;
req.tv_sec = sign_cast<time_t> (ns / SECOND);
req.tv_nsec = sign_cast<long> (ns % SECOND);
while (nanosleep (&req, &rem)) {
req = rem;
}
}
#endif
// ----------------------------------------------------------------------------
delta_clock::delta_clock (): delta_clock::delta_clock ():
time { util::nanoseconds (), util::nanoseconds () } time { util::nanoseconds (), util::nanoseconds () }
{ ; } { ; }
//-----------------------------------------------------------------------------
float float
delta_clock::seconds (void) { delta_clock::seconds (void)
{
time.prev = time.curr; time.prev = time.curr;
time.curr = nanoseconds (); time.curr = nanoseconds ();
return (time.curr - time.prev) / static_cast<float> (SECOND); return (time.curr - time.prev) / static_cast<float> (SECOND);
} }
// ----------------------------------------------------------------------------
util::period_query::period_query (float seconds) { ///////////////////////////////////////////////////////////////////////////////
util::period_query::period_query (float seconds)
{
m_time.start = nanoseconds (); m_time.start = nanoseconds ();
m_time.period = static_cast<uint64_t> (seconds * SECOND); m_time.period = static_cast<uint64_t> (seconds * SECOND);
} }
//-----------------------------------------------------------------------------
bool bool
util::period_query::poll (void) { util::period_query::poll (void)
{
uint64_t now = nanoseconds (); uint64_t now = nanoseconds ();
uint64_t diff = now - m_time.start; uint64_t diff = now - m_time.start;
if (diff < m_time.period) if (diff < m_time.period)
@ -108,15 +68,17 @@ util::period_query::poll (void) {
} }
// ---------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
util::rate_limiter::rate_limiter (unsigned rate): util::rate_limiter::rate_limiter (unsigned rate):
m_last (nanoseconds ()), m_last (nanoseconds ()),
m_target (SECOND / rate) m_target (SECOND / rate)
{ ; } { ; }
//-----------------------------------------------------------------------------
void void
util::rate_limiter::poll (void) { util::rate_limiter::poll (void)
{
uint64_t now = nanoseconds (); uint64_t now = nanoseconds ();
uint64_t total = now - m_last; uint64_t total = now - m_last;
@ -127,7 +89,7 @@ util::rate_limiter::poll (void) {
} }
// ---------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
util::polled_duration::polled_duration (std::string name, uint64_t interval): util::polled_duration::polled_duration (std::string name, uint64_t interval):
m_name (name), m_name (name),
m_interval (interval), m_interval (interval),
@ -135,12 +97,15 @@ util::polled_duration::polled_duration (std::string name, uint64_t interval):
{ ; } { ; }
//-----------------------------------------------------------------------------
void void
util::polled_duration::start (void) { util::polled_duration::start (void)
{
m_last = nanoseconds (); m_last = nanoseconds ();
} }
//-----------------------------------------------------------------------------
void void
util::polled_duration::stop (void) { util::polled_duration::stop (void) {
uint64_t now = nanoseconds (); uint64_t now = nanoseconds ();

52
time_posix.cpp Normal file
View File

@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#include "time.hpp"
#include "types/casts.hpp"
#include <ctime>
static const uint64_t SECOND = 1'000'000'000UL;
static const uint64_t MILLISECOND = 1'000'000UL;
///////////////////////////////////////////////////////////////////////////////
uint64_t
util::nanoseconds (void)
{
struct timespec t;
clock_gettime (CLOCK_MONOTONIC, &t);
CHECK_GT (t.tv_sec, 0);
CHECK_GT (t.tv_nsec, 0);
return static_cast<uint64_t> (t.tv_sec) * SECOND + static_cast<uint64_t> (t.tv_nsec);
}
///////////////////////////////////////////////////////////////////////////////
void
util::sleep (uint64_t ns)
{
struct timespec req, rem;
req.tv_sec = sign_cast<time_t> (ns / SECOND);
req.tv_nsec = sign_cast<long> (ns % SECOND);
while (nanosleep (&req, &rem)) {
req = rem;
}
}

47
time_win32.cpp Normal file
View File

@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#include "time.hpp"
#include "platform.hpp"
#if !defined(PLATFORM_WIN32)
#error
#endif
#include <windows.h>
static const uint64_t SECOND = 1'000'000'000UL;
static const uint64_t MILLISECOND = 1'000'000UL;
///////////////////////////////////////////////////////////////////////////////
uint64_t
util::nanoseconds (void)
{
LARGE_INTEGER freq, count;
QueryPerformanceFrequency (&freq);
QueryPerformanceCounter (&count);
return count.QuadPart * SECOND / freq.QuadPart;
}
///////////////////////////////////////////////////////////////////////////////
void
util::sleep (uint64_t ns)
{
Sleep (ns / MILLISECOND);
}