[vlc-commits] [Git][videolan/vlc][master] win32: dynamically pick GetSystemTimePreciseAsFileTime

Hugo Beauzée-Luyssen (@chouquette) gitlab at videolan.org
Mon Aug 15 12:54:34 UTC 2022



Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
5cddba1e by Steve Lhomme at 2022-08-15T10:39:45+00:00
win32: dynamically pick GetSystemTimePreciseAsFileTime

Even in Win7 builds we can use it if it's available on the system.

- - - - -


2 changed files:

- src/win32/thread.c
- src/win32/timer.c


Changes:

=====================================
src/win32/thread.c
=====================================
@@ -564,16 +564,20 @@ static vlc_tick_t mdate_perf_100ns(void)
     return VLC_TICK_FROM_MSFTIME(counter.QuadPart);
 }
 
+#if _WIN32_WINNT < _WIN32_WINNT_WIN8
+static void (WINAPI *SystemTimeAsFileTime_)(LPFILETIME) = GetSystemTimeAsFileTime;
+#endif // _WIN32_WINNT < _WIN32_WINNT_WIN8
+
 static vlc_tick_t mdate_wall (void)
 {
     FILETIME ts;
     ULARGE_INTEGER s;
 
-#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) && (!defined(VLC_WINSTORE_APP) || _WIN32_WINNT >= 0x0A00)
+#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
     GetSystemTimePreciseAsFileTime (&ts);
-#else
-    GetSystemTimeAsFileTime (&ts);
-#endif
+#else // _WIN32_WINNT < _WIN32_WINNT_WIN8
+    SystemTimeAsFileTime_ (&ts);
+#endif // _WIN32_WINNT < _WIN32_WINNT_WIN8
     s.LowPart = ts.dwLowDateTime;
     s.HighPart = ts.dwHighDateTime;
     /* hundreds of nanoseconds */
@@ -665,6 +669,12 @@ static void SelectClockSource(libvlc_int_t *obj)
     if (!strcmp (name, "wall"))
     {
         msg_Dbg (obj, "using system time as clock source");
+#if _WIN32_WINNT < _WIN32_WINNT_WIN8
+        HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
+        SystemTimeAsFileTime_ = (void*)GetProcAddress(h, "GetSystemTimePreciseAsFileTime");
+        if (unlikely(SystemTimeAsFileTime_ == NULL)) // win7
+            SystemTimeAsFileTime_ = GetSystemTimeAsFileTime;
+#endif // _WIN32_WINNT < _WIN32_WINNT_WIN8
         mdate_selected = mdate_wall;
     }
     else


=====================================
src/win32/timer.c
=====================================
@@ -44,10 +44,25 @@ static VOID CALLBACK timer_callback(PTP_CALLBACK_INSTANCE instance,
     timer->func(timer->data);
 }
 
+#if _WIN32_WINNT < _WIN32_WINNT_WIN8
+static vlc_once_t TIMER_INIT_FUNC = VLC_STATIC_ONCE;
+static void (WINAPI *SystemTimeAsFileTime_)(LPFILETIME);
+#endif // _WIN32_WINNT < _WIN32_WINNT_WIN8
+
 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
 {
     struct vlc_timer *timer = malloc (sizeof (*timer));
 
+#if _WIN32_WINNT < _WIN32_WINNT_WIN8
+    if (unlikely(!vlc_once_begin(&TIMER_INIT_FUNC))) {
+        HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
+        SystemTimeAsFileTime_ = (void*)GetProcAddress(h, "GetSystemTimePreciseAsFileTime");
+        if (unlikely(SystemTimeAsFileTime_ == NULL)) // win7
+            SystemTimeAsFileTime_ = GetSystemTimeAsFileTime;
+        vlc_once_complete(&TIMER_INIT_FUNC);
+    }
+#endif // _WIN32_WINNT < _WIN32_WINNT_WIN8
+
     if (timer == NULL)
         return ENOMEM;
     timer->t = CreateThreadpoolTimer(timer_callback, timer, NULL);
@@ -94,11 +109,11 @@ void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
 
     /* Get the system FILETIME */
     FILETIME time;
-#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) && (!defined(VLC_WINSTORE_APP) || _WIN32_WINNT >= 0x0A00)
+#if _WIN32_WINNT < _WIN32_WINNT_WIN8
+    SystemTimeAsFileTime_(&time);
+#else // _WIN32_WINNT >= _WIN32_WINNT_WIN8
     GetSystemTimePreciseAsFileTime(&time);
-#else
-    GetSystemTimeAsFileTime(&time);
-#endif
+#endif // _WIN32_WINNT >= _WIN32_WINNT_WIN8
 
     /* Convert it to ULARGE_INTEGER to allow calculation (addition here) */
     ULARGE_INTEGER time_ul = {



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/5cddba1eebc13c94f9cee0b77f495e0b109c6b73

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/5cddba1eebc13c94f9cee0b77f495e0b109c6b73
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list