forked from pro648/BasicDemos-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermanentThread.m
More file actions
161 lines (128 loc) · 4.89 KB
/
PermanentThread.m
File metadata and controls
161 lines (128 loc) · 4.89 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
156
157
158
159
160
161
//
// PermanentThread.m
// RunLoop
//
// Created by pro648 on 2020/1/11.
// Copyright © 2020 pro648. All rights reserved.
//
// 详细介绍:https://github.com/pro648/tips/wiki/RunLoop%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E8%BF%9B%E9%98%B6
#import "PermanentThread.h"
@interface CustomThread : NSThread
@end
@implementation CustomThread
- (void)dealloc {
NSLog(@"销毁自定义线程 %s", __PRETTY_FUNCTION__);
}
@end
@interface PermanentThread ()
@property (nonatomic, strong) CustomThread *innerThread;
@property (nonatomic, assign, getter=isStopped) BOOL stopped;
@end
@implementation PermanentThread
- (instancetype)init {
self = [super init];
if (self) {
__weak typeof(self) weakSelf = self;
self.innerThread = [[CustomThread alloc] initWithBlock:^{
// [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
//
// while (weakSelf && !weakSelf.isStopped) {
// [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
// };
// // 创建 observer
// CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
// switch (activity) {
// case kCFRunLoopEntry:
// NSLog(@"kCFRunLoopEntry -- %@", CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()));
// break;
// case kCFRunLoopBeforeTimers:
// NSLog(@"kCFRunLoopBeforeTimers");
// break;
//
// case kCFRunLoopBeforeSources:
// NSLog(@"kCFRunLoopBeforeSources");
// break;
// case kCFRunLoopBeforeWaiting:
// NSLog(@"kCFRunLoopBeforeWaiting");
// break;
// case kCFRunLoopAfterWaiting:
// NSLog(@"kCFRunLoopAfterWaiting");
// break;
// case kCFRunLoopExit:
// NSLog(@"kCFRunLoopExit");
// break;
// default:
// NSLog(@"default");
// break;
// }
// });
// // 将 observer 添加到当前线程的 run loop 的 common modes
// CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopCommonModes);
// // 释放 observer
// CFRelease(observer);
// C 语言版本
// 上下文
CFRunLoopSourceContext context = {0};
// 创建 source
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
// 添加 source 到 run loop
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
// 销毁 source
CFRelease(source);
// 启动
while (weakSelf && !weakSelf.stopped) {
//第三个参数 returnAfterSourceHandled 设置为 true,代表执行完source后就退出当前loop
// 设置为 false后,就不再需要 stopped 属性。
SInt32 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, true);
if (result == kCFRunLoopRunStopped) {
NSLog(@"run stopped");
} else if (result == kCFRunLoopRunFinished) {
weakSelf.stopped = YES;
NSLog(@"run finished");
} else if (result == kCFRunLoopRunTimedOut) {
NSLog(@"run timeout");
} else if (result == kCFRunLoopRunHandledSource) {
NSLog(@"run handle");
}
}
NSLog(@"--- end");
}];
}
return self;
}
- (void)executeTask:(PermanentThreadTask)task {
if (!task || !self.innerThread) {
return;
}
if (![self.innerThread isExecuting]) {
[self.innerThread start];
}
[self performSelector:@selector(_executeTask:) onThread:self.innerThread withObject:task waitUntilDone:NO];
}
//- (void)start {
// if (!self.innerThread) {
// return;
// }
//
// [self.innerThread start];
//}
- (void)stop {
if (!self.innerThread) {
return;
}
[self performSelector:@selector(_stop) onThread:self.innerThread withObject:NULL waitUntilDone:YES];
}
- (void)dealloc {
NSLog(@"线程 %s", __func__);
[self stop];
}
// MARK: - Private Methodes
- (void)_stop {
self.stopped = YES;
CFRunLoopStop(CFRunLoopGetCurrent());
self.innerThread = nil;
}
- (void)_executeTask:(PermanentThreadTask)task {
task();
}
@end