We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2411

Summary: Even if built with --enable-clock_gettime, SDL2 still calls gettimeofday().
Product: SDL Reporter: Tiago O. <toacn-foss>
Component: *don't know*Assignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: minor    
Priority: P2 CC: ravas.gorefiend
Version: HG 2.1   
Hardware: x86_64   
OS: Linux   
Attachments: When clock_gettime is available, make SDL_syscond.c and SDL_syssem.c use it

Description Tiago O. 2014-02-21 20:37:05 UTC
clock_gettime() could (and should, maybe?) entirely replace gettimeofday().
Comment 1 Ben Swick 2014-11-19 18:01:37 UTC
Since 2013-05-02 the configure script defaults to using clock_gettime. I verified by adding a print statement in SDL_GetTicks() that clock_gettime was used with the latest revision in HG.

I checked the SDL 2.0.2 package in the Ubuntu repository and it uses clock_gettime. I compared the value returned by SDL_GetPerformanceCounter() to the values from calls to clock_gettime() and gettimeofday().

The values were:
SDL_GetPerformanceCounter() returned      13044428999245
clock_gettime() in nanoseconds returned   13044428999743
gettimeofday() in microseconds returned 1416419469058256
Comment 2 Tiago O. 2014-11-19 20:40:32 UTC
As of rev 9224, SDL2 has references to gettimeofday without a clock_gettime alternative in:

src/thread/pthread/SDL_syscond.c line 108
src/thread/pthread/SDL_syssem.c line 128
src/audio/nas/SDL_nasaudio.c lines 237 and 249

"readelf -s libSDL2-2.0.so.0.4.0 | grep gettime" prints:

    64: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND gettimeofday@GLIBC_2.2.5 (3)
   127: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND clock_gettime@GLIBC_2.2.5 (10)
Comment 3 Ben Swick 2014-11-20 21:24:59 UTC
Created attachment 1942 [details]
When clock_gettime is available, make SDL_syscond.c and SDL_syssem.c use it

Makes SDL_syscond.c and SDL_syssem.c use clock_gettime(CLOCK_REALTIME) when HAVE_CLOCK_GETTIME is defined.
Comment 4 Ben Swick 2014-11-20 21:31:26 UTC
I've attached a patch making SDL_CondWaitTimeout and SDL_SemWaitTimeout use clock_gettime(CLOCK_REALTIME) when HAVE_CLOCK_GETTIME is defined.

I did not change the usage in SDL_nasaudio.c. The value of gettimeofday there is not actually used anywhere so it may just be for debugging purposes.
Comment 5 Sam Lantinga 2014-11-28 12:38:26 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/9f8962b9b09e