1- import Foundation
1+ @ preconcurrency import Foundation
22import Synchronized
33
4- public class DispatchTimer {
4+ public final class DispatchTimer : Sendable {
55 private let source : DispatchSourceTimer
6- private let block : ( ) -> Void
7- private let lock = Lock ( )
6+ private let block : @Sendable ( ) -> Void
87
98 public let isRepeating : Bool
109
11- public var nextDeadline : DispatchTime { lock. locked { return _nextDeadline } }
12- private var _nextDeadline : DispatchTime
10+ public var nextDeadline : DispatchTime {
11+ _nextDeadline. access { $0 }
12+ }
13+ private let _nextDeadline : Locked < DispatchTime >
1314
1415 public init (
1516 _ interval: DispatchTimeInterval ,
1617 repeat shouldRepeat: Bool = false ,
17- block: @escaping ( ) -> Void
18+ block: @escaping @ Sendable ( ) -> Void
1819 ) {
1920 self . source = DispatchSource . makeTimerSource ( )
2021 self . block = block
2122 self . isRepeating = shouldRepeat
2223
2324 let deadline = DispatchTime . now ( ) . advanced ( by: interval)
24- _nextDeadline = deadline
25+ _nextDeadline = Locked ( deadline)
2526 let repeating : DispatchTimeInterval = shouldRepeat ? interval : . never
2627 source. schedule (
2728 deadline: deadline,
@@ -34,16 +35,21 @@ public class DispatchTimer {
3435 self . fire ( )
3536
3637 guard shouldRepeat else { return }
37- self . lock. locked { self . _nextDeadline = DispatchTime . now ( ) . advanced ( by: interval) }
38+ self . _nextDeadline. access {
39+ $0 = DispatchTime . now ( ) . advanced ( by: interval)
40+ }
3841 }
3942 source. activate ( )
4043 }
4144
42- public init ( fireAt deadline: DispatchTime , block: @escaping ( ) -> Void ) {
45+ public init (
46+ fireAt deadline: DispatchTime ,
47+ block: @escaping @Sendable ( ) -> Void
48+ ) {
4349 self . source = DispatchSource . makeTimerSource ( )
4450 self . block = block
4551 self . isRepeating = false
46- _nextDeadline = deadline
52+ _nextDeadline = Locked ( deadline)
4753
4854 let interval = DispatchTime . now ( ) . distance ( to: deadline)
4955
0 commit comments