From 7050e421c74c29e909def2938587c57a789beda1 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Mon, 10 Nov 2003 06:40:24 +0000 Subject: [PATCH] Don't obtain locks unnecessarily git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18071 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 +++ Source/NSUserDefaults.m | 85 +++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc3843bee..9d49ad207 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2003-11-10 Richard Frith-Macdonald + + * Source/NSUserDefaults.m: Only obtain distributed lock for reading + or writing defaults ... not needed just to check the modification + date. Avoids unnecessary filesystem writes. + 2003-11-09 Frederic De Jaeger * Source/GSFileHandle.m ([GSFileHandle diff --git a/Source/NSUserDefaults.m b/Source/NSUserDefaults.m index ec9e24893..407599f07 100644 --- a/Source/NSUserDefaults.m +++ b/Source/NSUserDefaults.m @@ -1328,6 +1328,48 @@ static BOOL isPlistObject(id o) [_lock lock]; + /* + * If we haven't changed anything, we only need to synchronise if + * the on-disk database has been changed by someone else. + */ + attr = [mgr fileAttributesAtPath: _defaultsDatabase + traverseLink: YES]; + if (_changedDomains == nil) + { + BOOL wantRead = NO; + + if (_lastSync == nil) + { + wantRead = YES; + } + else + { + if (attr == nil) + { + wantRead = YES; + } + else + { + NSDate *mod; + + /* + * If the database was modified since the last synchronisation + * we need to read it. + */ + mod = [attr objectForKey: NSFileModificationDate]; + if (mod != nil && [_lastSync laterDate: mod] != _lastSync) + { + wantRead = YES; + } + } + } + if (wantRead == NO) + { + [_lock unlock]; + return YES; + } + } + wasLocked = isLocked; if (isLocked == NO && _fileLock != nil) { @@ -1373,51 +1415,10 @@ static BOOL isPlistObject(id o) } /* - * If we haven't changed anything, we only need to synchronise if - * the on-disk database has been changed by someone else. + * Re-fetch database attributes in cased they changed while obtaining lock. */ attr = [mgr fileAttributesAtPath: _defaultsDatabase traverseLink: YES]; - if (_changedDomains == nil) - { - BOOL wantRead = NO; - - if (_lastSync == nil) - { - wantRead = YES; - } - else - { - if (attr == nil) - { - wantRead = YES; - } - else - { - NSDate *mod; - - /* - * If the database was modified since the last synchronisation - * we need to read it. - */ - mod = [attr objectForKey: NSFileModificationDate]; - if (mod != nil && [_lastSync laterDate: mod] != _lastSync) - { - wantRead = YES; - } - } - } - if (wantRead == NO) - { - if (wasLocked == NO) - { - [_fileLock unlock]; - isLocked = NO; - } - [_lock unlock]; - return YES; - } - } DESTROY(_dictionaryRep);