Skip to content

Commit 88ba729

Browse files
Todd CopeSiegeLord
authored andcommitted
Use 'gettimeofday()' instead of 'clock_gettime()' when building for MacOSX with a target version < 10.12. Fixes liballeg#1434.
1 parent 1c7486a commit 88ba729

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/unix/utime.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,34 @@ struct timespec _al_unix_initial_time;
3333

3434

3535

36+
/* clock_gettime() doesn't exist in MacOSX versions < 10.12. Use
37+
* gettimeofday() if building for MacOSX and targeting version < 10.12.
38+
*/
39+
#if defined(ALLEGRO_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
40+
#include <mach/mach_time.h>
41+
int _internal_clock_gettime(clockid_t clock_id, struct timespec* t)
42+
{
43+
struct timeval now;
44+
45+
(void)clock_id;
46+
gettimeofday(&now, NULL);
47+
t->tv_sec = now.tv_sec;
48+
t->tv_nsec = now.tv_usec * 1000;
49+
return 0;
50+
}
51+
#define _al_clock_gettime _internal_clock_gettime
52+
#else
53+
#define _al_clock_gettime clock_gettime
54+
#endif
55+
56+
57+
3658
/* _al_unix_init_time:
3759
* Called by the system driver to mark the beginning of time.
3860
*/
3961
void _al_unix_init_time(void)
4062
{
41-
clock_gettime(CLOCK_REALTIME, &_al_unix_initial_time);
63+
_al_clock_gettime(CLOCK_REALTIME, &_al_unix_initial_time);
4264
}
4365

4466

@@ -48,7 +70,7 @@ double _al_unix_get_time(void)
4870
struct timespec now;
4971
double time;
5072

51-
clock_gettime(CLOCK_REALTIME, &now);
73+
_al_clock_gettime(CLOCK_REALTIME, &now);
5274
time = (double) (now.tv_sec - _al_unix_initial_time.tv_sec)
5375
+ (double) (now.tv_nsec - _al_unix_initial_time.tv_nsec) * 1.0e-9;
5476
return time;
@@ -79,7 +101,7 @@ void _al_unix_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds)
79101

80102
ASSERT(ut);
81103

82-
clock_gettime(CLOCK_REALTIME, &now);
104+
_al_clock_gettime(CLOCK_REALTIME, &now);
83105

84106
if (seconds <= 0.0) {
85107
ut->abstime.tv_sec = now.tv_sec;

0 commit comments

Comments
 (0)