summaryrefslogtreecommitdiffstats
path: root/macosx/HBDistributedArray.m
diff options
context:
space:
mode:
authorDamiano Galassi <[email protected]>2015-10-19 17:52:32 +0200
committerDamiano Galassi <[email protected]>2015-10-19 17:52:32 +0200
commite5b59be057da16e782d970e41299d78beef9e878 (patch)
treea4f84643b50cfd72f006cefd693d4ddc8b5de5d5 /macosx/HBDistributedArray.m
parent8720ad99a5a74a96a8413d339b0eda140bbae2bc (diff)
MacGui: add a recursive lock to HBDistributedArray.
Diffstat (limited to 'macosx/HBDistributedArray.m')
-rw-r--r--macosx/HBDistributedArray.m28
1 files changed, 23 insertions, 5 deletions
diff --git a/macosx/HBDistributedArray.m b/macosx/HBDistributedArray.m
index 704f828c4..867ee4ffb 100644
--- a/macosx/HBDistributedArray.m
+++ b/macosx/HBDistributedArray.m
@@ -59,6 +59,7 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk";
@property (nonatomic, readwrite) NSTimeInterval modifiedTime;
@property (nonatomic, readonly) sem_t *mutex;
+@property (nonatomic, readwrite) uint32_t mutexCount;
@end
@@ -87,7 +88,8 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk";
// it can cause a deadlock if an instance
// crashed while it has the lock on the semaphore.
_mutex = sem_open(name, O_CREAT, 0777, 1);
- if (_mutex == SEM_FAILED) {
+ if (_mutex == SEM_FAILED)
+ {
NSLog(@"%s: %d\n", "Error in creating semaphore: ", errno);
}
@@ -118,15 +120,25 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk";
- (void)lock
{
- sem_wait(self.mutex);
+ if (self.mutexCount == 0)
+ {
+ sem_wait(self.mutex);
+ }
+
+ self.mutexCount++;
}
- (void)unlock
{
- sem_post(self.mutex);
+ if (self.mutexCount == 1)
+ {
+ sem_post(self.mutex);
+ }
+
+ self.mutexCount--;
}
-- (void)beginTransaction
+- (HBDistributedArrayContent)beginTransaction
{
[self lock];
// We got the lock, need to check if
@@ -135,16 +147,22 @@ NSString *HBDistributedArraWrittenToDisk = @"HBDistributedArraWrittenToDisk";
// could have not received the notification yet
NSDate *date = nil;
[self.fileURL getResourceValue:&date forKey:NSURLAttributeModificationDateKey error:nil];
- if (date.timeIntervalSinceReferenceDate > self.modifiedTime)
+ if (date.timeIntervalSinceReferenceDate > ceil(self.modifiedTime))
{
// File was modified while we waited on the lock
// reload it
[self reload];
+ NSLog(@"WTF");
+ return HBDistributedArrayContentReload;
}
+
+ return HBDistributedArrayContentAcquired;
}
- (void)commit
{
+ // Save changes to disk
+ // and unlock
[self synchronize];
[self unlock];
}