QDeadlineTimer: optimize when std::chrono::steady_clock is the same

Change-Id: Ib57b52598e2f452985e9fffd14583173716343b0
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Thiago Macieira 2016-06-15 00:34:10 -07:00
parent 8c8cbd1508
commit c000143ee4
2 changed files with 40 additions and 0 deletions

View File

@ -192,6 +192,34 @@ public:
QPair<qint64, unsigned> _q_data() const { return qMakePair(t1, t2); }
};
#if QT_HAS_INCLUDE(<chrono>) && (defined(Q_OS_DARWIN) || defined(Q_OS_LINUX) || (defined(Q_CC_MSVC) && Q_CC_MSVC >= 1900))
// We know for these OS/compilers that the std::chrono::steady_clock uses the same
// reference time as QDeadlineTimer
template <> inline std::chrono::steady_clock::time_point
QDeadlineTimer::deadline<std::chrono::steady_clock, std::chrono::steady_clock::duration>() const
{
return std::chrono::steady_clock::time_point(std::chrono::nanoseconds(deadlineNSecs()));
}
template <> inline void
QDeadlineTimer::setDeadline<std::chrono::steady_clock, std::chrono::steady_clock::duration>(std::chrono::steady_clock::time_point tp, Qt::TimerType type_)
{
using namespace std::chrono;
if (tp == tp.max()) {
*this = Forever;
type = type_;
} else if (type_ != Qt::PreciseTimer) {
// if we aren't using PreciseTimer, then we need to convert
setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - steady_clock::now()).count(), type_);
} else {
setPreciseDeadline(0,
duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
type_);
}
}
#endif
Q_DECLARE_SHARED(QDeadlineTimer)
QT_END_NAMESPACE

View File

@ -596,6 +596,18 @@ void tst_QDeadlineTimer::stdchrono()
QTRY_VERIFY2_WITH_TIMEOUT(timersExecuted,
"Looks like timers didn't fire on time.", 4 * minResolution);
#if defined(Q_OS_DARWIN) || defined(Q_OS_LINUX) || (defined(Q_CC_MSVC) && Q_CC_MSVC >= 1900)
{
// We know for these OS/compilers that the std::chrono::steady_clock uses the same
// reference time as QDeadlineTimer
qint64 before = duration_cast<nanoseconds>(steady_before.time_since_epoch()).count();
qint64 after = duration_cast<nanoseconds>(steady_after.time_since_epoch()).count();
QVERIFY2(now.deadlineNSecs() > before, QByteArray::number(now.deadlineNSecs()) +
" > " + QByteArray::number(before));
QVERIFY2(now.deadlineNSecs() < after, QByteArray::number(now.deadlineNSecs()) +
" < " + QByteArray::number(after));
}
#endif
{
auto diff = duration_cast<milliseconds>(steady_after - steady_deadline);
QVERIFY2(diff.count() > minResolution / 2, QByteArray::number(qint64(diff.count())));