Skip to content

Commit f54ecae

Browse files
committed
Revert "[Basic] Introduce a Condition wrapper."
At least one of the tests is timing out. This reverts commit 42309df.
1 parent 42309df commit f54ecae

File tree

8 files changed

+26
-136
lines changed

8 files changed

+26
-136
lines changed

Sources/Basic/Condition.swift

Lines changed: 0 additions & 48 deletions
This file was deleted.

Sources/Basic/Lock.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import Foundation
1212

1313
// FIXME: Temporary compatibility shims.
1414
#if !os(macOS)
15-
private typealias NSLock = Foundation.Lock
15+
public typealias NSLock = Foundation.Lock
16+
public typealias NSCondition = Foundation.Condition
1617
#endif
1718

1819
/// A simple lock wrapper.
@@ -30,3 +31,12 @@ public struct Lock {
3031
return try body()
3132
}
3233
}
34+
35+
public extension NSCondition {
36+
/// A helper method to execute the given body while condition is locked.
37+
public func whileLocked<T>(_ body: @noescape () throws -> T) rethrows -> T {
38+
lock()
39+
defer { unlock() }
40+
return try body()
41+
}
42+
}

Sources/Basic/SynchronizedQueue.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
import Foundation
12+
1113
/// This class can be used as a shared queue between multiple threads providing
1214
/// thread safe APIs.
1315
public final class SynchronizedQueue<Element> {
16+
1417
/// Storage for queued elements.
1518
private var storage: [Element]
1619

1720
/// Condition variable to block the thread trying dequeue and queue is empty.
18-
private var notEmptyCondition: Condition
21+
private var notEmptyCondition: NSCondition
1922

2023
/// Create a default instance of queue.
2124
public init() {
2225
storage = []
23-
notEmptyCondition = Condition()
26+
notEmptyCondition = NSCondition()
2427
}
2528

2629
/// Safely enqueue an element to end of the queue and signals a thread blocked on dequeue.

Sources/Basic/Thread.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ final public class Thread {
2020
private var thread: ThreadImpl!
2121

2222
/// Condition variable to support blocking other threads using join when this thread has not finished executing.
23-
private var finishedCondition: Condition
23+
private var finishedCondition: NSCondition
2424

2525
/// A boolean variable to track if this thread has finished executing its task.
2626
private var finished: Bool
2727

2828
/// Creates an instance of thread class with closure to be executed when start() is called.
2929
public init(task: @escaping () -> Void) {
3030
finished = false
31-
finishedCondition = Condition()
31+
finishedCondition = NSCondition()
3232

3333
// Wrap the task with condition notifying any other threads blocked due to this thread.
3434
// Capture self weakly to avoid reference cycle. In case Thread is deinited before the task

Tests/BasicTests/ConditionTests.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.

Tests/BasicTests/SynchronizedQueueTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
import Foundation
1112
import XCTest
1213

1314
import Basic
1415

1516
class SyncronizedQueueTests: XCTestCase {
17+
1618
func testSingleProducerConsumer() {
1719
let queue = SynchronizedQueue<Int?>()
1820
let queueElements = Set(0..<10)
@@ -90,7 +92,7 @@ class SyncronizedQueueTests: XCTestCase {
9092

9193
var consumed = Set<Int>()
9294

93-
let canProduceCondition = Condition()
95+
let canProduceCondition = NSCondition()
9496
// Initially we should be able to produce.
9597
var canProduce = true
9698

Tests/BasicTests/ThreadTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ class ThreadTests: XCTestCase {
5151
}
5252

5353
func testNotDeinitBeforeExecutingTask() {
54-
let finishedCondition = Condition()
54+
let finishedCondition = NSCondition()
5555
var finished = false
5656

5757
Thread {
5858
finished = true
5959
finishedCondition.signal()
6060
}.start()
6161

62-
finishedCondition.whileLocked{
63-
while !finished {
64-
finishedCondition.wait()
65-
}
62+
finishedCondition.lock()
63+
while !finished {
64+
finishedCondition.wait()
6665
}
66+
finishedCondition.unlock()
6767

6868
XCTAssertTrue(finished)
6969
}

Tests/BasicTests/XCTestManifests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public func allTests() -> [XCTestCaseEntry] {
1515
return [
1616
testCase(ByteStringTests.allTests),
1717
testCase(CollectionAlgorithmsTests.allTests),
18-
testCase(ConditionTests.allTests),
1918
testCase(FileAccessTests.allTests),
2019
testCase(FileSystemTests.allTests),
2120
testCase(GraphAlgorithmsTests.allTests),

0 commit comments

Comments
 (0)