Changed the Unix implementation of sf::Sleep to a more robust one
This commit is contained in:
parent
bc64da0469
commit
3a732133cb
@ -26,6 +26,7 @@
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Unix/Platform.hpp>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@ -47,7 +48,35 @@ Uint64 Platform::GetSystemTime()
|
||||
////////////////////////////////////////////////////////////
|
||||
void Platform::Sleep(Uint32 time)
|
||||
{
|
||||
usleep(time * 1000);
|
||||
// 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
|
||||
|
||||
// first get the current time
|
||||
timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
// construct the time limit (current time + time to wait)
|
||||
timespec ti;
|
||||
ti.tv_nsec = (tv.tv_usec + (time % 1000) * 1000) * 1000;
|
||||
ti.tv_sec = tv.tv_sec + (time / 1000) + (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);
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
Loading…
Reference in New Issue
Block a user