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 5467

Summary: SDL sys timer Mac OS update proposal
Product: SDL Reporter: David Carlier <devnexen>
Component: timerAssignee: Ryan C. Gordon <icculus>
Status: WAITING --- QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: amaranth72, icculus, sezeroz
Version: HG 2.1   
Hardware: All   
OS: Mac OS X (All)   
Attachments: patch
patch 2
patch 3
patch 4
better check for clock_gettime_nsec_np()
better check for clock_gettime_nsec_np()
better check for clock_gettime_nsec_np() - v6

Description David Carlier 2021-01-15 20:44:06 UTC
Created attachment 4666 [details]
patch

Change of api from 2016 which reduce code complexity a bit.
Comment 1 Ozkan Sezer 2021-01-15 20:51:18 UTC
As far as I can see, this breaks macOS < 10.12
Comment 2 Ryan C. Gordon 2021-01-15 20:53:30 UTC
(In reply to Ozkan Sezer from comment #1)
> As far as I can see, this breaks macOS < 10.12

Fwiw, mach_absolute_time() is apparently deprecated as of 11.0, so we'll probably have to wrap this in preprocessor check in any case.  :/

--ryan.
Comment 3 David Carlier 2021-01-15 21:09:59 UTC
Ah older still supported. I can try detection at config time in both autoconf/Cmake fashions.
Comment 4 David Carlier 2021-01-15 21:37:18 UTC
Created attachment 4668 [details]
patch 2
Comment 5 Ozkan Sezer 2021-01-15 22:07:26 UTC
Created attachment 4671 [details]
patch 3

The second patch didn't apply, I regenerated it by guess-work:
Please confirm that it matches your intention.
Comment 6 David Carlier 2021-01-15 22:12:30 UTC
Yes sorry I m not comfortable with mercurial really much better with git :-) but the diff looks good indeed, except maybe those two lines removal

`
-#else
-#define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
`
Comment 7 Ozkan Sezer 2021-01-15 22:25:55 UTC
Created attachment 4672 [details]
patch 4

(In reply to David Carlier from comment #6)
> but the diff looks good indeed, except maybe those two lines removal

Attached v4 patch.
Comment 8 David Carlier 2021-01-15 22:27:11 UTC
+1
Comment 9 Sam Lantinga 2021-01-17 18:13:58 UTC
Is there any benefit for using the newer API?
If it's performance, can you attach your test and the timing results you got?
Comment 10 David Carlier 2021-01-17 18:41:25 UTC
Not that much (no more init but that s once) but more reliability, less error prone and easier to use. It s usually advised to move on to this newer api when possible (ideally I would have prefered removing the ancient one but depends on support wanted).
Comment 11 Sam Lantinga 2021-01-23 17:51:15 UTC
Patch added, thanks!
https://hg.libsdl.org/SDL/rev/8cad7d7ec50c
Comment 12 Ozkan Sezer 2021-01-23 22:31:20 UTC
Created attachment 4697 [details]
better check for clock_gettime_nsec_np()
Comment 13 Ozkan Sezer 2021-01-23 22:31:50 UTC
(In reply to Ozkan Sezer from comment #12)
> Created attachment 4697 [details]
> better check for clock_gettime_nsec_np()

The attached patch makes sure that the build is targeting macOS >= 10.12
for clock_gettime_nsec_np() availability.  It also adds missing HAVE_xxx
to SDL_config_macosx.h.  Please review.

Notes:
I do NOT know how to check IOS version requirements. time.h from 10.12
SDK says:
__IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)

If these are already the minimum requirements from SDL, then I can add
the availability unconditionally:  Tell me what to do for it and I will
complete the patch and push to hg.
Comment 14 David Carlier 2021-01-23 22:48:57 UTC
> I do NOT know how to check IOS version requirements

I do not know if that answers your question but the attribute available can

as :

if (@available(iOS 10, *)) // It is variadic to be able to check tvOs and so on
Comment 15 Ozkan Sezer 2021-01-24 00:08:31 UTC
(In reply to David Carlier from comment #14)
> I do not know if that answers your question but the attribute available can
> 
> as :
> 
> if (@available(iOS 10, *)) // It is variadic to be able to check tvOs and so on

I believe that's for detection at run time.  We need something for compile-time.
Comment 16 David Carlier 2021-01-24 06:22:41 UTC
ah right I think you have constants such as :
 - __IPHONE_OS_VERSION_MIN_REQUIRED
- more broadly __OSX_AVAILABLE_STARTING(__MAC_10_2,__IPHONE_10_0)

from the top of my head.
Comment 17 David Carlier 2021-01-24 06:28:28 UTC
otherwise there are these in case :
- __WATCHOS_AVAILABLE(<version>) and __TVOS_AVAILABLE(<version>)
Comment 18 Ozkan Sezer 2021-01-24 14:40:55 UTC
Created attachment 4700 [details]
better check for clock_gettime_nsec_np()

-- patch rebased to current hg
Comment 19 Ozkan Sezer 2021-01-24 16:47:00 UTC
Created attachment 4701 [details]
better check for clock_gettime_nsec_np() - v6

Updated patch, v6:

- Unconditionally defines HAVE_CLOCK_GETTIME_NSEC_NP for iOS / TVOS
  assuming iOS >= 10.0.
  This is similar to SDL_JOYSTICK_MFI being defined for iOS or TVOS
  which depend on GameController framework which require iOS >=10.0
  at the least.

Comments?  OK to apply?
Comment 20 David Carlier 2021-01-24 19:16:04 UTC
Seems ok to me
Comment 21 Ryan C. Gordon 2021-01-25 00:29:47 UTC
Latest patch looks good to me.

--ryan.
Comment 22 Ozkan Sezer 2021-01-25 01:13:40 UTC
(In reply to David Carlier from comment #20)
> Seems ok to me

(In reply to Ryan C. Gordon from comment #21)
> Latest patch looks good to me.
> 
> --ryan.

OK, applied the latest patch as https://hg.libsdl.org/SDL/rev/d01593bd58d9

Re-closing as fixed.
Comment 23 Sam Lantinga 2021-02-02 05:59:03 UTC
SDL_GetTicks() was broken, so I backed this out:
https://hg.libsdl.org/SDL/rev/88c79c6a8457
Comment 24 David Carlier 2021-02-02 06:16:00 UTC
Ah surprising but understood... Sorry for this.
Comment 25 Sam Lantinga 2021-02-02 06:41:04 UTC
It may make sense to use clock_gettime_nsec_np() for the performance timer and leave the original code for SDL_GetTicks().

Thoughts?
Comment 26 David Carlier 2021-02-02 06:44:38 UTC
Could work out indeed.
Comment 27 Sam Lantinga 2021-02-02 17:56:46 UTC
Is there any advantage in performance or resolution?
Comment 28 David Carlier 2021-02-02 18:06:39 UTC
Performance not really, resolution most likely and more consistent across architectures.
Comment 29 Alex Szpakowski 2021-02-02 22:24:48 UTC
As far as I know there's no benefit to using clock_gettime_nsec_np versus the current code aside from mach_absolute_time being marked as deprecated. I suspect Apple only marked it as deprecated in the first place because other code (not SDL) was using it wrong on macOS in a way that will produce correct results on x64 but not Apple Silicon CPUs.