]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
easy_lock: use Sleep(1) for thread yield on old Windows
authorJay Satiro <raysatiro@yahoo.com>
Sun, 19 Jan 2025 06:18:23 +0000 (01:18 -0500)
committerJay Satiro <raysatiro@yahoo.com>
Tue, 21 Jan 2025 06:45:21 +0000 (01:45 -0500)
- Prefer Sleep(1) over sched_yield() for pre-Vista thread yield.

On Windows sched_yield is often implemented as Sleep(0) which only
yields to threads of highest priority to current priority. However,
during libcurl initialization if there is thread contention then it's
possible that there is a wait for a different library or OS thread of
a lesser priority and then the yield is not effective during that time.
On the other hand Sleep(1) will wait the minimum time slice which is
usually like 15ms or more.

Prior to this change 2c4bfef removed sched_yield detection on Windows,
which effectively removed the yield in the spin lock, and therefore this
change restores the yield but in a different way.

For Windows Vista and later we use SRW locks and do not have this issue.

Ref: https://github.com/curl/curl/pull/16037#issuecomment-2600161764
Ref: https://devblogs.microsoft.com/oldnewthing/20051004-09/?p=33923

Closes https://github.com/curl/curl/pull/16048

lib/easy_lock.h

index 0747e7d928aa1746574eb1fd3415dd27799697b4..ec324cfc81a2e44626f8f04cc3436db30c52fc3a 100644 (file)
@@ -81,6 +81,8 @@ static CURL_INLINE void curl_simple_lock_lock(curl_simple_lock *lock)
       __builtin_ia32_pause();
 #elif defined(__aarch64__)
       __asm__ volatile("yield" ::: "memory");
+#elif defined(_WIN32)
+      Sleep(1);
 #elif defined(HAVE_SCHED_YIELD)
       sched_yield();
 #endif