libcruft-util/time_posix.cpp

30 lines
717 B
C++
Raw Normal View History

2015-07-23 14:13:09 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2015-07-23 14:13:09 +10:00
*
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
2015-07-23 14:13:09 +10:00
*/
#include "time.hpp"
2015-07-23 14:13:09 +10:00
#include "cast.hpp"
2015-07-23 14:13:09 +10:00
#include <ctime>
static constexpr uint64_t SECOND = 1'000'000'000UL;
2015-07-23 14:13:09 +10:00
///////////////////////////////////////////////////////////////////////////////
void
util::sleep (uint64_t ns)
{
struct timespec req, rem;
req.tv_sec = util::cast::lossless<time_t> (ns / SECOND);
req.tv_nsec = util::cast::lossless<long > (ns % SECOND);
2015-07-23 14:13:09 +10:00
while (nanosleep (&req, &rem)) {
req = rem;
}
}