Skip to content

Commit 0a5e34e

Browse files
committed
Cleanup, add support for Swift 5.6, drop support for Swift pre 5.5
1 parent 26dae7c commit 0a5e34e

File tree

10 files changed

+43
-94
lines changed

10 files changed

+43
-94
lines changed

Package.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.4
1+
// swift-tools-version:5.6
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -24,8 +24,7 @@ let package = Package(
2424
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2525
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2626
.target(
27-
name: "GCDCoreOperations",
28-
dependencies: []),
27+
name: "GCDCoreOperations"),
2928
.target(
3029
name: "GCDOperations",
3130
dependencies: ["GCDCoreOperations"]),
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.5
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -24,8 +24,7 @@ let package = Package(
2424
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2525
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2626
.target(
27-
name: "GCDCoreOperations",
28-
dependencies: []),
27+
name: "GCDCoreOperations"),
2928
.target(
3029
name: "GCDOperations",
3130
dependencies: ["GCDCoreOperations"]),

Sources/GCDCoreOperations/Base Classes/GroupOperation.swift

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,37 @@ import Dispatch
44
public final class GroupOperation: Operation {
55
private let group = DispatchGroup()
66

7+
@Synchronized
8+
private var _operations: ContiguousArray<Operation>
79
/// The operations grouped inside this operation.
8-
public private(set) var operations: ContiguousArray<Operation>
10+
public var operations: ContiguousArray<Operation> { _operations }
911

1012
/// Creates a new GroupOperation with the given list of operations.
1113
/// - Parameter operations: The operations to execute grouped in this operation.
1214
public init(operations: ContiguousArray<Operation>) {
13-
self.operations = operations
15+
__operations = .init(wrappedValue: operations)
16+
}
17+
18+
/// Creates a new GroupOperation with the given list of operations.
19+
/// - Parameter operations: The operations to execute grouped in this
20+
@inlinable
21+
public convenience init<C: Collection>(operations: C) where C.Element == Operation {
22+
self.init(operations: .init(operations))
1423
}
1524

1625
/// Creates a new GroupOperation with the given variadic list of operations.
1726
/// - Parameter operations: The variadic list of operations to execute grouped in this operation.
27+
@inlinable
1828
public convenience init(operations: Operation...) {
19-
self.init(operations: ContiguousArray(operations))
29+
self.init(operations: operations)
2030
}
2131

2232
/// Adds a new operation to this GroupOperation.
2333
/// - Parameter op: The operation to add.
2434
/// - Precondition: This GroupOperation must not have finished yet!
2535
public func addOperation(_ op: Operation) {
2636
assert(!isFinished, "Cannot add operations after GroupOperation has finished!")
27-
operations.append(op)
37+
__operations.withValue { $0.append(op) }
2838
if case .running = state, let queue = queue {
2939
includeOperation(op, on: queue)
3040
}
@@ -37,7 +47,7 @@ public final class GroupOperation: Operation {
3747
where Operations: Collection, Operations.Element == Operation
3848
{
3949
assert(!isFinished, "Cannot add operations after GroupOperation has finished!")
40-
operations.append(contentsOf: ops)
50+
__operations.withValue { $0.append(contentsOf: ops) }
4151
if case .running = state, let queue = queue {
4252
ops.forEach { includeOperation($0, on: queue) }
4353
}

Sources/GCDCoreOperations/Base Classes/Operation.swift

+9-10
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ open class Operation: CustomStringConvertible, CustomDebugStringConvertible {
2222
@Synchronized
2323
internal final var state: State = .created
2424

25+
// queue, observers and conditions are write-protected using `state`.
2526
internal private(set) final var queue: DispatchQueue?
2627

2728
/// The list of observers of this operation.
28-
public private(set) final var observers: [OperationObserver] = []
29+
public private(set) final var observers = Array<OperationObserver>()
2930
/// The list of conditions of this operation.
30-
public private(set) final var conditions: [OperationCondition] = []
31+
public private(set) final var conditions = Array<OperationCondition>()
3132

3233
@Synchronized
33-
private final var _dependencies: ContiguousArray<Operation> = []
34+
private final var _dependencies = ContiguousArray<Operation>()
3435
/// The list of dependencies of this operation.
3536
public final var dependencies: ContiguousArray<Operation> { _dependencies }
3637

@@ -39,7 +40,9 @@ open class Operation: CustomStringConvertible, CustomDebugStringConvertible {
3940
private let dependenciesGroup = DispatchGroup()
4041

4142
/// The list of errors this operation encountered.
42-
public private(set) final var errors: [Error] = []
43+
@Synchronized
44+
private final var _errors = Array<Error>()
45+
public final var errors: Array<Error> { _errors }
4346

4447
// MARK: - State Accessors
4548
/// Whether or not this operation was cancelled.
@@ -117,7 +120,7 @@ open class Operation: CustomStringConvertible, CustomDebugStringConvertible {
117120
/// Aggregates a collection of errors into the list of errors of this operation.
118121
/// - Parameter newErrors: The collections of errors to aggregate.
119122
public final func aggregate<Errors>(errors newErrors: Errors) where Errors: Collection, Errors.Element: Error {
120-
errors.append(contentsOf: newErrors.lazy.map { $0 })
123+
__errors.withValue { $0.append(contentsOf: newErrors.lazy.map { $0 }) }
121124
}
122125

123126
/// Aggregates a variadic list of errors into the list of errors of this operation.
@@ -153,11 +156,7 @@ open class Operation: CustomStringConvertible, CustomDebugStringConvertible {
153156
return false
154157
}
155158
guard !isCancelled else { return }
156-
if let group = group {
157-
queue.async(group: group, execute: run)
158-
} else {
159-
queue.async(execute: run)
160-
}
159+
queue.async(group: group, execute: run)
161160
}
162161

163162
private final func run() {

Sources/GCDCoreOperations/Base Classes/OperationQueue.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Dispatch
22
#if os(Linux)
3-
import func CoreFoundation._CFIsMainThread
3+
import func CoreFoundation._CFIsMainThread
44
#endif
55

66
private func isMainThread() -> Bool {
7-
#if os(Linux)
8-
return _CFIsMainThread()
9-
#else
10-
return pthread_main_np() != 0
11-
#endif
7+
#if os(Linux)
8+
return _CFIsMainThread()
9+
#else
10+
return pthread_main_np() != 0
11+
#endif
1212
}
1313

1414
private let _operationQueueKey = DispatchSpecificKey<Unmanaged<OperationQueue>>()
@@ -46,7 +46,7 @@ public final class OperationQueue {
4646

4747
private let queue: DispatchQueue
4848
private let operationsGroup = DispatchGroup()
49-
private var operations: Dictionary<Int, Operation> = [:]
49+
private var operations = Dictionary<Int, Operation>()
5050

5151
/// Whether or not the queue is currently suspended.
5252
public private(set) var isSuspended: Bool
@@ -100,7 +100,7 @@ public final class OperationQueue {
100100
operationsGroup.enter()
101101
op.addObserver(QueueObserver(queue: self))
102102
let operationIdentifier = op._queueID
103-
assert(operations[operationIdentifier] == nil, "Operation \(op) has already been added to this queue!")
103+
assert(!operations.keys.contains(operationIdentifier), "Operation \(op) has already been added to this queue!")
104104
operations[operationIdentifier] = op
105105

106106
op.conditions.lazy
@@ -129,7 +129,7 @@ public final class OperationQueue {
129129
dispatchPrecondition(condition: .notOnQueue(lockQueue))
130130
lockQueue.sync {
131131
let opID = op._queueID
132-
assert(operations[opID] != nil, "Operation \(op) is not enqueued in this queue!")
132+
assert(operations.keys.contains(opID), "Operation \(op) is not enqueued in this queue!")
133133
operations.removeValue(forKey: opID)
134134
operationsGroup.leave()
135135
}

Sources/GCDCoreOperations/Condition/ExclusivityController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@frozen
22
enum ExclusivityController {
33
@Synchronized
4-
private static var operations: [String: ContiguousArray<Operation>] = [:]
4+
private static var operations = Dictionary<String, ContiguousArray<Operation>>()
55

66
static func addOperation<Categories: Collection>(_ operation: Operation, categories: Categories)
77
where Categories.Element == String

Sources/GCDCoreOperations/Helpers/Synchronized.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class Synchronized<Value> {
1111
dispatchPrecondition(condition: .notOnQueue(accessQueue))
1212
return accessQueue.sync { _wrappedValue }
1313
}
14-
14+
1515
init(wrappedValue: Value) { _wrappedValue = wrappedValue }
1616

1717
func withValue<T>(do work: (inout Value) throws -> T) rethrows -> T {
@@ -38,3 +38,7 @@ final class Synchronized<Value> {
3838
}
3939
}
4040
}
41+
42+
#if compiler(>=5.5) && canImport(_Concurrency)
43+
extension Synchronized: @unchecked Sendable {}
44+
#endif

Tests/GCDCoreOperationsTests/XCTestManifests.swift

-34
This file was deleted.

Tests/GCDOperationsTests/XCTestManifests.swift

-18
This file was deleted.

Tests/LinuxMain.swift

-10
This file was deleted.

0 commit comments

Comments
 (0)