-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDTSpookyNexus.m
58 lines (49 loc) · 1.12 KB
/
DTSpookyNexus.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#import "DTSpookyNexus.h"
#import <libkern/OSAtomic.h>
static NSMutableDictionary *spookyGlobalDictionary = nil;
static volatile DTSpookyNexus *_spookyNexusManager = nil;
static OSSpinLock nexusLock = OS_SPINLOCK_INIT;
static volatile uint32_t nexusUseCount = 0;
@implementation DTSpookyNexus
+(id)sharedNexusManager
{
OSSpinLockLock(&nexusLock);
{
if(!_spookyNexusManager)
{
_spookyNexusManager = [DTSpookyNexus new];
spookyGlobalDictionary = [NSMutableDictionary new];
}
++nexusUseCount;
}
OSSpinLockUnlock(&nexusLock);
return _spookyNexusManager;
}
-(void)unuse
{
OSSpinLockLock(&nexusLock);
{
--nexusUseCount;
if(nexusUseCount == 0)
{
[_spookyNexusManager release];
[spookyGlobalDictionary release];
_spookyNexusManager = nil;
spookyGlobalDictionary = nil; // so GC will clean it up
}
}
OSSpinLockUnlock(&nexusLock);
}
-(id)objectForKey:(id)key
{
return [spookyGlobalDictionary objectForKey:key];
}
-(void)setValue:(id)value forKey:(id)key
{
[spookyGlobalDictionary setValue:value forKey:key];
}
-(void)removeObjectForKey:(id)key
{
[spookyGlobalDictionary removeObjectForKey:key];
}
@end