-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWHCTimer.m
More file actions
155 lines (125 loc) · 3.54 KB
/
WHCTimer.m
File metadata and controls
155 lines (125 loc) · 3.54 KB
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
//
// WHCTimer.m
// WHCAPP
//
// Created by Haochen Wang on 11/29/16.
// Copyright © 2016 WHC. All rights reserved.
//
#import "WHCTimer.h"
#import <libkern/OSAtomic.h>
@interface WHCTimer ()
{
uint32_t _timerIsInvalidated;
}
@property (nonatomic, assign) NSTimeInterval timeInterval;
@property (nonatomic, weak) id target;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, strong) dispatch_queue_t serialQueue;
@property (nonatomic, strong) dispatch_source_t timer;
@property (atomic, assign) NSTimeInterval tolerance;
@property (nonatomic, assign) BOOL repeats;
@end
@implementation WHCTimer
@synthesize tolerance = _tolerance;
-(id)initWithTimeInterval:(NSTimeInterval)timeInterval
target:(id)target
selector:(SEL)selector
repeats:(BOOL)repeats
{
self = [super init];
if (!self)
{
return nil;
}
self.timeInterval = timeInterval;
self.target = target;
self.selector = selector;
self.repeats = repeats;
NSString *queueName = [NSString stringWithFormat:@"WHCTimer.%p", self];
self.serialQueue = dispatch_queue_create([queueName cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(self.serialQueue, dispatch_get_main_queue());
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0,
0,
self.serialQueue);
[self start];
return self;
}
+(instancetype)timerCreateWithTimeInterval:(NSTimeInterval)timeInterval
target:(id)target
selector:(SEL)selector
repeats:(BOOL)repeats
{
return [[self alloc] initWithTimeInterval:timeInterval target:target selector:selector repeats:repeats];
}
- (void)setTolerance:(NSTimeInterval)tolerance
{
@synchronized(self)
{
if (tolerance != _tolerance)
{
_tolerance = tolerance;
[self resetTimerProperties];
}
}
}
- (NSTimeInterval)tolerance
{
@synchronized(self)
{
return _tolerance;
}
}
- (void)resetTimerProperties
{
int64_t intervalInNanoseconds = (int64_t)(self.timeInterval * NSEC_PER_SEC);
int64_t toleranceInNanoseconds = (int64_t)(self.tolerance * NSEC_PER_SEC);
dispatch_source_set_timer(self.timer,
dispatch_time(DISPATCH_TIME_NOW, intervalInNanoseconds),
(uint64_t)intervalInNanoseconds,
toleranceInNanoseconds
);
}
- (void)start
{
[self resetTimerProperties];
__weak WHCTimer *weakSelf = self;
dispatch_source_set_event_handler(self.timer, ^{
[weakSelf timerFired];
});
dispatch_resume(self.timer);
}
- (void)fire
{
[self timerFired];
}
- (void)cancel
{
if (!OSAtomicTestAndSetBarrier(7, &_timerIsInvalidated))
{
dispatch_source_t timer = self.timer;
dispatch_async(self.serialQueue, ^{
dispatch_source_cancel(timer);
});
}
}
- (void)timerFired
{
if (OSAtomicAnd32OrigBarrier(1, &_timerIsInvalidated))
{
return;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.target performSelector:self.selector withObject:self];
#pragma clang diagnostic pop
if (!self.repeats)
{
[self cancel];
}
}
- (void)dealloc
{
[self cancel];
}
@end