| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Unix time module. |
| 12 |
* |
| 13 |
* By Peter Wang. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#include <sys/time.h> |
| 20 |
#include <math.h> |
| 21 |
|
| 22 |
#include "allegro5/altime.h" |
| 23 |
#include "allegro5/debug.h" |
| 24 |
#include "allegro5/platform/aintunix.h" |
| 25 |
#include "allegro5/platform/aintuthr.h" |
| 26 |
|
| 27 |
ALLEGRO_STATIC_ASSERT(utime, |
| 28 |
sizeof(ALLEGRO_TIMEOUT_UNIX) <= sizeof(ALLEGRO_TIMEOUT)); |
| 29 |
|
| 30 |
|
| 31 |
/* Marks the time Allegro was initialised, for al_get_time(). */ |
| 32 |
struct timespec _al_unix_initial_time; |
| 33 |
|
| 34 |
/* clock_gettime() doesn't exist in MacOSX versions < 10.12. Use |
| 35 |
* gettimeofday() if building for MacOSX and targeting version < 10.12. |
| 36 |
*/ |
| 37 |
#if defined(ALLEGRO_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < 101200 |
| 38 |
#include <mach/mach_time.h> |
| 39 |
int _internal_clock_gettime(clockid_t clock_id, struct timespec* t) |
| 40 |
{ |
| 41 |
struct timeval now; |
| 42 |
|
| 43 |
(void)clock_id; |
| 44 |
gettimeofday(&now, NULL); |
| 45 |
t->tv_sec = now.tv_sec; |
| 46 |
t->tv_nsec = now.tv_usec * 1000; |
| 47 |
return 0; |
| 48 |
} |
| 49 |
#define _al_clock_gettime _internal_clock_gettime |
| 50 |
#else |
| 51 |
#define _al_clock_gettime clock_gettime |
| 52 |
#endif |
| 53 |
|
| 54 |
// Prefer clock that never decreases and includes time suspended. |
| 55 |
// Can probably use CLOCK_MONOTONIC_RAW for non-mac too, but remains to be |
| 56 |
// verified. |
| 57 |
#if defined(ALLEGRO_MACOSX) |
| 58 |
#define CLOCK_ID CLOCK_MONOTONIC_RAW |
| 59 |
#else |
| 60 |
#define CLOCK_ID CLOCK_REALTIME |
| 61 |
#endif |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
/* _al_unix_init_time: |
| 66 |
* Called by the system driver to mark the beginning of time. |
| 67 |
*/ |
| 68 |
void _al_unix_init_time(void) |
| 69 |
{ |
| 70 |
_al_clock_gettime(CLOCK_ID, &_al_unix_initial_time); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
double _al_unix_get_time(void) |
| 76 |
{ |
| 77 |
struct timespec now; |
| 78 |
double time; |
| 79 |
|
| 80 |
_al_clock_gettime(CLOCK_ID, &now); |
| 81 |
time = (double) (now.tv_sec - _al_unix_initial_time.tv_sec) |
| 82 |
+ (double) (now.tv_nsec - _al_unix_initial_time.tv_nsec) * 1.0e-9; |
| 83 |
return time; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
void _al_unix_rest(double seconds) |
| 89 |
{ |
| 90 |
struct timespec timeout; |
| 91 |
double integral; |
| 92 |
double frac; |
| 93 |
|
| 94 |
frac = modf(seconds, &integral); |
| 95 |
timeout.tv_sec = (time_t)integral; |
| 96 |
timeout.tv_nsec = (suseconds_t)(frac * 1e9); |
| 97 |
nanosleep(&timeout, 0); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void _al_unix_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds) |
| 103 |
{ |
| 104 |
ALLEGRO_TIMEOUT_UNIX *ut = (ALLEGRO_TIMEOUT_UNIX *) timeout; |
| 105 |
struct timespec now; |
| 106 |
double integral; |
| 107 |
double frac; |
| 108 |
|
| 109 |
ASSERT(ut); |
| 110 |
|
| 111 |
_al_clock_gettime(CLOCK_REALTIME, &now); |
| 112 |
|
| 113 |
if (seconds <= 0.0) { |
| 114 |
ut->abstime.tv_sec = now.tv_sec; |
| 115 |
ut->abstime.tv_nsec = now.tv_nsec; |
| 116 |
} |
| 117 |
else { |
| 118 |
frac = modf(seconds, &integral); |
| 119 |
|
| 120 |
ut->abstime.tv_sec = now.tv_sec + integral; |
| 121 |
ut->abstime.tv_nsec = now.tv_nsec + (frac * 1000000000L); |
| 122 |
ut->abstime.tv_sec += ut->abstime.tv_nsec / 1000000000L; |
| 123 |
ut->abstime.tv_nsec = ut->abstime.tv_nsec % 1000000000L; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/* vim: set sts=3 sw=3 et */ |