-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDTOrderManager.m
76 lines (65 loc) · 1.74 KB
/
DTOrderManager.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#import "DTOrderManager.h"
#import <libkern/OSAtomic.h>
static DTOrderManager *dtOrderManagerSingleton = nil;
static int32_t dtOrderManagerInstances = 0;
static OSSpinLock dtOrderManagerInitLock = OS_SPINLOCK_INIT;
@implementation DTOrderManager
+ (DTOrderManager *)sharedManager
{
OSSpinLockLock(&dtOrderManagerInitLock);
{
if(!dtOrderManagerSingleton)
{
dtOrderManagerSingleton = [[DTOrderManager allocWithZone:NULL] init];
}
OSAtomicIncrement32(&dtOrderManagerInstances);
}
OSSpinLockUnlock(&dtOrderManagerInitLock);
return dtOrderManagerSingleton;
}
- (NSUInteger)orderForContext:(QCContext*)context channel:(NSString *)channel time:(double)t
{
NSMutableDictionary *c = [contexts objectForKey:[context description]];
if(!c)
{
c = [[NSMutableDictionary alloc] init];
[contexts setObject:c forKey:[context description]];
[c release];
}
NSUInteger o;
NSString *timeKey = [channel stringByAppendingString:@"_time"];
NSString *orderKey = [channel stringByAppendingString:@"_order"];
if( fabs([[c objectForKey:timeKey] doubleValue]-t) > 0.00001f )
{
[c setObject:[NSNumber numberWithDouble:t] forKey:timeKey];
o = 0;
}
else
o = [[c objectForKey:orderKey] unsignedIntegerValue];
[c setObject:[NSNumber numberWithUnsignedInteger:o+1] forKey:orderKey];
return o;
}
- (void)invalidateContext:(QCContext*)context
{
[contexts removeObjectForKey:[context description]];
}
- (void)unuse
{
OSSpinLockLock(&dtOrderManagerInitLock);
if( !OSAtomicDecrement32(&dtOrderManagerInstances) )
{
[contexts release];
[dtOrderManagerSingleton release];
dtOrderManagerSingleton = nil;
}
OSSpinLockUnlock(&dtOrderManagerInitLock);
}
- (id)init
{
if(self=[super init])
{
contexts = [[NSMutableDictionary alloc] init];
}
return self;
}
@end