[threading linux] Fix nanosleep using microseconds
Add Sleep Test for 50ms. Fix Sleep under linux that was using microseconds as nanoseconds. Factor timespec creation to template function using div/mod and nanoseconds from duration cast.
This commit is contained in:
parent
6c79c93f2b
commit
d8d8a7dbb8
|
@ -15,6 +15,7 @@ namespace xe {
|
|||
namespace base {
|
||||
namespace test {
|
||||
using namespace threading;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TEST_CASE("Fence") {
|
||||
// TODO(bwrsandman):
|
||||
|
@ -43,8 +44,11 @@ TEST_CASE("Sync with Memory Barrier", "SyncMemory") {
|
|||
}
|
||||
|
||||
TEST_CASE("Sleep Current Thread", "Sleep") {
|
||||
// TODO(bwrsandman):
|
||||
REQUIRE(true);
|
||||
auto wait_time = 50ms;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
Sleep(wait_time);
|
||||
auto duration = std::chrono::steady_clock::now() - start;
|
||||
REQUIRE(duration >= wait_time);
|
||||
}
|
||||
|
||||
TEST_CASE("Sleep Current Thread in Alertable State", "Sleep") {
|
||||
|
|
|
@ -23,6 +23,15 @@
|
|||
namespace xe {
|
||||
namespace threading {
|
||||
|
||||
template <typename _Rep, typename _Period>
|
||||
inline timespec DurationToTimeSpec(
|
||||
std::chrono::duration<_Rep, _Period> duration) {
|
||||
auto nanoseconds =
|
||||
std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
|
||||
auto div = ldiv(nanoseconds.count(), 1000000000L);
|
||||
return timespec{div.quot, div.rem};
|
||||
}
|
||||
|
||||
// TODO(dougvj)
|
||||
void EnableAffinityConfiguration() {}
|
||||
|
||||
|
@ -47,8 +56,7 @@ void MaybeYield() {
|
|||
void SyncMemory() { __sync_synchronize(); }
|
||||
|
||||
void Sleep(std::chrono::microseconds duration) {
|
||||
timespec rqtp = {time_t(duration.count() / 1000000),
|
||||
time_t(duration.count() % 1000)};
|
||||
timespec rqtp = DurationToTimeSpec(duration);
|
||||
nanosleep(&rqtp, nullptr);
|
||||
// TODO(benvanik): spin while rmtp >0?
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue