diff --git a/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift b/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift index 449cec717..23c5f68e3 100644 --- a/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/ElectronDocument.swift @@ -11,7 +11,7 @@ import AudioToolbox class ElectronDocument: MachineDocument { - private var electron = CSElectron() + private var electron: CSElectron! = CSElectron() override init() { super.init() self.intendedCyclesPerSecond = 2000000 @@ -55,9 +55,21 @@ class ElectronDocument: MachineDocument { electron.setROM(data, slot: 15) } + override func close() { + objc_sync_enter(self) + electron.sync() + openGLView.invalidate() + openGLView.openGLContext!.makeCurrentContext() + electron = nil + super.close() + objc_sync_exit(self) + } + // MARK: CSOpenGLViewDelegate override func runForNumberOfCycles(numberOfCycles: Int32) { - electron.runForNumberOfCycles(numberOfCycles) + objc_sync_enter(self) + electron?.runForNumberOfCycles(numberOfCycles) + objc_sync_exit(self) } override func openGLView(view: CSCathodeRayView, drawViewOnlyIfDirty onlyIfDirty: Bool) { @@ -78,5 +90,4 @@ class ElectronDocument: MachineDocument { electron.setKey(kVK_Control, isPressed: newModifiers.modifierFlags.contains(.ControlKeyMask)) electron.setKey(kVK_Command, isPressed: newModifiers.modifierFlags.contains(.CommandKeyMask)) } - } diff --git a/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.h b/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.h index 9ce936fe2..e75f14bfb 100644 --- a/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.h +++ b/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.h @@ -13,6 +13,7 @@ @interface CSMachine : NSObject - (void)runForNumberOfCycles:(int)numberOfCycles; +- (void)sync; @property (nonatomic, weak) CSCathodeRayView *view; @property (nonatomic, weak) AudioQueue *audioQueue; diff --git a/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.mm b/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.mm index a237f1eb7..79a8fcb4e 100644 --- a/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.mm +++ b/OSBindings/Mac/Clock Signal/Wrappers/CSMachine.mm @@ -47,6 +47,10 @@ typedef NS_ENUM(NSInteger, CSAtari2600RunningState) { } } +- (void)sync { + dispatch_sync(_serialDispatchQueue, ^{}); +} + - (instancetype)init { self = [super init];