Skip to content

Commit 94e1709

Browse files
authored
Lock suspend and resume (#49)
1 parent dec6896 commit 94e1709

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Sources/NautilusTelemetry/Utilities/FlushTimer.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright © 2025 Airbnb Inc. All rights reserved.
33

44
import Foundation
5+
import os
56

67
// MARK: - FlushTimer
78

@@ -42,10 +43,12 @@ class FlushTimer {
4243
}
4344

4445
func suspend() {
45-
if !suspended {
46-
// Must match calls between suspend/resume
47-
flushTimer.suspend()
48-
suspended = true
46+
lock.withLock {
47+
if !suspended {
48+
// Must match calls between suspend/resume
49+
flushTimer.suspend()
50+
suspended = true
51+
}
4952
}
5053
}
5154

@@ -59,16 +62,18 @@ class FlushTimer {
5962
leeway: DispatchTimeInterval.milliseconds(100)
6063
)
6164
flushTimer.activate()
62-
if suspended {
63-
flushTimer.resume()
64-
suspended = false
65+
lock.withLock {
66+
if suspended {
67+
flushTimer.resume()
68+
suspended = false
69+
}
6570
}
6671
}
6772

6873
// MARK: Private
6974

7075
private var _flushInterval: TimeInterval
71-
76+
private var lock = OSAllocatedUnfairLock()
7277
}
7378

7479
extension DispatchTimeInterval {

0 commit comments

Comments
 (0)