-
Notifications
You must be signed in to change notification settings - Fork 4
/
GSTicker.m
360 lines (306 loc) · 7.32 KB
/
GSTicker.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* -*-objc-*- */
/** Implementation of GSTicker for GNUStep
Copyright (C) 2005 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: November 2005
This file is part of the Performance Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date$ $Revision$
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSString.h>
#import <Foundation/NSTimer.h>
#import "GSTicker.h"
static Class NSDateClass = 0;
static NSDate *startDate = nil;
static SEL tiSel = 0;
static NSTimeInterval (*tiImp)(Class,SEL) = 0;
static volatile NSTimeInterval baseTime = 0;
static volatile NSTimeInterval lastTime = 0;
@interface GSTickerObservation : NSObject
{
@public
id<GSTicker> observer;
id userInfo;
}
- (void) fire: (NSArray*)live;
@end
@implementation GSTickerObservation
- (void) fire: (NSArray*)live
{
if ([live indexOfObjectIdenticalTo: self] != NSNotFound)
{
[observer newSecond: userInfo];
}
}
@end
@interface GSTickerThread : NSObject
{
@public
/* NB. We retain theTimer rather than depending on the run loop to do it.
* This is because tis object is typically deallocated on thread exist,
* so the thread's run loop may be deallocated before this object is
* deallocated, and we want to be sure the timer is not already deallocated
* when we invalidate it.
*/
NSTimer *theTimer;
NSMutableArray *observers;
unsigned last;
}
@end
@implementation GSTickerThread
- (void) dealloc
{
[theTimer invalidate];
[theTimer release];
theTimer = nil;
[observers release];
observers = nil;
[super dealloc];
}
- (id) init
{
if (nil != (self = [super init]))
{
NSTimeInterval ti = GSTickerTimeNow();
observers = [NSMutableArray new];
theTimer = [[NSTimer scheduledTimerWithTimeInterval: ti - (int)ti
target: [GSTicker class]
selector: @selector(_tick:)
userInfo: self
repeats: NO] retain];
}
return self;
}
@end
@interface GSTicker (Private)
+ (void) _tick: (NSTimer*)t;
@end
inline NSTimeInterval GSTickerTimeLast()
{
return lastTime;
}
inline NSTimeInterval GSTickerTimeStart()
{
if (baseTime == 0)
{
[GSTicker class];
}
return baseTime;
}
inline unsigned GSTickerTimeTick()
{
NSTimeInterval start = GSTickerTimeStart();
return (GSTickerTimeLast() - start) + 1;
}
NSTimeInterval GSTickerTimeNow()
{
if (baseTime == 0)
{
[GSTicker class];
}
else
{
NSTimeInterval now;
/*
* If the clock has been reset so that time has gone backwards,
* we adjust the baseTime so that lastTime >= baseTime is true.
*/
now = (*tiImp)(NSDateClass, tiSel);
if (now < lastTime)
{
baseTime -= (lastTime - now);
}
lastTime = now;
}
return lastTime;
}
@implementation GSTicker
+ (void) initialize
{
if (0 == baseTime)
{
NSDateClass = [NSDate class];
tiSel = @selector(timeIntervalSinceReferenceDate);
tiImp
= (NSTimeInterval (*)(Class,SEL))[NSDateClass methodForSelector: tiSel];
baseTime = lastTime = (*tiImp)(NSDateClass, tiSel);
startDate = [[NSDateClass alloc]
initWithTimeIntervalSinceReferenceDate: baseTime];
}
}
+ (NSDate*) last
{
return [NSDateClass dateWithTimeIntervalSinceReferenceDate:
GSTickerTimeLast()];
}
+ (void) newSecond: (id)userInfo
{
return;
}
+ (NSDate*) now
{
return [NSDateClass dateWithTimeIntervalSinceReferenceDate:
GSTickerTimeNow()];
}
+ (void) registerObserver: (id<GSTicker>)anObject
userInfo: (id)userInfo
{
GSTickerThread *tt;
GSTickerObservation *to;
unsigned count;
tt = [[[NSThread currentThread] threadDictionary]
objectForKey: @"GSTickerThread"];
if (tt == nil)
{
tt = [GSTickerThread new];
[[[NSThread currentThread] threadDictionary]
setObject: tt forKey: @"GSTickerThread"];
[tt release];
}
count = [tt->observers count];
while (count-- > 0)
{
to = [tt->observers objectAtIndex: count];
if (to->observer == anObject)
{
return; // Already registered.
}
}
to = [GSTickerObservation new];
to->observer = anObject;
to->userInfo = userInfo;
[tt->observers addObject: to];
[to release];
}
+ (NSDate*) start
{
if (startDate == nil)
{
startDate = [NSDateClass alloc];
startDate = [startDate initWithTimeIntervalSinceReferenceDate:
GSTickerTimeStart()];
}
return startDate;
}
+ (unsigned) tick
{
return GSTickerTimeTick();
}
+ (void) unregisterObserver: (id<GSTicker>)anObject
{
GSTickerThread *tt;
tt = [[[NSThread currentThread] threadDictionary]
objectForKey: @"GSTickerThread"];
if (tt != nil)
{
GSTickerObservation *to;
unsigned count = [tt->observers count];
while (count-- > 0)
{
to = [tt->observers objectAtIndex: count];
if (to->observer == anObject)
{
[tt->observers removeObjectAtIndex: count];
break;
}
}
}
}
+ (void) update
{
GSTickerTimeNow();
}
- (NSDate*) last
{
return [NSDateClass dateWithTimeIntervalSinceReferenceDate:
GSTickerTimeLast()];
}
- (void) newSecond: (id)userInfo
{
return;
}
- (NSDate*) now
{
return [NSDateClass dateWithTimeIntervalSinceReferenceDate:
GSTickerTimeNow()];
}
- (NSDate*) start
{
return startDate;
}
- (unsigned) tick
{
return GSTickerTimeTick();
}
- (void) update
{
GSTickerTimeNow();
}
@end
@implementation GSTicker (Private)
+ (void) _tick: (NSTimer*)t
{
GSTickerThread *tt = [t userInfo];
if (tt == nil)
{
tt = [[[NSThread currentThread] threadDictionary]
objectForKey: @"GSTickerThread"];
}
if (tt != nil && [tt->observers count] > 0)
{
NSTimeInterval ti;
[tt->theTimer invalidate];
[tt->theTimer release];
tt->theTimer = nil;
if ([tt->observers count] > 0)
{
NSArray *a;
unsigned tick;
GSTickerTimeNow();
tick = GSTickerTimeTick();
if (tick != tt->last)
{
tt->last = tick;
a = [tt->observers copy];
NS_DURING
{
[a makeObjectsPerformSelector: @selector(fire:)
withObject: tt->observers];
}
NS_HANDLER
{
NSLog(@"Problem firing ticker observers: %@", localException);
}
NS_ENDHANDLER
[a release];
}
}
ti = GSTickerTimeNow();
tt->theTimer = [[NSTimer scheduledTimerWithTimeInterval: ti - (int)ti
target: self
selector: @selector(_tick:)
userInfo: tt
repeats: NO] retain];
}
else
{
[[[NSThread currentThread] threadDictionary]
removeObjectForKey: @"GSTickerThread"];
}
}
@end