Cache is now flushed synchronously when quitting. If an async flush is in progress at the time, it is simply allowed to complete.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@2538 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2009-09-19 22:21:35 +00:00
parent c988131375
commit 4c54a93cfb
4 changed files with 26 additions and 8 deletions

View File

@ -898,6 +898,7 @@ static NSComparisonResult CompareDisplayModes(id arg1, id arg2, void *context)
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
[[OOCacheManager sharedCache] flushSynchronously];
OOLoggingTerminate();
return NSTerminateNow;
}

View File

@ -213,6 +213,8 @@ static void InitAsyncWorkManager(void)
- (void) waitForTaskToComplete:(id<OOAsyncWorkTask>)task
{
if (task == nil) return;
#if OO_DEBUG
NSParameterAssert([(id)task respondsToSelector:@selector(completeAsyncTask)]);
NSAssert1(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread self] isMainThread], @"%s can only be called from the main thread.", __FUNCTION__);

View File

@ -45,8 +45,8 @@ enum
{
@private
NSMutableDictionary *_caches;
id _scheduledWrite;
BOOL _permitWrites;
BOOL _writeScheduled;
}
+ (id)sharedCache;
@ -69,5 +69,6 @@ enum
- (void)setAllowCacheWrites:(BOOL)flag;
- (void)flush;
- (void)flushSynchronously;
@end

View File

@ -292,11 +292,22 @@ static OOCacheManager *sSingleton = nil;
- (void)flush
{
if (_permitWrites && [self dirty] && _scheduledWrite == nil)
{
[self write];
[self markClean];
}
}
- (void)flushSynchronously
{
if (_permitWrites && [self dirty])
{
[self write];
[self markClean];
[[OOAsyncWorkManager sharedAsyncWorkManager] waitForTaskToComplete:_scheduledWrite];
}
}
@ -394,7 +405,7 @@ static OOCacheManager *sSingleton = nil;
uint64_t endianTagValue = kEndianTagValue;
if (_caches == nil) return;
if (_writeScheduled) return;
if (_scheduledWrite != nil) return;
#if PRUNE_BEFORE_FLUSH
[[_caches allValues] makeObjectsPerformSelector:@selector(prune)];
@ -418,10 +429,8 @@ static OOCacheManager *sSingleton = nil;
[newCache setObject:endianTag forKey:kCacheKeyEndianTag];
[newCache setObject:pListRep forKey:kCacheKeyCaches];
OOAsyncCacheWriter *writer = [[OOAsyncCacheWriter alloc] initWithCacheContents:newCache];
_writeScheduled = YES;
[[OOAsyncWorkManager sharedAsyncWorkManager] addTask:writer priority:kOOAsyncPriorityLow];
[writer release];
_scheduledWrite = [[OOAsyncCacheWriter alloc] initWithCacheContents:newCache];
[[OOAsyncWorkManager sharedAsyncWorkManager] addTask:_scheduledWrite priority:kOOAsyncPriorityLow];
}
@ -514,7 +523,7 @@ static OOCacheManager *sSingleton = nil;
}
BOOL result = [plist writeToFile:path atomically:NO];
_writeScheduled = NO;
DESTROY(_scheduledWrite);
return result;
}
@ -794,7 +803,6 @@ static OOCacheManager *sSingleton = nil;
{
if ([[OOCacheManager sharedCache] writeDict:_cacheContents])
{
[[OOCacheManager sharedCache] markClean];
OOLog(kOOLogDataCacheWriteSuccess, @"Wrote data cache.");
}
else
@ -804,4 +812,10 @@ static OOCacheManager *sSingleton = nil;
DESTROY(_cacheContents);
}
- (void) completeAsyncTask
{
// Don't need to do anything, but this needs to be here so we can wait on it.
}
@end