Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 77 lines (65 sloc) 1.784 kB
ba4545f @jlstrecker Initial commit
jlstrecker authored
1 #import "DTOrderManager.h"
2 #import <libkern/OSAtomic.h>
3
4 static DTOrderManager *dtOrderManagerSingleton = nil;
5 static int32_t dtOrderManagerInstances = 0;
6 static OSSpinLock dtOrderManagerInitLock = OS_SPINLOCK_INIT;
7
8 @implementation DTOrderManager
9
10 + (DTOrderManager *)sharedManager
11 {
12 OSSpinLockLock(&dtOrderManagerInitLock);
13 {
14 if(!dtOrderManagerSingleton)
15 {
16 dtOrderManagerSingleton = [[DTOrderManager allocWithZone:NULL] init];
17 }
18 OSAtomicIncrement32(&dtOrderManagerInstances);
19 }
20 OSSpinLockUnlock(&dtOrderManagerInitLock);
21
22 return dtOrderManagerSingleton;
23 }
24
25 - (NSUInteger)orderForContext:(QCContext*)context channel:(NSString *)channel time:(double)t
26 {
27 NSMutableDictionary *c = [contexts objectForKey:[context description]];
28 if(!c)
29 {
30 c = [[NSMutableDictionary alloc] init];
31 [contexts setObject:c forKey:[context description]];
32 [c release];
33 }
34
35 NSUInteger o;
36 NSString *timeKey = [channel stringByAppendingString:@"_time"];
37 NSString *orderKey = [channel stringByAppendingString:@"_order"];
38 if( fabs([[c objectForKey:timeKey] doubleValue]-t) > 0.00001f )
39 {
40 [c setObject:[NSNumber numberWithDouble:t] forKey:timeKey];
41 o = 0;
42 }
43 else
44 o = [[c objectForKey:orderKey] unsignedIntegerValue];
45
46 [c setObject:[NSNumber numberWithUnsignedInteger:o+1] forKey:orderKey];
47 return o;
48 }
49
50 - (void)invalidateContext:(QCContext*)context
51 {
52 [contexts removeObjectForKey:[context description]];
53 }
54
55 - (void)unuse
56 {
57 OSSpinLockLock(&dtOrderManagerInitLock);
58 if( !OSAtomicDecrement32(&dtOrderManagerInstances) )
59 {
60 [contexts release];
61 [dtOrderManagerSingleton release];
62 dtOrderManagerSingleton = nil;
63 }
64 OSSpinLockUnlock(&dtOrderManagerInitLock);
65 }
66
67 - (id)init
68 {
69 if(self=[super init])
70 {
71 contexts = [[NSMutableDictionary alloc] init];
72 }
73 return self;
74 }
75
76 @end
Something went wrong with that request. Please try again.