3
3
// (See https://www.boost.org/LICENSE_1_0.txt)
4
4
5
5
import AppKit
6
+ import os
6
7
7
- private actor PQRSOSXFrontmostApplicationMonitor {
8
+ private class PQRSOSXFrontmostApplicationMonitor {
8
9
static let shared = PQRSOSXFrontmostApplicationMonitor ( )
9
10
10
11
var callback : pqrs_osx_frontmost_application_monitor_callback ?
12
+ let lock = OSAllocatedUnfairLock ( )
11
13
12
14
init ( ) {
13
15
let sharedWorkspace = NSWorkspace . shared
@@ -34,27 +36,31 @@ private actor PQRSOSXFrontmostApplicationMonitor {
34
36
let bundleIdentifier = runningApplication. bundleIdentifier ?? " "
35
37
let path = runningApplication. executableURL? . path ?? " "
36
38
37
- Task . detached {
38
- await self . runCallback ( bundleIdentifier: bundleIdentifier, path: path)
39
- }
39
+ self . runCallback ( bundleIdentifier: bundleIdentifier, path: path)
40
40
}
41
41
}
42
42
43
43
func setCallback( _ callback: pqrs_osx_frontmost_application_monitor_callback ) {
44
- self . callback = callback
44
+ lock. withLock {
45
+ self . callback = callback
46
+ }
45
47
}
46
48
47
49
func unsetCallback( ) {
48
- callback = nil
50
+ lock. withLock {
51
+ callback = nil
52
+ }
49
53
}
50
54
51
55
func runCallback( bundleIdentifier: String , path: String ) {
52
- bundleIdentifier. utf8CString. withUnsafeBufferPointer { bundleIdentifierPtr in
53
- path. utf8CString. withUnsafeBufferPointer { pathPtr in
54
- callback ? (
55
- bundleIdentifierPtr. baseAddress,
56
- pathPtr. baseAddress
57
- )
56
+ lock. withLock {
57
+ bundleIdentifier. utf8CString. withUnsafeBufferPointer { bundleIdentifierPtr in
58
+ path. utf8CString. withUnsafeBufferPointer { pathPtr in
59
+ callback ? (
60
+ bundleIdentifierPtr. baseAddress,
61
+ pathPtr. baseAddress
62
+ )
63
+ }
58
64
}
59
65
}
60
66
}
@@ -73,21 +79,15 @@ private actor PQRSOSXFrontmostApplicationMonitor {
73
79
func PQRSOSXFrontmostApplicationMonitorSetCallback(
74
80
_ callback: pqrs_osx_frontmost_application_monitor_callback
75
81
) {
76
- Task . detached {
77
- await PQRSOSXFrontmostApplicationMonitor . shared. setCallback ( callback)
78
- }
82
+ PQRSOSXFrontmostApplicationMonitor . shared. setCallback ( callback)
79
83
}
80
84
81
85
@_cdecl ( " pqrs_osx_frontmost_application_monitor_unset_callback " )
82
86
func PQRSOSXFrontmostApplicationMonitorUnsetCallback( ) {
83
- Task . detached {
84
- await PQRSOSXFrontmostApplicationMonitor . shared. unsetCallback ( )
85
- }
87
+ PQRSOSXFrontmostApplicationMonitor . shared. unsetCallback ( )
86
88
}
87
89
88
90
@_cdecl ( " pqrs_osx_frontmost_application_monitor_trigger " )
89
91
func PQRSOSXFrontmostApplicationMonitorTrigger( ) {
90
- Task . detached {
91
- await PQRSOSXFrontmostApplicationMonitor . shared. runCallbackWithFrontmostApplication ( )
92
- }
92
+ PQRSOSXFrontmostApplicationMonitor . shared. runCallbackWithFrontmostApplication ( )
93
93
}
0 commit comments