File tree Expand file tree Collapse file tree 2 files changed +15
-18
lines changed
Sources/Segment/Utilities Expand file tree Collapse file tree 2 files changed +15
-18
lines changed Original file line number Diff line number Diff line change 7
7
8
8
import Foundation
9
9
10
+ // NOTE: Revised from previous implementation which used a struct and NSLock's.
11
+ // Thread Sanitizer was *correctly* capturing this issue, which was a little obscure
12
+ // given the property wrapper PLUS the semantics of a struct. Moving to `class`
13
+ // removes the semantics problem and lets TSan approve of what's happening.
14
+ //
15
+ // Additionally, moving to a lock free version is just desirable, so moved to a queue.
16
+ //
17
+ // Also see thread here: https://github.com/apple/swift-evolution/pull/1387
18
+
10
19
@propertyWrapper
11
- public struct Atomic < T> {
12
- var value : T
13
- private let lock = NSLock ( )
20
+ public class Atomic < T> {
21
+ private var value : T
22
+ private let queue = DispatchQueue ( label : " com.segment.atomic. \( UUID ( ) . uuidString ) " )
14
23
15
24
public init ( wrappedValue value: T ) {
16
25
self . value = value
17
26
}
18
27
19
28
public var wrappedValue : T {
20
- get { return load ( ) }
21
- set { store ( newValue: newValue) }
22
- }
23
-
24
- func load( ) -> T {
25
- lock. lock ( )
26
- defer { lock. unlock ( ) }
27
- return value
28
- }
29
-
30
- mutating func store( newValue: T ) {
31
- lock. lock ( )
32
- defer { lock. unlock ( ) }
33
- value = newValue
29
+ get { return queue. sync { return value } }
30
+ set { queue. sync { value = newValue } }
34
31
}
35
32
}
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import Foundation
10
10
public class CountBasedFlushPolicy : FlushPolicy {
11
11
public weak var analytics : Analytics ?
12
12
internal var desiredCount : Int ?
13
- internal var count : Int = 0
13
+ @ Atomic internal var count : Int = 0
14
14
15
15
init ( ) { }
16
16
You can’t perform that action at this time.
0 commit comments