Beispiel #1
0
void spring_time::sleep()
{
    // for very short time intervals use a yielding loop (yield is ~5x more accurate than sleep(), check the UnitTest)
    if (toMilliSecsf() < (avgSleepErrMs + avgYieldMs * 5.0f)) {
        const spring_time s = gettime();
        while ((gettime() - s) < *this) {
            thread_yield();
        }
        return;
    }

    const spring_time expectedWakeUpTime = gettime() + *this;

#if defined(SPRINGTIME_USING_STD_SLEEP)
    this_thread::sleep_for(chrono::nanoseconds(toNanoSecsi()));
#else
    boost::this_thread::sleep(boost::posix_time::microseconds(std::ceil(toNanoSecsf() * 1e-3)));
#endif

    const float diffMs = (gettime() - expectedWakeUpTime).toMilliSecsf();
    avgSleepErrMs = avgSleepErrMs * 0.9f + diffMs * 0.1f;

    //if (diffMs > 7.0f) {
    //	LOG_L(L_WARNING, "SpringTime: used sleep() function is too inaccurate");
    //}
}
void spring_time::sleep()
{
	// for very short time intervals use a yielding loop (yield is ~5x more accurate than sleep(), check the UnitTest)
	if (toMilliSecsf() < (avgThreadSleepTimeMilliSecs + avgThreadYieldTimeMilliSecs * 5.0f)) {
		const spring_time s = gettime();

		while ((gettime() - s) < *this)
			thread_yield();

		return;
	}

	// expected wakeup time
	const spring_time t0 = gettime() + *this;

	#if defined(SPRINGTIME_USING_STD_SLEEP)
		this_thread::sleep_for(chrono::nanoseconds(toNanoSecsi()));
	#else
		boost::this_thread::sleep(boost::posix_time::microseconds(std::ceil(toNanoSecsf() * 1e-3)));
	#endif

	const spring_time t1 = gettime();
	const spring_time dt = t1 - t0;

	if (t1 >= t0) {
		boost::mutex::scoped_lock lock(sleepTimeMutex);
		avgThreadSleepTimeMilliSecs = mix(avgThreadSleepTimeMilliSecs, dt.toMilliSecsf(), 0.1f);
	}
}