src/timer/unix/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6138 4c64952a58fb
child 7137 e4daf38f9bde
permissions -rw-r--r--
Happy New Year!
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #ifdef SDL_TIMER_UNIX
    24 
    25 #include <stdio.h>
    26 #include <sys/time.h>
    27 #include <unistd.h>
    28 #include <errno.h>
    29 
    30 #include "SDL_timer.h"
    31 
    32 /* The clock_gettime provides monotonous time, so we should use it if
    33    it's available. The clock_gettime function is behind ifdef
    34    for __USE_POSIX199309
    35    Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
    36 */
    37 #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
    38 #include <time.h>
    39 #endif
    40 
    41 /* The first ticks value of the application */
    42 #ifdef HAVE_CLOCK_GETTIME
    43 static struct timespec start;
    44 #else
    45 static struct timeval start;
    46 #endif /* HAVE_CLOCK_GETTIME */
    47 
    48 
    49 void
    50 SDL_StartTicks(void)
    51 {
    52     /* Set first ticks value */
    53 #if HAVE_CLOCK_GETTIME
    54     clock_gettime(CLOCK_MONOTONIC, &start);
    55 #else
    56     gettimeofday(&start, NULL);
    57 #endif
    58 }
    59 
    60 Uint32
    61 SDL_GetTicks(void)
    62 {
    63 #if HAVE_CLOCK_GETTIME
    64     Uint32 ticks;
    65     struct timespec now;
    66 
    67     clock_gettime(CLOCK_MONOTONIC, &now);
    68     ticks =
    69         (now.tv_sec - start.tv_sec) * 1000 + (now.tv_nsec -
    70                                               start.tv_nsec) / 1000000;
    71     return (ticks);
    72 #else
    73     Uint32 ticks;
    74     struct timeval now;
    75 
    76     gettimeofday(&now, NULL);
    77     ticks =
    78         (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec -
    79                                               start.tv_usec) / 1000;
    80     return (ticks);
    81 #endif
    82 }
    83 
    84 Uint64
    85 SDL_GetPerformanceCounter(void)
    86 {
    87 #if HAVE_CLOCK_GETTIME
    88     Uint64 ticks;
    89     struct timespec now;
    90 
    91     clock_gettime(CLOCK_MONOTONIC, &now);
    92     ticks = now.tv_sec;
    93     ticks *= 1000000000;
    94     ticks += now.tv_nsec;
    95     return (ticks);
    96 #else
    97     Uint64 ticks;
    98     struct timeval now;
    99 
   100     gettimeofday(&now, NULL);
   101     ticks = now.tv_sec;
   102     ticks *= 1000000;
   103     ticks += now.tv_usec;
   104     return (ticks);
   105 #endif
   106 }
   107 
   108 Uint64
   109 SDL_GetPerformanceFrequency(void)
   110 {
   111 #if HAVE_CLOCK_GETTIME
   112     return 1000000000;
   113 #else
   114     return 1000000;
   115 #endif
   116 }
   117 
   118 void
   119 SDL_Delay(Uint32 ms)
   120 {
   121     int was_error;
   122 
   123 #if HAVE_NANOSLEEP
   124     struct timespec elapsed, tv;
   125 #else
   126     struct timeval tv;
   127     Uint32 then, now, elapsed;
   128 #endif
   129 
   130     /* Set the timeout interval */
   131 #if HAVE_NANOSLEEP
   132     elapsed.tv_sec = ms / 1000;
   133     elapsed.tv_nsec = (ms % 1000) * 1000000;
   134 #else
   135     then = SDL_GetTicks();
   136 #endif
   137     do {
   138         errno = 0;
   139 
   140 #if HAVE_NANOSLEEP
   141         tv.tv_sec = elapsed.tv_sec;
   142         tv.tv_nsec = elapsed.tv_nsec;
   143         was_error = nanosleep(&tv, &elapsed);
   144 #else
   145         /* Calculate the time interval left (in case of interrupt) */
   146         now = SDL_GetTicks();
   147         elapsed = (now - then);
   148         then = now;
   149         if (elapsed >= ms) {
   150             break;
   151         }
   152         ms -= elapsed;
   153         tv.tv_sec = ms / 1000;
   154         tv.tv_usec = (ms % 1000) * 1000;
   155 
   156         was_error = select(0, NULL, NULL, NULL, &tv);
   157 #endif /* HAVE_NANOSLEEP */
   158     } while (was_error && (errno == EINTR));
   159 }
   160 
   161 #endif /* SDL_TIMER_UNIX */
   162 
   163 /* vi: set ts=4 sw=4 expandtab: */