Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make windows sleep consistent with non-windows sleep #2382

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/libponyrt/sched/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,34 +297,55 @@ void ponyint_cpu_core_pause(uint64_t tsc, uint64_t tsc2, bool yield)
if((tsc2 - tsc) < 10000000)
return;

#ifndef PLATFORM_IS_WINDOWS
#ifdef PLATFORM_IS_WINDOWS
DWORD ts = 0;
#else
struct timespec ts = {0, 0};
#endif

if(yield)
{
// A billion cycles is roughly half a second, depending on clock speed.
if((tsc2 - tsc) > 10000000000)
{
// If it has been 10 billion cycles, pause 30 ms.
#ifdef PLATFORM_IS_WINDOWS
ts = 30;
#else
ts.tv_nsec = 30000000;
#endif
} else if((tsc2 - tsc) > 3000000000) {
// If it has been 3 billion cycles, pause 10 ms.
#ifdef PLATFORM_IS_WINDOWS
ts = 10;
#else
ts.tv_nsec = 10000000;
#endif
} else if((tsc2 - tsc) > 1000000000) {
// If it has been 1 billion cycles, pause 1 ms.
#ifdef PLATFORM_IS_WINDOWS
ts = 1;
#else
ts.tv_nsec = 1000000;
#endif
}
else
{
#ifdef PLATFORM_IS_WINDOWS
// Otherwise, pause for 1 ms (minimum on windows)
ts = 1;
#else
// Otherwise, pause for 100 microseconds
ts.tv_nsec = 100000;
#endif
}
}

#ifdef PLATFORM_IS_WINDOWS
Sleep(ts);
#else
DTRACE1(CPU_NANOSLEEP, ts.tv_nsec);
nanosleep(&ts, NULL);
#else
Sleep(yield ? 1 : 0);
#endif
}

Expand Down