Skip to content

Commit

Permalink
Merge pull request SFML#475 from binary1248/timer_resolution_fix
Browse files Browse the repository at this point in the history
Increased the resolution of sf::sleep on Windows, improved the implementation of sf:sleep on Linux
LaurentGomila committed Oct 7, 2013
2 parents 42cf79b + 50332a8 commit 5931236
Showing 3 changed files with 28 additions and 32 deletions.
3 changes: 3 additions & 0 deletions src/SFML/System/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -77,6 +77,9 @@ endif()
if(LINUX)
set(SYSTEM_EXT_LIBS ${SYSTEM_EXT_LIBS} rt)
endif()
if(WINDOWS)
set(SYSTEM_EXT_LIBS ${SYSTEM_EXT_LIBS} winmm)
endif()

# define the sfml-system target
sfml_add_library(sfml-system
46 changes: 14 additions & 32 deletions src/SFML/System/Unix/SleepImpl.cpp
Original file line number Diff line number Diff line change
@@ -26,9 +26,8 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Unix/SleepImpl.hpp>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>


namespace sf
@@ -38,38 +37,21 @@ namespace priv
////////////////////////////////////////////////////////////
void sleepImpl(Time time)
{
// usleep is not reliable enough (it might block the
// whole process instead of just the current thread)
// so we must use pthread_cond_timedwait instead

// this implementation is inspired from Qt

Uint64 usecs = time.asMicroseconds();

// get the current time
timeval tv;
gettimeofday(&tv, NULL);

// construct the time limit (current time + time to wait)
// Construct the time to wait
timespec ti;
ti.tv_nsec = (tv.tv_usec + (usecs % 1000000)) * 1000;
ti.tv_sec = tv.tv_sec + (usecs / 1000000) + (ti.tv_nsec / 1000000000);
ti.tv_nsec %= 1000000000;

// create a mutex and thread condition
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, 0);
pthread_cond_t condition;
pthread_cond_init(&condition, 0);

// wait...
pthread_mutex_lock(&mutex);
pthread_cond_timedwait(&condition, &mutex, &ti);
pthread_mutex_unlock(&mutex);

// destroy the mutex and condition
pthread_cond_destroy(&condition);
pthread_mutex_destroy(&mutex);
ti.tv_nsec = (usecs % 1000000) * 1000;
ti.tv_sec = usecs / 1000000;

// Wait...
// If nanosleep returns -1, we check errno. If it is EINTR
// nanosleep was interrupted and has set ti to the remaining
// duration. We continue sleeping until the complete duration
// has passed. We stop sleeping if it was due to an error.
while ((nanosleep(&ti, &ti) == -1) && (errno == EINTR))
{
}
}

} // namespace priv
11 changes: 11 additions & 0 deletions src/SFML/System/Win32/SleepImpl.cpp
Original file line number Diff line number Diff line change
@@ -36,7 +36,18 @@ namespace priv
////////////////////////////////////////////////////////////
void sleepImpl(Time time)
{
// Get the supported timer resolutions on this system
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));

// Set the timer resolution to the minimum for the Sleep call
timeBeginPeriod(tc.wPeriodMin);

// Wait...
::Sleep(time.asMilliseconds());

// Reset the timer resolution back to the system default
timeEndPeriod(tc.wPeriodMin);
}

} // namespace priv

0 comments on commit 5931236

Please sign in to comment.