diff options
author | Alexander Momchilov <alexandermomchilov@gmail.com> | 2020-12-08 23:12:44 -0500 |
---|---|---|
committer | cgzones <cgzones@googlemail.com> | 2020-12-19 21:30:39 +0100 |
commit | 67ccd6b909d28ab84c77acecdfee927337489cc2 (patch) | |
tree | 25c9c86935167f733356a387fdc6656a0aaef292 /darwin/Platform.c | |
parent | f614b8a19fe92cd13862605c16d69aa23c8b9bd1 (diff) |
Unhardcode tick-to-ms conversion
Division by 100000.0 worked because `sysconf(_SC_CLK_TCK)` happened to be 100.
By unhardcoding:
1) It becomes more clear what this 100000.0 figure comes from.
2) It protects against bugs in the case `sysconf(_SC_CLK_TCK)` ever changes.
Diffstat (limited to 'darwin/Platform.c')
-rw-r--r-- | darwin/Platform.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/darwin/Platform.c b/darwin/Platform.c index a0ea6721..a4ed4647 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -10,16 +10,18 @@ in the source distribution for its full text. #include "Platform.h" +#include <errno.h> #include <math.h> #include <stdlib.h> - -#include <CoreFoundation/CoreFoundation.h> +#include <unistd.h> #include <CoreFoundation/CFString.h> +#include <CoreFoundation/CoreFoundation.h> #include <IOKit/ps/IOPowerSources.h> #include <IOKit/ps/IOPSKeys.h> #include "ClockMeter.h" #include "CPUMeter.h" +#include "CRT.h" #include "DarwinProcessList.h" #include "DateMeter.h" #include "DateTimeMeter.h" @@ -112,6 +114,8 @@ const MeterClass* const Platform_meterTypes[] = { double Platform_timebaseToNS = 1.0; +long Platform_clockTicksPerSec = -1; + void Platform_init(void) { // Check if we can determine the timebase used on this system. // If the API is unavailable assume we get our timebase in nanoseconds. @@ -122,6 +126,14 @@ void Platform_init(void) { #else Platform_timebaseToNS = 1.0; #endif + + // Determine the number of clock ticks per second + errno = 0; + Platform_clockTicksPerSec = sysconf(_SC_CLK_TCK); + + if (errno || Platform_clockTicksPerSec < 1) { + CRT_fatalError("Unable to retrieve clock tick rate"); + } } void Platform_done(void) { |