X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Ftimer.cc;fp=src%2Ftimer.cc;h=2c7a1cbc39e05f3957422cc7de6f7d0f71db6b64;hb=79b5f738f2e38acb60cda7e09f54802933a17105;hp=0000000000000000000000000000000000000000;hpb=a891a2dcbbb63d9e771da6efff00a33da614e737;p=chaz%2Fyoink diff --git a/src/timer.cc b/src/timer.cc new file mode 100644 index 0000000..2c7a1cb --- /dev/null +++ b/src/timer.cc @@ -0,0 +1,101 @@ + +/******************************************************************************* + + 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 +#include +#include + +#include + +#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 +