Skip to content

Commit eec4846

Browse files
committed
added yield to Thread
1 parent 200240a commit eec4846

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
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/FileSystemTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class FileSystemTests: XCTestCase {
625625
// Sum and write back to file.
626626
try fs.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
627627

628-
sched_yield()
628+
Thread.yield()
629629

630630
// Get thr current contents of the file if any.
631631
let valueB: Int
@@ -645,7 +645,7 @@ class FileSystemTests: XCTestCase {
645645
try! fs.withLock(on: lockFile, type: .shared) {
646646
try XCTAssertEqual(fs.readFileContents(fileA), fs.readFileContents(fileB))
647647

648-
sched_yield()
648+
Thread.yield()
649649

650650
try XCTAssertEqual(fs.readFileContents(fileA), fs.readFileContents(fileB))
651651
}
@@ -677,7 +677,7 @@ class FileSystemTests: XCTestCase {
677677
// Sum and write back to file.
678678
try localFileSystem.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
679679

680-
sched_yield()
680+
Thread.yield()
681681

682682
// Get thr current contents of the file if any.
683683
let valueB: Int
@@ -697,7 +697,7 @@ class FileSystemTests: XCTestCase {
697697
try! localFileSystem.withLock(on: lockFile, type: .shared) {
698698
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
699699

700-
sched_yield()
700+
Thread.yield()
701701

702702
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
703703
}
@@ -737,7 +737,7 @@ class FileSystemTests: XCTestCase {
737737
// Sum and write back to file.
738738
try! fs.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
739739

740-
sched_yield()
740+
Thread.yield()
741741

742742
// Get thr current contents of the file if any.
743743
let valueB: Int
@@ -757,7 +757,7 @@ class FileSystemTests: XCTestCase {
757757
try! fs.withLock(on: lockFile, type: .shared) {
758758
try XCTAssertEqual(fs.readFileContents(fileA), fs.readFileContents(fileB))
759759

760-
sched_yield()
760+
Thread.yield()
761761

762762
try XCTAssertEqual(fs.readFileContents(fileA), fs.readFileContents(fileB))
763763
}

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)