summaryrefslogtreecommitdiff
path: root/src/SFML/System/Unix/SleepImpl.cpp
diff options
context:
space:
mode:
authorJames Cowgill <james410@cowgill.org.uk>2014-12-09 20:21:40 +0000
committerJames Cowgill <james410@cowgill.org.uk>2014-12-09 20:21:40 +0000
commitfa21c65d0c764705cfc377bf0d0de08fac26874e (patch)
treedbc9e87bbd8684d15e79fc0c8b7a8985389c3b35 /src/SFML/System/Unix/SleepImpl.cpp
parentdd835931261c340acd5f0409341d13fa2670423e (diff)
Imported Upstream version 2.2.0+dfsg
Diffstat (limited to 'src/SFML/System/Unix/SleepImpl.cpp')
-rw-r--r--src/SFML/System/Unix/SleepImpl.cpp48
1 files changed, 15 insertions, 33 deletions
diff --git a/src/SFML/System/Unix/SleepImpl.cpp b/src/SFML/System/Unix/SleepImpl.cpp
index d817d58..08d00f8 100644
--- a/src/SFML/System/Unix/SleepImpl.cpp
+++ b/src/SFML/System/Unix/SleepImpl.cpp
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
+// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
@@ -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