forked from EFEducationFirstMobile/librabbitmq-objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMQPNetworkThread.m
200 lines (164 loc) · 5.06 KB
/
AMQPNetworkThread.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
//
// AMQPNetworkThread.m
// AMQPKit
//
// Created by Andrew Mackenzie-Ross on 23/02/2015.
// Copyright (c) 2015 librabbitmq. All rights reserved.
//
#import "AMQPNetworkThread.h"
@class AMQPBlock;
@interface AMQPNetworkThread ()
@property (nonatomic, readonly) NSTimer *distantFutureTimer;
@property (nonatomic, readwrite) AMQPBlock *head;
@property (nonatomic, readwrite) AMQPBlock *tail;
@property (nonatomic, readonly) NSRunLoop *runloop;
@property (nonatomic, readonly) dispatch_semaphore_t waitForRunloop;
@property (nonatomic, readonly) NSMutableArray *threadExitBlocks;
@end
@interface AMQPBlock : NSObject
- (instancetype)initWithBlock:(dispatch_block_t)block semaphore:(dispatch_semaphore_t)semaphore;
- (void)execute;
@property (nonatomic, readwrite) AMQPBlock *nextBlock;
@end
@implementation AMQPNetworkThread
- (instancetype)init
{
self = [super init];
if (self) {
_waitForRunloop = dispatch_semaphore_create(0);
_threadExitBlocks = [[NSMutableArray alloc] init];
}
return self;
}
- (void)start
{
[super start];
}
- (void)main
{
@autoreleasepool {
_runloop = [NSRunLoop currentRunLoop];
dispatch_semaphore_signal(self.waitForRunloop);
_waitForRunloop = nil;
// distantFutureTimer keeps this runloop from disappearing
_distantFutureTimer = [NSTimer timerWithTimeInterval:[[NSDate distantFuture] timeIntervalSinceNow] target:self selector:@selector(cancel) userInfo:nil repeats:NO];
[_runloop addTimer:_distantFutureTimer forMode:NSDefaultRunLoopMode];
while ([_runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
if (self.isCancelled) {
break;
}
}
[self runThreadExitBlocks];
[_distantFutureTimer invalidate];
_distantFutureTimer = nil;
_runloop = nil;
[NSThread exit];
}
}
- (void)runBlock:(dispatch_block_t)block
{
NSParameterAssert(block);
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[self scheduleBlock:block semaphore:sem];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}
- (void)scheduleBlock:(dispatch_block_t)block
{
[self scheduleBlock:block semaphore:NULL];
}
- (void)scheduleBlock:(dispatch_block_t)block semaphore:(dispatch_semaphore_t)semaphore
{
NSParameterAssert(block);
if (self.waitForRunloop && dispatch_semaphore_wait(self.waitForRunloop, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC))) != 0) {
[NSException raise:NSInternalInconsistencyException format:@"Cannot schedule a block until thread is executing and runloop exists."];
}
AMQPBlock *blockCommand = [[AMQPBlock alloc] initWithBlock:block semaphore:semaphore];
BOOL runHead = NO;
@synchronized(self) {
self.tail.nextBlock = blockCommand;
self.tail = blockCommand;
if (!self.head) {
self.head = blockCommand;
runHead = YES;
}
}
if (runHead) {
[self performSelector:@selector(runHead) onThread:self withObject:nil waitUntilDone:NO];
}
}
- (void)runHead
{
[self.head execute];
BOOL runHeadAgain = NO;
@synchronized(self) {
self.head = self.head.nextBlock;
runHeadAgain = self.head != nil;
}
if (runHeadAgain) {
[self performSelector:@selector(runHead) onThread:self withObject:nil waitUntilDone:NO];
}
}
- (void)scheduleThreadExitBlock:(dispatch_block_t)block
{
AMQPBlock *blockCommand = [[AMQPBlock alloc] initWithBlock:block semaphore:NULL];
@synchronized(self) {
[self.threadExitBlocks addObject:blockCommand];
}
}
- (void)runThreadExitBlocks
{
@synchronized(self) {
for (AMQPBlock *block in [self.threadExitBlocks reverseObjectEnumerator]) {
[block execute];
}
[self.threadExitBlocks removeAllObjects];
}
}
- (void)cancel
{
@synchronized(self) {
if (self.isCancelled || self.isFinished) {
return;
}
[super cancel];
}
// Running a timer will cause runMode:beforeDate to return and because the
// thread is now cancelled the while loop will exit and the clean up will
// occur.
//
// Order 0 is used so that this message will be processed before scheduled
// blocks.
[[NSRunLoop currentRunLoop] performSelector:@selector(description) target:self argument:nil order:0 modes:@[NSDefaultRunLoopMode]];
}
@end
@implementation AMQPBlock
{
dispatch_block_t _block;
dispatch_semaphore_t _semaphore;
}
- (instancetype)initWithBlock:(dispatch_block_t)block semaphore:(dispatch_semaphore_t)semaphore
{
self = [super init];
if (self) {
_block = [block copy];
_semaphore = semaphore;
}
return self;
}
- (void)execute
{
_block();
_block = nil;
if (_semaphore) {
dispatch_semaphore_signal(_semaphore);
_semaphore = NULL;
}
}
- (void)dealloc
{
if (_semaphore) {
dispatch_semaphore_signal(_semaphore);
_semaphore = NULL;
}
}
@end