From 5f99f4442cdef9e70cd203828d083ff810bcf278 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 30 Sep 2017 20:07:04 -0400 Subject: [PATCH] Ensures safe machine release upon window closure. --- .../Documents/MachineDocument.swift | 9 +++++- .../Updater/CSBestEffortUpdater.m | 31 ++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift index 4949e9989..605db2f06 100644 --- a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift @@ -95,9 +95,16 @@ class MachineDocument: } override func close() { - bestEffortUpdater.flush() + optionsPanel?.setIsVisible(false) + optionsPanel = nil + + openGLView.delegate = nil + bestEffortUpdater.delegate = nil + bestEffortUpdater = nil + actionLock.lock() drawLock.lock() + machine = nil openGLView.invalidate() openGLView.openGLContext!.makeCurrentContext() actionLock.unlock() diff --git a/OSBindings/Mac/Clock Signal/Updater/CSBestEffortUpdater.m b/OSBindings/Mac/Clock Signal/Updater/CSBestEffortUpdater.m index eeb24c99e..0361498a9 100644 --- a/OSBindings/Mac/Clock Signal/Updater/CSBestEffortUpdater.m +++ b/OSBindings/Mac/Clock Signal/Updater/CSBestEffortUpdater.m @@ -20,6 +20,7 @@ NSTimeInterval _previousTimeInterval; NSTimeInterval _cyclesError; BOOL _hasSkipped; + id _delegate; } - (instancetype)init @@ -35,15 +36,12 @@ return self; } -- (void)update -{ +- (void)update { // Always post an -openGLView:didUpdateToTime: if a previous one isn't still ongoing. This is the hook upon which the substantial processing occurs. - if(!atomic_flag_test_and_set(&_updateIsOngoing)) - { + if(!atomic_flag_test_and_set(&_updateIsOngoing)) { dispatch_async(_serialDispatchQueue, ^{ NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate]; - if(_previousTimeInterval > DBL_EPSILON && timeInterval > _previousTimeInterval) - { + if(_previousTimeInterval > DBL_EPSILON && timeInterval > _previousTimeInterval) { NSTimeInterval timeToRunFor = timeInterval - _previousTimeInterval; double cyclesToRunFor = timeToRunFor * self.clockRate + _cyclesError; @@ -58,18 +56,29 @@ _hasSkipped = NO; atomic_flag_clear(&_updateIsOngoing); }); - } - else - { + } else { dispatch_async(_serialDispatchQueue, ^{ _hasSkipped = YES; }); } } -- (void)flush -{ +- (void)flush { dispatch_sync(_serialDispatchQueue, ^{}); } +- (void)setDelegate:(id)delegate { + dispatch_sync(_serialDispatchQueue, ^{ + _delegate = delegate; + }); +} + +- (id)delegate { + __block id delegate; + dispatch_sync(_serialDispatchQueue, ^{ + delegate = _delegate; + }); + return delegate; +} + @end