2013-12-12 18:33:29 +01:00
|
|
|
//
|
|
|
|
// Clock.cpp
|
|
|
|
//
|
2013-12-12 21:46:57 +01:00
|
|
|
// $Id: //poco/1.4/Foundation/src/Clock.cpp#3 $
|
2013-12-12 18:33:29 +01:00
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: DateTime
|
|
|
|
// Module: Clock
|
|
|
|
//
|
|
|
|
// Copyright (c) 2013, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person or organization
|
|
|
|
// obtaining a copy of the software and accompanying documentation covered by
|
|
|
|
// this license (the "Software") to use, reproduce, display, distribute,
|
|
|
|
// execute, and transmit the Software, and to prepare derivative works of the
|
|
|
|
// Software, and to permit third-parties to whom the Software is furnished to
|
|
|
|
// do so, all subject to the following:
|
|
|
|
//
|
|
|
|
// The copyright notices in the Software and this entire statement, including
|
|
|
|
// the above license grant, this restriction and the following disclaimer,
|
|
|
|
// must be included in all copies of the Software, in whole or in part, and
|
|
|
|
// all derivative works of the Software, unless such copies or derivative
|
|
|
|
// works are solely in the form of machine-executable object code generated by
|
|
|
|
// a source language processor.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
|
|
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
|
|
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
|
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Clock.h"
|
|
|
|
#include "Poco/Exception.h"
|
2013-12-12 21:46:57 +01:00
|
|
|
#include "Poco/Timestamp.h"
|
2013-12-12 18:33:29 +01:00
|
|
|
#if defined(__MACH__)
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#elif defined(POCO_OS_FAMILY_UNIX)
|
|
|
|
#include <time.h>
|
|
|
|
#elif defined(POCO_VXWORKS)
|
|
|
|
#include <timers.h>
|
|
|
|
#elif defined(POCO_OS_FAMILY_WINDOWS)
|
|
|
|
#include "Poco/UnWindows.h"
|
|
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
Clock::Clock()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock::Clock(ClockVal tv)
|
|
|
|
{
|
|
|
|
_clock = tv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock::Clock(const Clock& other)
|
|
|
|
{
|
|
|
|
_clock = other._clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock::~Clock()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock& Clock::operator = (const Clock& other)
|
|
|
|
{
|
|
|
|
_clock = other._clock;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock& Clock::operator = (ClockVal tv)
|
|
|
|
{
|
|
|
|
_clock = tv;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Clock::swap(Clock& timestamp)
|
|
|
|
{
|
|
|
|
std::swap(_clock, timestamp._clock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Clock::update()
|
|
|
|
{
|
|
|
|
#if defined(POCO_OS_FAMILY_WINDOWS)
|
|
|
|
|
|
|
|
LARGE_INTEGER perfCounter;
|
|
|
|
LARGE_INTEGER perfFreq;
|
|
|
|
if (QueryPerformanceCounter(&perfCounter) && QueryPerformanceFrequency(&perfFreq))
|
|
|
|
{
|
|
|
|
_clock = perfCounter.QuadPart*resolution()/perfFreq.QuadPart;
|
|
|
|
}
|
|
|
|
else throw Poco::SystemException("cannot get system clock");
|
|
|
|
|
|
|
|
#elif defined(__MACH__)
|
|
|
|
|
|
|
|
clock_serv_t cs;
|
|
|
|
mach_timespec_t ts;
|
|
|
|
|
|
|
|
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
|
|
|
|
clock_get_time(cs, &ts);
|
|
|
|
mach_port_deallocate(mach_task_self(), cs);
|
|
|
|
|
|
|
|
_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
|
|
|
|
|
|
|
|
#elif defined(POCO_VXWORKS)
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
#if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
|
|
|
throw SystemException("cannot get system clock");
|
|
|
|
#else
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts))
|
|
|
|
throw SystemException("cannot get system clock");
|
|
|
|
#endif
|
|
|
|
_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
|
|
|
|
|
2013-12-12 21:46:57 +01:00
|
|
|
#elif (defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)) || defined(__QNX__)
|
2013-12-12 18:33:29 +01:00
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
|
|
|
throw SystemException("cannot get system clock");
|
|
|
|
_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
Poco::Timestamp now;
|
|
|
|
_clock = now.epochMicroseconds();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock::ClockVal Clock::accuracy()
|
|
|
|
{
|
|
|
|
#if defined(POCO_OS_FAMILY_WINDOWS)
|
|
|
|
|
|
|
|
LARGE_INTEGER perfFreq;
|
|
|
|
if (QueryPerformanceFrequency(&perfFreq) && perfFreq.QuadPart > 0)
|
|
|
|
{
|
|
|
|
ClockVal acc = resolution()/perfFreq.QuadPart;
|
|
|
|
return acc > 0 ? acc : 1;
|
|
|
|
}
|
|
|
|
else throw Poco::SystemException("cannot get system clock accuracy");
|
|
|
|
|
|
|
|
#elif defined(__MACH__)
|
|
|
|
|
|
|
|
clock_serv_t cs;
|
|
|
|
int nanosecs;
|
|
|
|
mach_msg_type_number_t n = 1;
|
|
|
|
|
|
|
|
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
|
|
|
|
clock_get_attributes(cs, CLOCK_GET_TIME_RES, (clock_attr_t)&nanosecs, &n);
|
|
|
|
mach_port_deallocate(mach_task_self(), cs);
|
|
|
|
|
|
|
|
ClockVal acc = nanosecs/1000;
|
|
|
|
return acc > 0 ? acc : 1;
|
|
|
|
|
|
|
|
#elif defined(POCO_VXWORKS)
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
#if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
|
|
|
|
if (clock_getres(CLOCK_MONOTONIC, &ts))
|
|
|
|
throw SystemException("cannot get system clock");
|
|
|
|
#else
|
|
|
|
if (clock_getres(CLOCK_REALTIME, &ts))
|
|
|
|
throw SystemException("cannot get system clock");
|
|
|
|
#endif
|
|
|
|
ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
|
|
|
|
return acc > 0 ? acc : 1;
|
|
|
|
|
|
|
|
#elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_getres(CLOCK_MONOTONIC, &ts))
|
|
|
|
throw SystemException("cannot get system clock");
|
|
|
|
|
|
|
|
ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
|
|
|
|
return acc > 0 ? acc : 1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return 1000;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Clock::monotonic()
|
|
|
|
{
|
|
|
|
#if defined(POCO_OS_FAMILY_WINDOWS)
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#elif defined(__MACH__)
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#elif defined(POCO_VXWORKS)
|
|
|
|
|
|
|
|
#if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|