Skip to content

Commit 91a296f

Browse files
committed
stdlib: avoid posix sched_yield in favour of C++11
Use the C++11 std::this_thread::yield to abstract away platform specific differences for yield execution time. This makes the code more portable to non-POSIX environments (i.e. Windows). NFC.
1 parent 78c04e2 commit 91a296f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

stdlib/public/runtime/HeapObject.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <cstring>
2929
#include <cstdio>
3030
#include <cstdlib>
31+
#include <thread>
3132
#include "../SwiftShims/RuntimeShims.h"
3233
#if SWIFT_OBJC_INTEROP
3334
# include <objc/NSObject.h>
@@ -766,7 +767,7 @@ HeapObject *swift::swift_weakLoadStrong(WeakReference *ref) {
766767
short c = 0;
767768
while (__atomic_load_n(&ref->Value, __ATOMIC_RELAXED) & WR_READING) {
768769
if (++c == WR_SPINLIMIT) {
769-
sched_yield();
770+
std::this_thread::yield();
770771
c -= 1;
771772
}
772773
}
@@ -815,7 +816,7 @@ void swift::swift_weakCopyInit(WeakReference *dest, WeakReference *src) {
815816
short c = 0;
816817
while (__atomic_load_n(&src->Value, __ATOMIC_RELAXED) & WR_READING) {
817818
if (++c == WR_SPINLIMIT) {
818-
sched_yield();
819+
std::this_thread::yield();
819820
c -= 1;
820821
}
821822
}

0 commit comments

Comments
 (0)