Skip to content

Commit b856edf

Browse files
committed
Fixed runloop bug
1 parent 464b0e4 commit b856edf

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

ExecutionContext.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
65FB86CA1C78AC8B0005CD1B /* ImmediateExecutionContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 659E809B1C77457E00DE85B1 /* ImmediateExecutionContext.swift */; };
5353
65FB86CB1C78AC8B0005CD1B /* Result+Some.swift in Sources */ = {isa = PBXBuildFile; fileRef = 659E809D1C77457E00DE85B1 /* Result+Some.swift */; };
5454
65FB86CC1C78ACAE0005CD1B /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 659E80F01C78A70700DE85B1 /* Result.framework */; };
55+
9678EA2D1C7B22DC00E39B17 /* RunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9678EA2C1C7B22DC00E39B17 /* RunLoop.swift */; };
56+
9678EA2E1C7B22DC00E39B17 /* RunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9678EA2C1C7B22DC00E39B17 /* RunLoop.swift */; };
57+
9678EA2F1C7B22DC00E39B17 /* RunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9678EA2C1C7B22DC00E39B17 /* RunLoop.swift */; };
58+
9678EA301C7B22DC00E39B17 /* RunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9678EA2C1C7B22DC00E39B17 /* RunLoop.swift */; };
5559
/* End PBXBuildFile section */
5660

5761
/* Begin PBXContainerItemProxy section */
@@ -149,6 +153,7 @@
149153
65FB86981C78AA400005CD1B /* ExecutionContext.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ExecutionContext.framework; sourceTree = BUILT_PRODUCTS_DIR; };
150154
65FB86A11C78AA400005CD1B /* ExecutionContextTests-tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ExecutionContextTests-tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
151155
65FB86BE1C78AC260005CD1B /* ExecutionContext.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ExecutionContext.framework; sourceTree = BUILT_PRODUCTS_DIR; };
156+
9678EA2C1C7B22DC00E39B17 /* RunLoop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RunLoop.swift; sourceTree = "<group>"; };
152157
/* End PBXFileReference section */
153158

154159
/* Begin PBXFrameworksBuildPhase section */
@@ -242,6 +247,7 @@
242247
659E80981C77457E00DE85B1 /* DefaultExecutionContext.swift */,
243248
659E80991C77457E00DE85B1 /* DispatchExecutionContext.swift */,
244249
659E809C1C77457E00DE85B1 /* PThreadExecutionContext.swift */,
250+
9678EA2C1C7B22DC00E39B17 /* RunLoop.swift */,
245251
659E809B1C77457E00DE85B1 /* ImmediateExecutionContext.swift */,
246252
65E646DC1C79949C0036D028 /* CustomExecutionContext.swift */,
247253
659E809D1C77457E00DE85B1 /* Result+Some.swift */,
@@ -597,6 +603,7 @@
597603
buildActionMask = 2147483647;
598604
files = (
599605
659E809F1C77457E00DE85B1 /* DispatchExecutionContext.swift in Sources */,
606+
9678EA2D1C7B22DC00E39B17 /* RunLoop.swift in Sources */,
600607
659E80A01C77457E00DE85B1 /* ExecutionContext.swift in Sources */,
601608
65E646DD1C79949C0036D028 /* CustomExecutionContext.swift in Sources */,
602609
659E80A31C77457E00DE85B1 /* Result+Some.swift in Sources */,
@@ -620,6 +627,7 @@
620627
buildActionMask = 2147483647;
621628
files = (
622629
659E80D81C78A4AF00DE85B1 /* DefaultExecutionContext.swift in Sources */,
630+
9678EA2E1C7B22DC00E39B17 /* RunLoop.swift in Sources */,
623631
659E80DA1C78A4B500DE85B1 /* PThreadExecutionContext.swift in Sources */,
624632
65E646DE1C7994A00036D028 /* CustomExecutionContext.swift in Sources */,
625633
659E80DC1C78A4BC00DE85B1 /* Result+Some.swift in Sources */,
@@ -643,6 +651,7 @@
643651
buildActionMask = 2147483647;
644652
files = (
645653
65FB86B01C78AAE00005CD1B /* DefaultExecutionContext.swift in Sources */,
654+
9678EA2F1C7B22DC00E39B17 /* RunLoop.swift in Sources */,
646655
65FB86B21C78AAE50005CD1B /* PThreadExecutionContext.swift in Sources */,
647656
65E646DF1C7994A10036D028 /* CustomExecutionContext.swift in Sources */,
648657
65FB86B41C78AAE50005CD1B /* Result+Some.swift in Sources */,
@@ -666,6 +675,7 @@
666675
buildActionMask = 2147483647;
667676
files = (
668677
65FB86C71C78AC8B0005CD1B /* DefaultExecutionContext.swift in Sources */,
678+
9678EA301C7B22DC00E39B17 /* RunLoop.swift in Sources */,
669679
65FB86C91C78AC8B0005CD1B /* PThreadExecutionContext.swift in Sources */,
670680
65E646E01C7994A20036D028 /* CustomExecutionContext.swift in Sources */,
671681
65FB86CB1C78AC8B0005CD1B /* Result+Some.swift in Sources */,

ExecutionContext/RunLoop.swift

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,33 @@ import CoreFoundation
5252

5353
private protocol RunLoopCallback {
5454
var info : RunLoopCallbackInfo { get }
55-
var cfObject: AnyObject { get }
55+
var cfObject: AnyObject { mutating get }
5656
}
5757

5858
struct RunLoopSource : RunLoopCallback {
5959
private let info : RunLoopCallbackInfo
6060
private let priority : Int
61+
private var _source: CFRunLoopSource! = nil
6162

6263
private var cfObject : AnyObject {
63-
var context = CFRunLoopSourceContext(
64-
version: 0,
65-
info: UnsafeMutablePointer<Void>(Unmanaged.passUnretained(info).toOpaque()),
66-
retain: runLoopCallbackInfoRetain,
67-
release: runLoopCallbackInfoRelease,
68-
copyDescription: nil,
69-
equal: nil,
70-
hash: nil,
71-
schedule: nil,
72-
cancel: nil,
73-
perform: runLoopCallbackInfoRun
74-
)
75-
return CFRunLoopSourceCreate(nil, priority, &context)
64+
mutating get {
65+
if _source == nil {
66+
var context = CFRunLoopSourceContext(
67+
version: 0,
68+
info: UnsafeMutablePointer<Void>(Unmanaged.passUnretained(info).toOpaque()),
69+
retain: runLoopCallbackInfoRetain,
70+
release: runLoopCallbackInfoRelease,
71+
copyDescription: nil,
72+
equal: nil,
73+
hash: nil,
74+
schedule: nil,
75+
cancel: nil,
76+
perform: runLoopCallbackInfoRun
77+
)
78+
_source = CFRunLoopSourceCreate(nil, priority, &context)
79+
}
80+
return _source
81+
}
7682
}
7783

7884
init(_ task: SafeTask, priority: Int = 0) {
@@ -88,16 +94,22 @@ import CoreFoundation
8894
struct RunLoopDelay : RunLoopCallback {
8995
private let info : RunLoopCallbackInfo
9096
private let delay: Double
97+
private var _timer: CFRunLoopTimer! = nil
9198

9299
private var cfObject : AnyObject {
93-
var context = CFRunLoopTimerContext(
94-
version: 0,
95-
info: UnsafeMutablePointer<Void>(Unmanaged.passUnretained(info).toOpaque()),
96-
retain: runLoopCallbackInfoRetain,
97-
release: runLoopCallbackInfoRelease,
98-
copyDescription: nil
99-
)
100-
return CFRunLoopTimerCreate(nil, CFAbsoluteTimeGetCurrent()+delay, -1, 0, 0, timerRunCallback, &context)
100+
mutating get {
101+
if _timer == nil {
102+
var context = CFRunLoopTimerContext(
103+
version: 0,
104+
info: UnsafeMutablePointer<Void>(Unmanaged.passUnretained(info).toOpaque()),
105+
retain: runLoopCallbackInfoRetain,
106+
release: runLoopCallbackInfoRelease,
107+
copyDescription: nil
108+
)
109+
_timer = CFRunLoopTimerCreate(nil, CFAbsoluteTimeGetCurrent()+delay, -1, 0, 0, timerRunCallback, &context)
110+
}
111+
return _timer
112+
}
101113
}
102114

103115
init(_ task: SafeTask, delay: Double) {
@@ -155,14 +167,14 @@ import CoreFoundation
155167
while true { run() }
156168
}
157169

158-
func addSource(rls: RunLoopSource, mode: NSString) {
170+
func addSource(var rls: RunLoopSource, mode: NSString) {
159171
rls.info.runLoops.append(self)
160172
CFRunLoopAddSource(cfRunLoop, unsafeBitCast(rls.cfObject, CFRunLoopSource.self), mode.cfString)
161173
CFRunLoopSourceSignal(unsafeBitCast(rls.cfObject, CFRunLoopSource.self))
162174
CFRunLoopWakeUp(cfRunLoop)
163175
}
164176

165-
func addDelay(rld: RunLoopDelay, mode: NSString) {
177+
func addDelay(var rld: RunLoopDelay, mode: NSString) {
166178
rld.info.runLoops.append(self)
167179
CFRunLoopAddTimer(cfRunLoop, unsafeBitCast(rld.cfObject, CFRunLoopTimer.self), mode.cfString)
168180
CFRunLoopWakeUp(cfRunLoop)

Tests/ExecutionContext/ExecutionContextTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ extension ExecutionContextTests : XCTestCaseProvider {
159159
("testParallel", testParallel),
160160
("testGlobal", testGlobal),
161161
("testMain", testMain),
162-
("testMain", testCustomOnGlobal),
163-
("testMain", testCustomOnMain),
164-
("testMain", testCustomSimple)
162+
("testCustomOnGlobal", testCustomOnGlobal),
163+
("testCustomOnMain", testCustomOnMain),
164+
("testCustomSimple", testCustomSimple)
165165
]
166166
}
167167
}

0 commit comments

Comments
 (0)