Oolite
Loading...
Searching...
No Matches
OOScriptTimer.m
Go to the documentation of this file.
1/*
2
3OOScriptTimer.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOScriptTimer.h"
27#import "OOLogging.h"
28#import "OOPriorityQueue.h"
29#import "Universe.h"
30
32
33// During an update, new timers must be deferred to avoid an infinite loop.
34static BOOL sUpdating;
35static NSMutableArray* sDeferredTimers;
36
37@implementation OOScriptTimer
38
39- (id)initWithNextTime:(OOTimeAbsolute)nextTime
40 interval:(OOTimeDelta)interval
41{
43
44 if ((self = [super init])) {
45 if (interval <= 0.0)
46 interval = -1.0;
47
48 now = [UNIVERSE getTime];
49 if (nextTime < 0.0)
50 nextTime = now + interval;
51 if (nextTime < now && interval < 0) {
52 // Negative or old nextTime and negative interval = meaningless.
53 [self release];
54 self = nil;
55 } else {
58 _hasBeenRun = NO;
59 }
60 }
61
62 return self;
63}
64
65// Sets nextTime to current time + delay.
66- (id)initOneShotTimerWithDelay:(OOTimeDelta)delay
67{
68 return [self initWithNextTime:[UNIVERSE getTime] + delay interval:-1.0];
69}
70
71- (void)dealloc
72{
73 if (_isScheduled)
74 [self unscheduleTimer];
75
76 [super dealloc];
77}
78
80{
81 NSString* intervalDesc = nil;
82
83 if (_interval <= 0.0)
84 intervalDesc = @"one-shot";
85 else
86 intervalDesc = [NSString stringWithFormat:@"interval: %g", _interval];
87
88 return [NSString stringWithFormat:@"nextTime: %g, %@, %srunning", _nextTime, intervalDesc, _isScheduled ? "" : "not "];
89}
90
92{
93 return _nextTime;
94}
95
96- (BOOL)setNextTime:(OOTimeAbsolute)nextTime
97{
98 if (_isScheduled)
99 return NO;
100
102 return YES;
103}
104
106{
107 return _interval;
108}
109
110- (void)setInterval:(OOTimeDelta)interval
111{
112 if (interval <= 0.0)
113 interval = -1.0;
115}
116
121
123{
124 if (_isScheduled)
125 return YES;
126 if (![self isValidForScheduling])
127 return NO;
128
129 if (EXPECT(!sUpdating)) {
130 if (EXPECT_NOT(sTimers == nil))
131 sTimers = [[OOPriorityQueue alloc] initWithComparator:@selector(compareByNextFireTime:)];
132 [sTimers addObject:self];
133 } else {
134 if (sDeferredTimers == nil)
135 sDeferredTimers = [[NSMutableArray alloc] init];
136 [sDeferredTimers addObject:self];
137 }
138
139 _isScheduled = YES;
140 return YES;
141}
142
144{
146 _isScheduled = NO;
147 _hasBeenRun = NO;
148}
149
151{
152 return _isScheduled;
153}
154
156{
157 OOScriptTimer* timer = nil;
158 OOTimeAbsolute now;
159
160 sUpdating = YES;
161
162 now = [UNIVERSE getTime];
163 for (;;) {
164 timer = [sTimers peekAtNextObject];
165 if (timer == nil || now < [timer nextTime])
166 break;
167
169
170 // Must fire before rescheduling so that the timer callback can stop itself. -- Ahruman 2011-01-01
172
173 timer->_hasBeenRun = YES;
174
175 if (timer->_isScheduled) {
176 timer->_isScheduled = NO;
178 }
179 }
180
181 if (sDeferredTimers != nil) {
182 [sTimers addObjects:sDeferredTimers];
184 }
185
186 sUpdating = NO;
187}
188
190{
191 NSArray* timers = nil;
192 OOScriptTimer* timer = nil;
193
194 // Intermediate array is required so we don't get stuck in an endless loop over reinserted timers. Note that -sortedObjects also clears the queue!
195 timers = [sTimers sortedObjects];
196 foreach (timer, timers) {
197 timer->_isScheduled = NO;
198 }
199}
200
202{
203 OOTimeAbsolute now;
204 double scaled;
205
206 now = [UNIVERSE getTime];
207 if (_nextTime <= now) {
208 if (_interval <= 0.0 && _hasBeenRun)
209 return NO; // One-shot timer which has expired
210
211 // Move _nextTime to the closest future time that's a multiple of _interval
212 scaled = (now - _nextTime) / _interval;
213 scaled = ceil(scaled);
214 _nextTime += scaled * _interval;
215 if (_nextTime <= now && _hasBeenRun) {
216 // Should only happen if _nextTime is exactly equal to now after previous stuff
218 }
219 }
220
221 return YES;
222}
223
224- (NSComparisonResult)compareByNextFireTime:(OOScriptTimer*)other
225{
226 OOTimeAbsolute otherTime = -INFINITY;
227
228 @try {
229 if (other != nil)
230 otherTime = [other nextTime];
231 }
232 @catch (NSException* exception) {
233 OOLog(kOOLogException, @"\n\n***** Ignoring Timer Exception: %@ : %@ *****\n\n", [exception name], [exception reason]);
234 }
235
236 if (_nextTime < otherTime)
237 return NSOrderedAscending;
238 else if (_nextTime > otherTime)
239 return NSOrderedDescending;
240 else
241 return NSOrderedSame;
242}
243
244@end
#define DESTROY(x)
#define EXPECT_NOT(x)
#define EXPECT(x)
NSString *const kOOLogException
Definition OOLogging.m:600
#define OOLogGenericSubclassResponsibility()
Definition OOLogging.h:141
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
static NSMutableArray * sDeferredTimers
static BOOL sUpdating
static OOPriorityQueue * sTimers
double OOTimeDelta
Definition OOTypes.h:193
double OOTimeAbsolute
Definition OOTypes.h:192
void removeExactObject:(id object)
void addObject:(id object)
void addObjects:(id collection)
NSArray * sortedObjects()
OOTimeAbsolute _nextTime
BOOL isValidForScheduling()
OOTimeDelta interval()
OOTimeDelta _interval
NSString * descriptionComponents()
OOTimeAbsolute nextTime()
id initWithNextTime:interval:(OOTimeAbsolute nextTime,[interval] OOTimeDelta interval)