Improve perf of Win32 usleep

This commit is contained in:
Zach Pomerantz 2016-05-03 16:25:09 -07:00
parent 3619821b82
commit 526143b097

View file

@ -455,19 +455,29 @@ void printVoxelCode(unsigned char* voxelCode) {
} }
#ifdef _WIN32 #ifdef _WIN32
void usleep(int waitTime) { void usleep(int waitTime) {
const quint64 BUSY_LOOP_USECS = 2000; quint64 sleepUntil = waitTime + usecTimestampNow();
quint64 compTime = waitTime + usecTimestampNow();
quint64 compTimeSleep = compTime - BUSY_LOOP_USECS; // Busy wait with sleep/yield where possible
while (true) { while (true) {
if (usecTimestampNow() < compTimeSleep) { quint64 now = usecTimestampNow();
QThread::msleep(1); if (now >= sleepUntil) {
} break;
if (usecTimestampNow() >= compTime) { }
break;
} // Sleep if we have at least 1ms to spare
const int MIN_SLEEP_USECS = 1000;
// msleep is allowed to overshoot, so give it a 100us berth
const int MIN_SLEEP_USECS_BERTH = 100;
if (sleepUntil - now > (MIN_SLEEP_USECS + MIN_SLEEP_USECS_BERTH)) {
unsigned long sleepFor = (sleepUntil - now - MIN_SLEEP_USECS_BERTH) / USECS_PER_MSEC;
QThread::msleep(sleepFor);
// Yield otherwise
} else {
QThread::yieldCurrentThread();
} }
} }
}
#endif #endif
// Inserts the value and key into three arrays sorted by the key array, the first array is the value, // Inserts the value and key into three arrays sorted by the key array, the first array is the value,