Skip to content

Commit e96c4c1

Browse files
committed
Removed the actor from PQRSOSXFrontmostApplicationMonitor to ensure that the order of set_callback and trigger does not get reversed.
1 parent 1275435 commit e96c4c1

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

Diff for: include/pqrs/osx/frontmost_application_monitor/monitor.hpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ class monitor final : public dispatcher::extra::dispatcher_client {
2626
monitor(const monitor&) = delete;
2727

2828
monitor(std::weak_ptr<dispatcher::dispatcher> weak_dispatcher) : dispatcher_client(weak_dispatcher) {
29-
enqueue_to_dispatcher([] {
30-
pqrs_osx_frontmost_application_monitor_set_callback(static_cpp_callback);
31-
});
29+
pqrs_osx_frontmost_application_monitor_set_callback(static_cpp_callback);
3230
}
3331

3432
public:
3533
virtual ~monitor(void) {
36-
detach_from_dispatcher([] {
37-
pqrs_osx_frontmost_application_monitor_unset_callback();
38-
});
34+
pqrs_osx_frontmost_application_monitor_unset_callback();
35+
36+
detach_from_dispatcher();
3937
}
4038

4139
static void initialize_shared_monitor(std::weak_ptr<dispatcher::dispatcher> weak_dispatcher) {

Diff for: src/pqrs/osx/frontmost_application_monitor/PQRSOSXFrontmostApplicationMonitorImpl.swift

+21-21
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
// (See https://www.boost.org/LICENSE_1_0.txt)
44

55
import AppKit
6+
import os
67

7-
private actor PQRSOSXFrontmostApplicationMonitor {
8+
private class PQRSOSXFrontmostApplicationMonitor {
89
static let shared = PQRSOSXFrontmostApplicationMonitor()
910

1011
var callback: pqrs_osx_frontmost_application_monitor_callback?
12+
let lock = OSAllocatedUnfairLock()
1113

1214
init() {
1315
let sharedWorkspace = NSWorkspace.shared
@@ -34,27 +36,31 @@ private actor PQRSOSXFrontmostApplicationMonitor {
3436
let bundleIdentifier = runningApplication.bundleIdentifier ?? ""
3537
let path = runningApplication.executableURL?.path ?? ""
3638

37-
Task.detached {
38-
await self.runCallback(bundleIdentifier: bundleIdentifier, path: path)
39-
}
39+
self.runCallback(bundleIdentifier: bundleIdentifier, path: path)
4040
}
4141
}
4242

4343
func setCallback(_ callback: pqrs_osx_frontmost_application_monitor_callback) {
44-
self.callback = callback
44+
lock.withLock {
45+
self.callback = callback
46+
}
4547
}
4648

4749
func unsetCallback() {
48-
callback = nil
50+
lock.withLock {
51+
callback = nil
52+
}
4953
}
5054

5155
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+
}
5864
}
5965
}
6066
}
@@ -73,21 +79,15 @@ private actor PQRSOSXFrontmostApplicationMonitor {
7379
func PQRSOSXFrontmostApplicationMonitorSetCallback(
7480
_ callback: pqrs_osx_frontmost_application_monitor_callback
7581
) {
76-
Task.detached {
77-
await PQRSOSXFrontmostApplicationMonitor.shared.setCallback(callback)
78-
}
82+
PQRSOSXFrontmostApplicationMonitor.shared.setCallback(callback)
7983
}
8084

8185
@_cdecl("pqrs_osx_frontmost_application_monitor_unset_callback")
8286
func PQRSOSXFrontmostApplicationMonitorUnsetCallback() {
83-
Task.detached {
84-
await PQRSOSXFrontmostApplicationMonitor.shared.unsetCallback()
85-
}
87+
PQRSOSXFrontmostApplicationMonitor.shared.unsetCallback()
8688
}
8789

8890
@_cdecl("pqrs_osx_frontmost_application_monitor_trigger")
8991
func PQRSOSXFrontmostApplicationMonitorTrigger() {
90-
Task.detached {
91-
await PQRSOSXFrontmostApplicationMonitor.shared.runCallbackWithFrontmostApplication()
92-
}
92+
PQRSOSXFrontmostApplicationMonitor.shared.runCallbackWithFrontmostApplication()
9393
}

Diff for: tests/src/monitor_test.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ void run_monitor_test(void) {
1111
auto dispatcher = std::make_shared<pqrs::dispatcher::dispatcher>(time_source);
1212

1313
for (int i = 0; i < 10000; ++i) {
14-
printf("%d\n", i);
1514
auto wait = pqrs::make_thread_wait();
1615

1716
pqrs::osx::frontmost_application_monitor::monitor::initialize_shared_monitor(dispatcher);

0 commit comments

Comments
 (0)