input-capture: one lock for the tap's run loop and its thread
QA finding 1: `EventTap.thread` was a plain `var` read in `start()` and written in both `start()` and `stop()`, while the class comment claimed every cross-thread field was locked and `stop()` claimed to be safe from any thread (the contracts-C2 signal-handler recovery calls it off the tap thread). `runLoopLock` becomes `stateLock` and guards both fields; `start()` claims the slot and tests it in one critical section so two concurrent starts cannot both install a tap. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
2aa08946ad
commit
8af26751a8
1 file changed
+31
-15
@@ -59,10 +59,11 @@ public enum EventTapError: Error, Equatable {
|
||||
///
|
||||
/// `@unchecked Sendable`: every field that crosses a thread boundary is behind a
|
||||
/// lock — the routing snapshot and virtual cursor in `RoutingState`, the queue's
|
||||
/// own ring, the counters here, and `runLoop`, which `stop()` reads from another
|
||||
/// thread. Everything else (`machPort`, the modifier bookkeeping, the wheel
|
||||
/// remainders, the hotkey matcher) is created and touched only on the tap's own
|
||||
/// run-loop thread, which is the single thread the callback ever runs on. `self`
|
||||
/// own ring, the counters here, and the two lifecycle fields `runLoopStorage`
|
||||
/// and `thread`, which `stop()` reads and writes from another thread and which
|
||||
/// `stateLock` guards together. Everything else (`machPort`, the modifier
|
||||
/// bookkeeping, the wheel remainders, the hotkey matcher) is touched only on the
|
||||
/// tap's own run-loop thread, the single thread the callback ever runs on. `self`
|
||||
/// reaches the C callback as an unretained opaque pointer, so the tap must
|
||||
/// outlive the run loop — `stop()` is what ends it.
|
||||
public final class EventTap: @unchecked Sendable {
|
||||
@@ -76,8 +77,12 @@ public final class EventTap: @unchecked Sendable {
|
||||
private let screens: Screens
|
||||
private let onWake: @Sendable () -> Void
|
||||
private let counters = OSAllocatedUnfairLock(initialState: Counters())
|
||||
private let runLoopLock = NSLock()
|
||||
private let stateLock = NSLock()
|
||||
|
||||
// Both guarded by `stateLock`: `start()` runs on whatever thread the app
|
||||
// shell calls it from, `stop()` on any thread at all (a signal handler
|
||||
// included, contracts C2), and the tap thread clears the run loop as it
|
||||
// exits.
|
||||
private var runLoopStorage: CFRunLoop?
|
||||
private var thread: Thread?
|
||||
|
||||
@@ -109,34 +114,45 @@ public final class EventTap: @unchecked Sendable {
|
||||
/// Installs the tap on a dedicated run-loop thread and returns once it is
|
||||
/// live (contracts C3: never the main thread, so a busy UI cannot stall it).
|
||||
public func start() throws {
|
||||
guard thread == nil else { return }
|
||||
let ready = DispatchSemaphore(value: 0)
|
||||
let failure = OSAllocatedUnfairLock<EventTapError?>(initialState: nil)
|
||||
|
||||
// Claiming the slot and testing it are one step, so two `start()`s can
|
||||
// never both install a tap.
|
||||
stateLock.lock()
|
||||
guard thread == nil else {
|
||||
stateLock.unlock()
|
||||
return
|
||||
}
|
||||
let worker = Thread { [self] in run(ready: ready, failure: failure) }
|
||||
worker.name = "com.mwb-mac.event-tap"
|
||||
worker.qualityOfService = .userInteractive
|
||||
thread = worker
|
||||
stateLock.unlock()
|
||||
|
||||
worker.start()
|
||||
ready.wait()
|
||||
if let error = failure.withLock({ $0 }) {
|
||||
stateLock.lock()
|
||||
thread = nil
|
||||
stateLock.unlock()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the tap and lets its thread exit. Safe to call from any thread.
|
||||
public func stop() {
|
||||
runLoopLock.lock()
|
||||
stateLock.lock()
|
||||
let loop = runLoopStorage
|
||||
runLoopLock.unlock()
|
||||
if let loop { CFRunLoopStop(loop) }
|
||||
thread = nil
|
||||
stateLock.unlock()
|
||||
if let loop { CFRunLoopStop(loop) }
|
||||
}
|
||||
|
||||
/// Whether the tap thread is running.
|
||||
public var isRunning: Bool {
|
||||
runLoopLock.lock()
|
||||
defer { runLoopLock.unlock() }
|
||||
stateLock.lock()
|
||||
defer { stateLock.unlock() }
|
||||
return runLoopStorage != nil
|
||||
}
|
||||
|
||||
@@ -203,9 +219,9 @@ public final class EventTap: @unchecked Sendable {
|
||||
|
||||
machPort = port
|
||||
let loop = CFRunLoopGetCurrent()
|
||||
runLoopLock.lock()
|
||||
stateLock.lock()
|
||||
runLoopStorage = loop
|
||||
runLoopLock.unlock()
|
||||
stateLock.unlock()
|
||||
|
||||
CFRunLoopAddSource(loop, source, .commonModes)
|
||||
CGEvent.tapEnable(tap: port, enable: true)
|
||||
@@ -216,9 +232,9 @@ public final class EventTap: @unchecked Sendable {
|
||||
CFRunLoopRemoveSource(loop, source, .commonModes)
|
||||
CFMachPortInvalidate(port)
|
||||
machPort = nil
|
||||
runLoopLock.lock()
|
||||
stateLock.lock()
|
||||
runLoopStorage = nil
|
||||
runLoopLock.unlock()
|
||||
stateLock.unlock()
|
||||
}
|
||||
|
||||
// MARK: - The callback
|
||||
|
||||
Reference in new issue
Block a user