AuroraRuntime/Source/Threading/WaitFor.hpp
2021-06-27 22:25:29 +01:00

48 lines
2.4 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WaitFor.hpp
Date: 2021-6-12
Author: Reece
***/
#pragma once
namespace Aurora::Threading
{
// the original idea:
// It's not insane to expect slow linux kernels to run at 250 jiffies a second, so, 4ms
// It's also not insane to expect a complete context swap/rescheduled yield on windows to last 15ms
// -> if sleep time greater than 15ms, yield to nt Sleep
// -> if sleep time greater than 4ms, yield to linux kernel
// -> if sleep time greater than 2ms (?), yield to SwitchToThread
// -> SPIIIIIN
static const AuUInt64 kPredictedLinuxKernelJiffies = 250; // some kernel builds go up to 1000
static const AuUInt64 kPredictedLinuxKernelTimeMilli = (1000 / kPredictedLinuxKernelJiffies);
static const AuUInt64 kPredictedLinuxKernelTimeMicro = kPredictedLinuxKernelTimeMilli * 1000;
static const AuUInt64 kPredictedLinuxKernelTimeNano = kPredictedLinuxKernelTimeMilli * 1000000;
static const AuUInt64 kLinuxYieldTimeNano = 1e+6 / 150; // completely arbitrary
static const AuUInt64 kLinuxYieldTimeThresNano = 1e+6 / 25; // completely arbitrary
static const AuUInt64 kPredictedLinuxKernelTimeRTNano = (kLinuxYieldTimeNano + kPredictedLinuxKernelTimeNano) * 3;
//static const AuUInt64 kPredictedNTOSSwitchTimeMS = 10;
//static const AuUInt64 kPredictedNTOSSwitchTimeYDMS = kPredictedNTOSSwitchTimeMS / 4;
//static const AuUInt64 kPredictedNTOSSwitchTimeRTMS = kPredictedNTOSSwitchTimeMS + kPredictedNTOSSwitchTimeMS;
static const AuUInt64 kPredictedNTOSSwitchTimeRTNS = 1000000;// kPredictedNTOSSwitchTimeRTMS* 1000000;
//static const AuUInt64 kPredictedNTOSSwitchTimeNS = 3* 1000000;
static const AuUInt64 kPredictedNTOSSwitchTimeYDNS = 1000000 / 4;// kPredictedNTOSSwitchTimeNS / 4;
static const AuMach kYieldFlagsNone = 0;
static const AuMach kYieldFlagsRemoved = 1;
static const AuMach kYieldFlagsContextSwitchASAP = 2;
static const AuMach kYieldFlagsContextSwitchForever = 4;
static const AuMach kYieldFlagsRegular = kYieldFlagsContextSwitchASAP | kYieldFlagsContextSwitchForever;
template<AuMach yieldFaster>
void FastSnooze(long &count, AuUInt64 &startTime, AuUInt64 maxStallMS);
bool YieldPoll(bool permitMultipleContextSwitches, AuUInt64 timeoutMs, Aurora::Threading::PollCallback_cb cb);
void YieldToOtherThread();
}