Skip to content

Commit 9b97ba4

Browse files
authored
Concurrency package (#843)
1 parent 0f8343e commit 9b97ba4

13 files changed

Lines changed: 658 additions & 78 deletions

Package.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ let package = Package(
88
products: [
99
.library(name: "Split", targets: ["Split"]),
1010

11-
.library(name: "SplitCommons", targets: ["Logging", "Http", "BackoffCounter", "PeriodicRecorderWorker", "Tracker"]),],
11+
.library(name: "SplitCommons", targets: ["Logging", "Http", "BackoffCounter", "PeriodicRecorderWorker", "Tracker", "Concurrency"]),],
1212
targets: [
1313

1414
// MARK: Split
1515
.target(
1616
name: "Split",
17-
dependencies: ["Http", "BackoffCounter", "Logging", "PeriodicRecorderWorker", "Tracker"],
17+
dependencies: ["BackoffCounter", "Concurrency", "Http", "Logging", "PeriodicRecorderWorker", "Tracker"],
1818
path: "Split",
1919
exclude: [
2020
"Common/Yaml/LICENSE",
@@ -88,6 +88,17 @@ let package = Package(
8888
dependencies: ["TrackerTests"],
8989
path: "Sources/Tracker/Tests"
9090
),
91+
92+
.target(
93+
name: "Concurrency",
94+
dependencies: [],
95+
exclude: ["Tests", "README.md"]
96+
),
97+
.testTarget(
98+
name: "ConcurrencyTests",
99+
dependencies: ["Concurrency"],
100+
path: "Sources/Concurrency/Tests"
101+
),
91102
// #INJECT_TARGET
92103
]
93104
)
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,94 +8,94 @@
88

99
import Foundation
1010

11-
final class Atomic<T>: @unchecked Sendable {
11+
public final class Atomic<T>: @unchecked Sendable {
1212
// private let queue = DispatchQueue(label: "split-atomic", target: DispatchQueue.general)
1313
private var currentValue: T
1414

1515
private var lock = NSLock()
1616

17-
init(_ value: T) {
17+
public init(_ value: T) {
1818
self.currentValue = value
1919
}
2020

21-
var value: T {
21+
public var value: T {
2222
lock.lock()
2323
defer { lock.unlock() }
2424
return self.currentValue
2525
}
2626

27-
func mutate(_ transformation: (inout T) -> Void) {
27+
public func mutate(_ transformation: (inout T) -> Void) {
2828
lock.lock()
2929
transformation(&self.currentValue)
3030
lock.unlock()
3131
}
3232

33-
func mutate(_ transformation: (T, inout T) -> Void) {
33+
public func mutate(_ transformation: (T, inout T) -> Void) {
3434
lock.lock()
3535
transformation(currentValue, &self.currentValue)
3636
lock.unlock()
3737
}
3838

39-
func getAndSet(_ newValue: T) -> T {
39+
public func getAndSet(_ newValue: T) -> T {
4040
lock.lock()
4141
defer { lock.unlock() }
4242
let oldValue = self.currentValue
4343
self.currentValue = newValue
4444
return oldValue
4545
}
4646

47-
func set(_ newValue: T) {
47+
public func set(_ newValue: T) {
4848
lock.lock()
4949
self.currentValue = newValue
5050
lock.unlock()
5151
}
5252
}
5353

54-
final class AtomicInt: @unchecked Sendable {
54+
public final class AtomicInt: @unchecked Sendable {
5555
private var curValue: Int
5656
private var lock = NSLock()
5757

58-
init(_ value: Int) {
58+
public init(_ value: Int) {
5959
self.curValue = value
6060
}
6161

62-
var value: Int {
62+
public var value: Int {
6363
lock.lock()
6464
defer { lock.unlock() }
6565
return curValue
6666
}
6767

68-
func getAndAdd(_ addValue: Int) -> Int {
68+
public func getAndAdd(_ addValue: Int) -> Int {
6969
lock.lock()
7070
defer { lock.unlock() }
7171
let oldValue = self.curValue
7272
curValue+=addValue
7373
return oldValue
7474
}
7575

76-
func addAndGet(_ addValue: Int) -> Int {
76+
public func addAndGet(_ addValue: Int) -> Int {
7777
lock.lock()
7878
defer { lock.unlock() }
7979
curValue+=addValue
8080
let newValue = self.curValue
8181
return newValue
8282
}
8383

84-
func set(_ newValue: Int) {
84+
public func set(_ newValue: Int) {
8585
lock.lock()
8686
defer { lock.unlock() }
8787
curValue = newValue
8888
}
8989

90-
func getAndSet(_ newValue: Int) -> Int {
90+
public func getAndSet(_ newValue: Int) -> Int {
9191
lock.lock()
9292
defer { lock.unlock() }
9393
let oldValue = curValue
9494
curValue = newValue
9595
return oldValue
9696
}
9797

98-
func mutate(_ transformation: (inout Int) -> Void) {
98+
public func mutate(_ transformation: (inout Int) -> Void) {
9999
lock.lock()
100100
transformation(&self.curValue)
101101
lock.unlock()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import Foundation

Sources/Concurrency/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Concurrency

Split/Common/Structs/SynchronizedDictionary.swift renamed to Sources/Concurrency/SynchronizedDictionary.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,70 @@
88

99
import Foundation
1010

11-
class SynchronizedDictionary<K: Hashable, T>: @unchecked Sendable {
11+
public class SynchronizedDictionary<K: Hashable, T>: @unchecked Sendable {
1212

1313
private var queue: DispatchQueue = DispatchQueue(label: "split-synchronized-dictionary", target: .global())
1414
private var items = [K: T]()
1515

16-
var keys: Set<K> {
16+
public init() {}
17+
18+
public var keys: Set<K> {
1719
queue.sync {
1820
let keys = items.keys
1921
return Set(keys.map { $0 as K})
2022
}
2123
}
22-
var all: [K: T] {
24+
public var all: [K: T] {
2325
var allItems: [K: T]?
2426
queue.sync {
2527
allItems = items
2628
}
2729
return allItems!
2830
}
2931

30-
var count: Int {
32+
public var count: Int {
3133
var count: Int = 0
3234
queue.sync {
3335
count = items.count
3436
}
3537
return count
3638
}
3739

38-
func value(forKey key: K) -> T? {
40+
public func value(forKey key: K) -> T? {
3941
var value: T?
4042
queue.sync {
4143
value = items[key]
4244
}
4345
return value
4446
}
4547

46-
func removeValue(forKey key: K) {
48+
public func removeValue(forKey key: K) {
4749
queue.sync {
4850
_ = items.removeValue(forKey: key)
4951
}
5052
}
5153

52-
func removeValues(forKeys keys: Dictionary<K, T>.Keys) {
54+
public func removeValues(forKeys keys: Dictionary<K, T>.Keys) {
5355
queue.sync {
5456
for key in keys {
5557
items.removeValue(forKey: key)
5658
}
5759
}
5860
}
5961

60-
func removeAll() {
62+
public func removeAll() {
6163
queue.sync {
6264
items.removeAll()
6365
}
6466
}
6567

66-
func setValue(_ value: T, forKey key: K) {
68+
public func setValue(_ value: T, forKey key: K) {
6769
queue.sync {
6870
items[key] = value
6971
}
7072
}
7173

72-
func setValues(_ values: [K: T]) {
74+
public func setValues(_ values: [K: T]) {
7375
queue.sync {
7476
items.removeAll()
7577
for (key, value) in values {
@@ -78,15 +80,15 @@ class SynchronizedDictionary<K: Hashable, T>: @unchecked Sendable {
7880
}
7981
}
8082

81-
func putValues(_ values: [K: T]) {
83+
public func putValues(_ values: [K: T]) {
8284
queue.sync {
8385
for (key, value) in values {
8486
items[key] = value
8587
}
8688
}
8789
}
8890

89-
func takeValue(forKey key: K) -> T? {
91+
public func takeValue(forKey key: K) -> T? {
9092
var value: T?
9193
queue.sync {
9294
value = items[key]
@@ -97,7 +99,7 @@ class SynchronizedDictionary<K: Hashable, T>: @unchecked Sendable {
9799
return value
98100
}
99101

100-
func takeAll() -> [K: T] {
102+
public func takeAll() -> [K: T] {
101103
var allItems: [K: T]!
102104
queue.sync {
103105
allItems = items

Split/Common/Structs/SynchronizedDictionaryComposed.swift renamed to Sources/Concurrency/SynchronizedDictionaryComposed.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,61 @@
88

99
import Foundation
1010

11-
class SynchronizedDictionaryComposed<K: Hashable, IK: Hashable>: @unchecked Sendable {
11+
public class SynchronizedDictionaryComposed<K: Hashable, IK: Hashable>: @unchecked Sendable {
1212

1313
private var queue: DispatchQueue = DispatchQueue(label: "split-synchronized-dictionary-composed",
1414
target: .global())
1515
private var items = [K: [IK: Any]]()
1616

17-
func count(forKey key: K) -> Int {
17+
public init() {}
18+
19+
public func count(forKey key: K) -> Int {
1820
var count: Int?
1921
queue.sync {
2022
count = items[key]?.count
2123
}
2224
return count ?? 0
2325
}
2426

25-
func values(forKey key: K) -> [IK: Any]? {
27+
public func values(forKey key: K) -> [IK: Any]? {
2628
var value: [IK: Any]?
2729
queue.sync {
2830
value = items[key]
2931
}
3032
return value
3133
}
3234

33-
func value(_ innerKey: IK, forKey key: K) -> Any? {
35+
public func value(_ innerKey: IK, forKey key: K) -> Any? {
3436
var value: Any?
3537
queue.sync {
3638
value = items[key]?[innerKey]
3739
}
3840
return value
3941
}
4042

41-
func contains(innerKey: IK, forKey key: K) -> Bool {
43+
public func contains(innerKey: IK, forKey key: K) -> Bool {
4244
var hasValue: Bool?
4345
queue.sync {
4446
hasValue = items[key]?.keys.contains(innerKey)
4547
}
4648
return hasValue ?? false
4749
}
4850

49-
func set(_ values: [IK: Any], forKey key: K) {
51+
public func set(_ values: [IK: Any], forKey key: K) {
5052
queue.sync {
5153
self.items[key] = values
5254
}
5355
}
5456

55-
func set(_ value: Any, forInnerKey innerKey: IK, forKey key: K) {
57+
public func set(_ value: Any, forInnerKey innerKey: IK, forKey key: K) {
5658
queue.sync {
5759
var values = self.items[key] ?? [:]
5860
values[innerKey] = value
5961
self.items[key] = values
6062
}
6163
}
6264

63-
func putValues(_ values: [IK: Any], forKey key: K) {
65+
public func putValues(_ values: [IK: Any], forKey key: K) {
6466
queue.sync {
6567
var newValues = self.items[key] ?? [:]
6668
for (innerKey, value) in values {
@@ -70,19 +72,19 @@ class SynchronizedDictionaryComposed<K: Hashable, IK: Hashable>: @unchecked Send
7072
}
7173
}
7274

73-
func removeValue(_ innerKey: IK, forKey key: K) {
75+
public func removeValue(_ innerKey: IK, forKey key: K) {
7476
queue.sync {
7577
_ = self.items[key]?.removeValue(forKey: innerKey)
7678
}
7779
}
7880

79-
func removeValues(forKey key: K) {
81+
public func removeValues(forKey key: K) {
8082
queue.sync {
8183
_ = self.items.removeValue(forKey: key)
8284
}
8385
}
8486

85-
func removeAll() {
87+
public func removeAll() {
8688
queue.sync {
8789
self.items.removeAll()
8890
}

0 commit comments

Comments
 (0)