Fix periodic lag/stutter: ensure that tv_nsec<10e9

Each time when "ts.tv_nsec" became greater or equal than 10e9, the cnd_timedwait() function returned immediately, causing the thread to become a busy waiting loop (slowing the other thread, on which the current was waiting, down), until "ts.tv_nsec" became smaller than 10e9.
This caused noticeable stutter one 10th of a second, since the timeout was 100ms.
This commit is contained in:
Elias Vanderstuyft 2016-08-15 17:29:07 +02:00 committed by GitHub
parent f1c536fe13
commit 0f38bf0240

View File

@ -457,7 +457,12 @@ static void draw_particles(GLFWwindow* window, double t, float dt)
{ {
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += 100000000; ts.tv_nsec += 100 * 1000 * 1000;
if (ts.tv_nsec >= 1000 * 1000 * 1000)
{
ts.tv_sec++;
ts.tv_nsec -= 1000 * 1000 * 1000;
}
cnd_timedwait(&thread_sync.p_done, &thread_sync.particles_lock, &ts); cnd_timedwait(&thread_sync.p_done, &thread_sync.particles_lock, &ts);
} }
@ -908,7 +913,12 @@ static int physics_thread_main(void* arg)
{ {
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += 100000000; ts.tv_nsec += 100 * 1000 * 1000;
if (ts.tv_nsec >= 1000 * 1000 * 1000)
{
ts.tv_sec++;
ts.tv_nsec -= 1000 * 1000 * 1000;
}
cnd_timedwait(&thread_sync.d_done, &thread_sync.particles_lock, &ts); cnd_timedwait(&thread_sync.d_done, &thread_sync.particles_lock, &ts);
} }