1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
import Cocoa
struct Timing {
let time: UInt64
let closure: () -> ()
}
class PreciseTimer {
unowned var common: Common
let nanoPerSecond: Double = 1e+9
let machToNano: Double = {
var timebase: mach_timebase_info = mach_timebase_info()
mach_timebase_info(&timebase)
return Double(timebase.numer) / Double(timebase.denom)
}()
let condition = NSCondition()
var events: [Timing] = []
var isRunning: Bool = true
var isHighPrecision: Bool = false
var thread: pthread_t!
var threadPort: thread_port_t = thread_port_t()
let policyFlavor = thread_policy_flavor_t(THREAD_TIME_CONSTRAINT_POLICY)
let policyCount = MemoryLayout<thread_time_constraint_policy>.size /
MemoryLayout<integer_t>.size
var typeNumber: mach_msg_type_number_t {
return mach_msg_type_number_t(policyCount)
}
var threadAttr: pthread_attr_t = {
var attr = pthread_attr_t()
var param = sched_param()
pthread_attr_init(&attr)
param.sched_priority = sched_get_priority_max(SCHED_FIFO)
pthread_attr_setschedparam(&attr, ¶m)
pthread_attr_setschedpolicy(&attr, SCHED_FIFO)
return attr
}()
init?(common com: Common) {
common = com
pthread_create(&thread, &threadAttr, entryC, TypeHelper.bridge(obj: self))
if thread == nil {
common.log.warning("Couldn't create pthread for high precision timer")
return nil
}
threadPort = pthread_mach_thread_np(thread)
}
func updatePolicy(periodSeconds: Double = 1 / 60.0) {
let period = periodSeconds * nanoPerSecond / machToNano
var policy = thread_time_constraint_policy(
period: UInt32(period),
computation: UInt32(0.75 * period),
constraint: UInt32(0.85 * period),
preemptible: 1
)
let success = withUnsafeMutablePointer(to: &policy) {
$0.withMemoryRebound(to: integer_t.self, capacity: policyCount) {
thread_policy_set(threadPort, policyFlavor, $0, typeNumber)
}
}
isHighPrecision = success == KERN_SUCCESS
if !isHighPrecision {
common.log.warning("Couldn't create a high precision timer")
}
}
func terminate() {
condition.lock()
isRunning = false
condition.signal()
condition.unlock()
pthread_kill(thread, SIGALRM)
pthread_join(thread, nil)
}
func scheduleAt(time: UInt64, closure: @escaping () -> ()) {
condition.lock()
let firstEventTime = events.first?.time ?? 0
let lastEventTime = events.last?.time ?? 0
events.append(Timing(time: time, closure: closure))
if lastEventTime > time {
events.sort{ $0.time < $1.time }
}
condition.signal()
condition.unlock()
if firstEventTime > time {
pthread_kill(thread, SIGALRM)
}
}
let threadSignal: @convention(c) (Int32) -> () = { (sig: Int32) in }
let entryC: @convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? = { (ptr: UnsafeMutableRawPointer) in
let ptimer: PreciseTimer = TypeHelper.bridge(ptr: ptr)
ptimer.entry()
return nil
}
func entry() {
signal(SIGALRM, threadSignal)
while isRunning {
condition.lock()
while events.count == 0 && isRunning {
condition.wait()
}
if !isRunning { break }
guard let event = events.first else {
continue
}
condition.unlock()
mach_wait_until(event.time)
condition.lock()
if events.first?.time == event.time && isRunning {
event.closure()
events.removeFirst()
}
condition.unlock()
}
}
}
|