File tree Expand file tree Collapse file tree 8 files changed +26
-136
lines changed Expand file tree Collapse file tree 8 files changed +26
-136
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ import Foundation
12
12
13
13
// FIXME: Temporary compatibility shims.
14
14
#if !os(macOS)
15
- private typealias NSLock = Foundation . Lock
15
+ public typealias NSLock = Foundation . Lock
16
+ public typealias NSCondition = Foundation . Condition
16
17
#endif
17
18
18
19
/// A simple lock wrapper.
@@ -30,3 +31,12 @@ public struct Lock {
30
31
return try body ( )
31
32
}
32
33
}
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
+ }
Original file line number Diff line number Diff line change 8
8
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9
9
*/
10
10
11
+ import Foundation
12
+
11
13
/// This class can be used as a shared queue between multiple threads providing
12
14
/// thread safe APIs.
13
15
public final class SynchronizedQueue < Element> {
16
+
14
17
/// Storage for queued elements.
15
18
private var storage : [ Element ]
16
19
17
20
/// Condition variable to block the thread trying dequeue and queue is empty.
18
- private var notEmptyCondition : Condition
21
+ private var notEmptyCondition : NSCondition
19
22
20
23
/// Create a default instance of queue.
21
24
public init ( ) {
22
25
storage = [ ]
23
- notEmptyCondition = Condition ( )
26
+ notEmptyCondition = NSCondition ( )
24
27
}
25
28
26
29
/// Safely enqueue an element to end of the queue and signals a thread blocked on dequeue.
Original file line number Diff line number Diff line change @@ -20,15 +20,15 @@ final public class Thread {
20
20
private var thread : ThreadImpl !
21
21
22
22
/// 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
24
24
25
25
/// A boolean variable to track if this thread has finished executing its task.
26
26
private var finished : Bool
27
27
28
28
/// Creates an instance of thread class with closure to be executed when start() is called.
29
29
public init ( task: @escaping ( ) -> Void ) {
30
30
finished = false
31
- finishedCondition = Condition ( )
31
+ finishedCondition = NSCondition ( )
32
32
33
33
// Wrap the task with condition notifying any other threads blocked due to this thread.
34
34
// Capture self weakly to avoid reference cycle. In case Thread is deinited before the task
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 8
8
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9
9
*/
10
10
11
+ import Foundation
11
12
import XCTest
12
13
13
14
import Basic
14
15
15
16
class SyncronizedQueueTests : XCTestCase {
17
+
16
18
func testSingleProducerConsumer( ) {
17
19
let queue = SynchronizedQueue < Int ? > ( )
18
20
let queueElements = Set ( 0 ..< 10 )
@@ -90,7 +92,7 @@ class SyncronizedQueueTests: XCTestCase {
90
92
91
93
var consumed = Set < Int > ( )
92
94
93
- let canProduceCondition = Condition ( )
95
+ let canProduceCondition = NSCondition ( )
94
96
// Initially we should be able to produce.
95
97
var canProduce = true
96
98
Original file line number Diff line number Diff line change @@ -51,19 +51,19 @@ class ThreadTests: XCTestCase {
51
51
}
52
52
53
53
func testNotDeinitBeforeExecutingTask( ) {
54
- let finishedCondition = Condition ( )
54
+ let finishedCondition = NSCondition ( )
55
55
var finished = false
56
56
57
57
Thread {
58
58
finished = true
59
59
finishedCondition. signal ( )
60
60
} . start ( )
61
61
62
- finishedCondition. whileLocked {
63
- while !finished {
64
- finishedCondition. wait ( )
65
- }
62
+ finishedCondition. lock ( )
63
+ while !finished {
64
+ finishedCondition. wait ( )
66
65
}
66
+ finishedCondition. unlock ( )
67
67
68
68
XCTAssertTrue ( finished)
69
69
}
Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ public func allTests() -> [XCTestCaseEntry] {
15
15
return [
16
16
testCase ( ByteStringTests . allTests) ,
17
17
testCase ( CollectionAlgorithmsTests . allTests) ,
18
- testCase ( ConditionTests . allTests) ,
19
18
testCase ( FileAccessTests . allTests) ,
20
19
testCase ( FileSystemTests . allTests) ,
21
20
testCase ( GraphAlgorithmsTests . allTests) ,
You can’t perform that action at this time.
0 commit comments