[vlc-commits] darwin: thread: fix invalid ticks conversion
Zhao Zhili
git at videolan.org
Fri Sep 28 09:09:40 CEST 2018
vlc | branch: master | Zhao Zhili <quinkblack at foxmail.com> | Thu Sep 27 20:14:06 2018 +0800| [65407e7030174bb4965b6589d9cdedbb4577949c] | committer: Steve Lhomme
darwin: thread: fix invalid ticks conversion
Signed-off-by: Steve Lhomme <robux4 at ycbcr.xyz>
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=65407e7030174bb4965b6589d9cdedbb4577949c
---
src/darwin/thread.c | 24 ++++++++++++++----------
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/src/darwin/thread.c b/src/darwin/thread.c
index f5eb4f234e..a96334cd4e 100644
--- a/src/darwin/thread.c
+++ b/src/darwin/thread.c
@@ -42,12 +42,21 @@
#include <mach/mach_time.h>
#include <execinfo.h>
-static mach_timebase_info_data_t vlc_clock_conversion_factor;
+static struct {
+ uint32_t quotient;
+ uint32_t remainder;
+ uint32_t divider;
+} vlc_clock_conversion;
static void vlc_clock_setup_once (void)
{
- if (unlikely(mach_timebase_info (&vlc_clock_conversion_factor) != 0))
+ mach_timebase_info_data_t timebase;
+ if (unlikely(mach_timebase_info (&timebase) != 0))
abort ();
+ lldiv_t d = lldiv (timebase.numer, timebase.denom);
+ vlc_clock_conversion.quotient = (uint32_t)d.quot;
+ vlc_clock_conversion.remainder = (uint32_t)d.rem;
+ vlc_clock_conversion.divider = timebase.denom;
}
static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
@@ -516,14 +525,9 @@ vlc_tick_t vlc_tick_now (void)
vlc_clock_setup();
uint64_t date = mach_absolute_time();
- /* denom is uint32_t, switch to 64 bits to prevent overflow. */
- uint64_t denom = vlc_clock_conversion_factor.denom;
-
- /* Switch to microsecs */
- denom *= UINT64_C(1000);
-
- /* Split the division to prevent overflow */
- return vlc_tick_from_frac( date * vlc_clock_conversion_factor.numer, denom );
+ date = date * vlc_clock_conversion.quotient +
+ date * vlc_clock_conversion.remainder / vlc_clock_conversion.divider;
+ return VLC_TICK_FROM_NS(date);
}
#undef vlc_tick_wait
More information about the vlc-commits
mailing list