Create lock in +initialize so it can safely be used to prevent threads in the

same process from trying to obtain the distributed lock concurrently.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@40174 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2016-10-24 13:45:56 +00:00
parent 9a8495c0d6
commit 1a74105054

View file

@ -111,6 +111,7 @@ static NSImage *multipleFiles = nil;
static NSImage *unknownApplication = nil; static NSImage *unknownApplication = nil;
static NSImage *unknownTool = nil; static NSImage *unknownTool = nil;
NSLock *mlock = nil;
static NSString *GSWorkspaceNotification = @"GSWorkspaceNotification"; static NSString *GSWorkspaceNotification = @"GSWorkspaceNotification";
static NSString *GSWorkspacePreferencesChanged = static NSString *GSWorkspacePreferencesChanged =
@ -134,9 +135,7 @@ static id GSLaunched(NSNotification *notification, BOOL active)
NSDictionary *apps = nil; NSDictionary *apps = nil;
BOOL modified = NO; BOOL modified = NO;
unsigned sleeps = 0; unsigned sleeps = 0;
NSLock *mlock = nil;
mlock = [[NSLock alloc] init];
[mlock lock]; // start critical section [mlock lock]; // start critical section
if (path == nil) if (path == nil)
{ {
@ -177,6 +176,7 @@ static id GSLaunched(NSNotification *notification, BOOL active)
} }
if (sleeps >= 10) if (sleeps >= 10)
{ {
[mlock unlock];
NSLog(@"Unable to obtain lock %@", lock); NSLog(@"Unable to obtain lock %@", lock);
return nil; return nil;
} }
@ -282,7 +282,7 @@ static id GSLaunched(NSNotification *notification, BOOL active)
NSLog(@"Unable to unlock %@", lock); NSLog(@"Unable to unlock %@", lock);
break; break;
} }
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]]; [NSThread sleepForTimeInterval: 0.1];
continue; continue;
} }
NS_ENDHANDLER; NS_ENDHANDLER;
@ -290,7 +290,6 @@ static id GSLaunched(NSNotification *notification, BOOL active)
} }
NS_ENDHANDLER; NS_ENDHANDLER;
[mlock unlock]; // end critical section [mlock unlock]; // end critical section
[mlock release];
if (active == YES) if (active == YES)
{ {
@ -595,6 +594,7 @@ static NSDictionary *urlPreferences = nil;
} }
beenHere = YES; beenHere = YES;
mlock = [NSLock new];
NS_DURING NS_DURING
{ {