diff options
author | Damiano Galassi <[email protected]> | 2019-08-24 09:08:21 +0200 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2019-08-24 09:08:21 +0200 |
commit | 98edbce7b4a8fee679c91c94df00643ccf06cc1b (patch) | |
tree | 2d6d58e8ae6ab8affba1d90f5b32f874e32cf5e2 /macosx | |
parent | a58118b81ac79e1073e23115729a649f966dc366 (diff) |
MacGui: workaround a issue in 10.15 beta where the queue file modification date is bigger than the date we get after writing it to disk.
Diffstat (limited to 'macosx')
-rw-r--r-- | macosx/HBDistributedArray.m | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/macosx/HBDistributedArray.m b/macosx/HBDistributedArray.m index 1abce6094..438f81c87 100644 --- a/macosx/HBDistributedArray.m +++ b/macosx/HBDistributedArray.m @@ -64,6 +64,8 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; @property (nonatomic, readonly) sem_t *mutex; @property (nonatomic, readwrite) uint32_t mutexCount; +@property (nonatomic, readwrite) BOOL multipleInstances; + @end @implementation HBDistributedArray @@ -98,9 +100,14 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; [HBUtilities writeToActivityLog:"%s: %d", "Error in creating semaphore: ", errno]; } - [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:HBDistributedArraWrittenToDisk object:nil]; + [self lock]; + NSUInteger instances = [NSRunningApplication runningApplicationsWithBundleIdentifier:NSBundle.mainBundle.bundleIdentifier].count; + _multipleInstances = instances > 1; + [self unlock]; - if ([[NSFileManager defaultManager] fileExistsAtPath:_fileURL.path]) + [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(handleNotification:) name:HBDistributedArraWrittenToDisk object:nil]; + + if ([NSFileManager.defaultManager fileExistsAtPath:_fileURL.path]) { // Load the array from disk [self lock]; @@ -114,7 +121,7 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; - (void)dealloc { - [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; + [NSDistributedNotificationCenter.defaultCenter removeObserver:self]; [self lock]; [self synchronize]; @@ -123,7 +130,7 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; sem_close(_mutex); } -- (void)lock +- (uint32_t)lock { if (self.mutexCount == 0) { @@ -131,6 +138,7 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; } self.mutexCount++; + return self.mutexCount; } - (void)unlock @@ -145,19 +153,23 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; - (HBDistributedArrayContent)beginTransaction { - [self lock]; + BOOL alreadyLocked = [self lock] > 1; // We got the lock, need to check if // someone else modified the file // while we were locked, because we // could have not received the notification yet - NSDate *date = nil; - [self.fileURL getResourceValue:&date forKey:NSURLAttributeModificationDateKey error:nil]; - if (date.timeIntervalSinceReferenceDate > ceil(self.modifiedTime)) + if (alreadyLocked == false && self.multipleInstances) { - // File was modified while we waited on the lock - // reload it - [self reload]; - return HBDistributedArrayContentReload; + NSDate *date = nil; + [self.fileURL getResourceValue:&date forKey:NSURLAttributeModificationDateKey error:nil]; + + if (date.timeIntervalSinceReferenceDate > self.modifiedTime) + { + // File was modified while we waited on the lock + // reload it + [self reload]; + return HBDistributedArrayContentReload; + } } return HBDistributedArrayContentAcquired; @@ -183,6 +195,7 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk"; { if (!([notification.object integerValue] == getpid())) { + self.multipleInstances = YES; [self lock]; [self reload]; [self unlock]; |