[vlc-commits] [Git][videolan/vlc][master] 5 commits: win32: thread: avoid checking the delay value twice at each loop

Steve Lhomme (@robUx4) gitlab at videolan.org
Fri Aug 19 05:04:41 UTC 2022



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
62a88b42 by Steve Lhomme at 2022-08-19T04:42:30+00:00
win32: thread: avoid checking the delay value twice at each loop

We either return 0 or ETIMEDOUT from the loop.

- - - - -
0169059a by Steve Lhomme at 2022-08-19T04:42:30+00:00
win32: thread: rework the conversion to milliseconds in vlc_atomic_timedwait()

First we convert the delay to int64_t (same width/sign as a vlc_tick_t) in
milliseconds from vlc_tick_t/time_t. Then we check it fits in a DWORD, aka an
unsigned long. If it doesn't fit we clip the value to ULONG_MAX.

- - - - -
dfb6d152 by Steve Lhomme at 2022-08-19T04:42:30+00:00
win32: thread: use ULONG_MAX for the vlc_atomic_timedwait_daytime() clipping

DWORD is an unsigned long so it can use up to ULONG_MAX.

- - - - -
289075fa by Steve Lhomme at 2022-08-19T04:42:30+00:00
win32: thread: use ULONG_MAX for the vlc_tick_wait() clipping

DWORD is an unsigned long so it can use up to ULONG_MAX.

- - - - -
c0a51bce by Steve Lhomme at 2022-08-19T04:42:30+00:00
win32: thread: don't rely on the precision of vlc_tick_now to timeout

This also saves a call to vlc_tick_now() on timeout.

- - - - -


1 changed file:

- src/win32/thread.c


Changes:

=====================================
src/win32/thread.c
=====================================
@@ -276,50 +276,51 @@ int vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_t deadline)
 {
     vlc_tick_t delay;
 
-    do
+    for(;;)
     {
-        long ms;
-
         delay = deadline - vlc_tick_now();
 
         if (delay < 0)
-            break; // deadline passed
-        if (delay >= VLC_TICK_FROM_MS(LONG_MAX))
-            ms = LONG_MAX;
+            return ETIMEDOUT; // deadline passed
+
+        DWORD ms;
+        int64_t idelay = MS_FROM_VLC_TICK(delay);
+        static_assert(sizeof(unsigned long) <= sizeof(DWORD), "unknown max DWORD");
+        if (unlikely(idelay > ULONG_MAX))
+            ms = ULONG_MAX;
         else
-            ms = MS_FROM_VLC_TICK(delay);
+            ms = idelay;
 
         if (WaitOnAddress(addr, &val, sizeof (val), ms))
             return 0;
+        if (GetLastError() == ERROR_TIMEOUT)
+            return ETIMEDOUT;
     }
-    while (delay > 0);
-
-    return ETIMEDOUT;
 }
 
 int vlc_atomic_timedwait_daytime(void *addr, unsigned val, time_t deadline)
 {
-    long delay;
+    time_t delay;
 
-    do
+    for(;;)
     {
-        long ms;
-
         delay = deadline - time(NULL);
 
         if (delay < 0)
-            break; // deadline passed
-        if (delay >= (LONG_MAX / 1000))
-            ms = LONG_MAX;
+            return ETIMEDOUT; // deadline passed
+
+        DWORD ms;
+        static_assert(sizeof(unsigned long) <= sizeof(DWORD), "unknown max DWORD");
+        if (unlikely(delay > (ULONG_MAX / 1000)))
+            ms = ULONG_MAX;
         else
             ms = delay * 1000;
 
         if (WaitOnAddress(addr, &val, sizeof (val), ms))
             return 0;
+        if (GetLastError() == ERROR_TIMEOUT)
+            return ETIMEDOUT;
     }
-    while (delay > 0);
-
-    return ETIMEDOUT;
 }
 
 void vlc_atomic_notify_one(void *addr)
@@ -615,14 +616,18 @@ void (vlc_tick_wait)(vlc_tick_t deadline)
 
     while ((delay = (deadline - vlc_tick_now())) > 0)
     {
-        delay = (delay + (1000-1)) / 1000;
-        if (unlikely(delay > 0x7fffffff))
-            delay = 0x7fffffff;
+        int64_t idelay = MS_FROM_VLC_TICK(delay + VLC_TICK_FROM_MS(1)-1);
+        DWORD delay_ms;
+        static_assert(sizeof(unsigned long) <= sizeof(DWORD), "unknown max DWORD");
+        if (unlikely(idelay > ULONG_MAX))
+            delay_ms = ULONG_MAX;
+        else
+            delay_ms = idelay;
 
 #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
-        Sleep(delay);
+        Sleep(delay_ms);
 #else
-        SleepEx(delay, TRUE);
+        SleepEx(delay_ms, TRUE);
 
         if (likely(th != NULL) && th->killable)
         {
@@ -630,6 +635,8 @@ void (vlc_tick_wait)(vlc_tick_t deadline)
                 vlc_docancel(th);
         }
 #endif
+        if (delay_ms != ULONG_MAX)
+            break;
     }
 }
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/f8ba2cd14232eec6c04bd5679d40ca89e1deb7bc...c0a51bcec29a78fff61de7cf3efb1d44b08972ab

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/f8ba2cd14232eec6c04bd5679d40ca89e1deb7bc...c0a51bcec29a78fff61de7cf3efb1d44b08972ab
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