mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
Added setting of the timer resolution in the Win32 implementation of Sleep and rewrote the Unix Sleep implementation to use nanosleep instead of pthread_cond_timedwait to prevent spurious wakeups from causing the function to return too early.
This commit is contained in:
parent
91e1743516
commit
20db4969c5
@ -74,6 +74,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
|
||||
|
@ -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,17 @@ 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);
|
||||
ti.tv_nsec = (usecs % 1000000) * 1000;
|
||||
ti.tv_sec = usecs / 1000000;
|
||||
|
||||
// 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);
|
||||
while ((nanosleep(&ti, &ti) == -1) && (errno == EINTR))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user