Skip to content

Commit f2eddd7

Browse files
committed
added yield to Thread
1 parent 6c8cb71 commit f2eddd7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Sources/TSCBasic/Thread.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ final public class Thread {
6565
}
6666
}
6767
}
68+
69+
/// Causes the calling thread to yield execution to another thread.
70+
public static func yield() {
71+
#if os(Windows)
72+
SwitchToThread()
73+
#else
74+
sched_yield()
75+
#endif
76+
}
6877
}
6978

7079
#if canImport(Darwin)

Tests/TSCBasicTests/LockTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class LockTests: XCTestCase {
6969
// Sum and write back to file.
7070
try localFileSystem.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
7171

72-
sched_yield()
72+
Thread.yield()
7373

7474
// Get thr current contents of the file if any.
7575
let valueB: Int
@@ -89,7 +89,7 @@ class LockTests: XCTestCase {
8989
try! lock.withLock(type: .shared) {
9090
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
9191

92-
sched_yield()
92+
Thread.yield()
9393

9494
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
9595
}

0 commit comments

Comments
 (0)