Skip to content

Commit

Permalink
Use 'gettimeofday()' instead of 'clock_gettime()' when building for M…
Browse files Browse the repository at this point in the history
…acOSX with a target version < 10.12. Fixes liballeg#1434.
  • Loading branch information
Todd Cope authored and SiegeLord committed May 21, 2023
1 parent 1c7486a commit 88ba729
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/unix/utime.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,34 @@ struct timespec _al_unix_initial_time;



/* clock_gettime() doesn't exist in MacOSX versions < 10.12. Use
* gettimeofday() if building for MacOSX and targeting version < 10.12.
*/
#if defined(ALLEGRO_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
#include <mach/mach_time.h>
int _internal_clock_gettime(clockid_t clock_id, struct timespec* t)
{
struct timeval now;

(void)clock_id;
gettimeofday(&now, NULL);
t->tv_sec = now.tv_sec;
t->tv_nsec = now.tv_usec * 1000;
return 0;
}
#define _al_clock_gettime _internal_clock_gettime
#else
#define _al_clock_gettime clock_gettime
#endif



/* _al_unix_init_time:
* Called by the system driver to mark the beginning of time.
*/
void _al_unix_init_time(void)
{
clock_gettime(CLOCK_REALTIME, &_al_unix_initial_time);
_al_clock_gettime(CLOCK_REALTIME, &_al_unix_initial_time);
}


Expand All @@ -48,7 +70,7 @@ double _al_unix_get_time(void)
struct timespec now;
double time;

clock_gettime(CLOCK_REALTIME, &now);
_al_clock_gettime(CLOCK_REALTIME, &now);
time = (double) (now.tv_sec - _al_unix_initial_time.tv_sec)
+ (double) (now.tv_nsec - _al_unix_initial_time.tv_nsec) * 1.0e-9;
return time;
Expand Down Expand Up @@ -79,7 +101,7 @@ void _al_unix_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds)

ASSERT(ut);

clock_gettime(CLOCK_REALTIME, &now);
_al_clock_gettime(CLOCK_REALTIME, &now);

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

0 comments on commit 88ba729

Please sign in to comment.