Skip to content

Instantly share code, notes, and snippets.

@yfrancis
Created September 29, 2012 19:03
Show Gist options
  • Save yfrancis/3804924 to your computer and use it in GitHub Desktop.
Save yfrancis/3804924 to your computer and use it in GitHub Desktop.
static NSMutableDictionary* resources_ = nil;
@implementation GRResource
+ (void)initialize
{
static dispatch_once_t once;
dispatch_once(&once, ^{
resources_ = [NSMutableDictionary new];
});
}
+ (GRResource*)_resourceWithName:(NSString*)resourceName
{
@synchronized(resources_) {
GRResource* r = [resources_ objectForKey:resourceName];
if (r == nil) {
r = [[[GRResource alloc] _initWithName:name] autorelease];
[resources_ setObject:r forKey:resourceName];
}
}
return r;
}
- (id)_initWithName:(NSString*)name
{
self = [super init];
if (self != nil)
self.name = name;
return self;
}
+ (void)acquireResources:(NSArray*)resources
{
for (NSString* resourceName in resources)
[[GRResource _resourceWithName:resourceName] lock];
}
+ (void)relinquishResources:(NSArray*)resources
{
for (NSString* resourceName in resources)
[[GRResource _resourceWithName:resourceName] unlock];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment