--- /dev/null
+
+/*******************************************************************************
+
+ Copyright (c) 2009, Charles McGarvey
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*******************************************************************************/
+
+#include <stdexcept>
+#include <ctime>
+#include <cerrno>
+
+#include <SDL/SDL.h>
+
+#include "timer.hh"
+
+
+namespace dc {
+
+
+#if HAVE_LIBRT
+
+scalar ticks()
+{
+ struct timespec ts;
+
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
+ {
+ throw std::runtime_error("cannot access monotonic clock");
+ }
+
+ return scalar(ts.tv_sec) + scalar(ts.tv_nsec) / 1000000000.0;
+}
+
+void sleep(scalar seconds, bool absolute)
+{
+ struct timespec ts;
+ int ret;
+
+ if (!absolute) seconds += ticks();
+ ts.tv_sec = time_t(seconds);
+ ts.tv_nsec = time_t((seconds - scalar(ts.tv_sec)) * 1000000000.0);
+
+ do
+ {
+ ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, 0);
+ }
+ while (ret == -1 && errno == EINTR);
+}
+
+#else // ! HAVE_LIBRT
+
+// If we don't have librt, we'll have to use different timing methods.
+
+scalar ticks()
+{
+ unsigned ms = SDL_GetTicks();
+ return scalar(ms / 1000) + scalar(ms % 1000) / 1000.0;
+}
+
+void sleep(scalar seconds, bool absolute)
+{
+ struct timespec ts;
+ int ret;
+
+ if (absolute) seconds -= ticks();
+ ts.tv_sec = time_t(seconds);
+ ts.tv_nsec = time_t((seconds - scalar(ts.tv_sec)) * 1000000000.0);
+
+ do
+ {
+ ret = nanosleep(&ts, &ts);
+ }
+ while (ret == -1 && errno == EINTR);
+}
+
+#endif // HAVE_LIBRT
+
+
+} // namespace dc
+