Skip to content

Commit 4701651

Browse files
committed
Revert "Remove duplicated runloop code (#1193)"
This reverts commit ae0648d.
1 parent ae0648d commit 4701651

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

Sources/Nimble/Utils/AsyncAwait.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#if !os(WASI)
22

3+
#if canImport(CoreFoundation)
4+
import CoreFoundation
5+
#endif
6+
37
import Dispatch
48
import Foundation
59

Sources/Nimble/Utils/AsyncTimerSequence.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#if !os(WASI)
22

3+
#if canImport(CoreFoundation)
4+
import CoreFoundation
5+
#endif
36
import Dispatch
47
import Foundation
58

Sources/Nimble/Utils/PollAwait.swift

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#if !os(WASI)
22

3+
#if canImport(CoreFoundation)
4+
import CoreFoundation
5+
#endif
36
import Dispatch
47
import Foundation
58

@@ -195,24 +198,48 @@ internal class AwaitPromiseBuilder<T> {
195198
let timedOutSem = DispatchSemaphore(value: 0)
196199
let semTimedOutOrBlocked = DispatchSemaphore(value: 0)
197200
semTimedOutOrBlocked.signal()
201+
#if canImport(CoreFoundation)
202+
let runLoop = CFRunLoopGetMain()
203+
#if canImport(Darwin)
204+
let runLoopMode = CFRunLoopMode.defaultMode.rawValue
205+
#else
206+
let runLoopMode = kCFRunLoopDefaultMode
207+
#endif
208+
CFRunLoopPerformBlock(runLoop, runLoopMode) {
209+
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
210+
timedOutSem.signal()
211+
semTimedOutOrBlocked.signal()
212+
if self.promise.resolveResult(.timedOut) {
213+
CFRunLoopStop(CFRunLoopGetMain())
214+
}
215+
}
216+
}
217+
// potentially interrupt blocking code on run loop to let timeout code run
218+
CFRunLoopStop(runLoop)
219+
#else
198220
let runLoop = RunLoop.main
199221
runLoop.perform(inModes: [.default], block: {
200222
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
201223
timedOutSem.signal()
202224
semTimedOutOrBlocked.signal()
203225
if self.promise.resolveResult(.timedOut) {
204-
RunLoop.main.stop()
226+
RunLoop.main._stop()
205227
}
206228
}
207229
})
208230
// potentially interrupt blocking code on run loop to let timeout code run
209-
runLoop.stop()
231+
runLoop._stop()
232+
#endif
210233
let now = DispatchTime.now() + forcefullyAbortTimeout.dispatchTimeInterval
211234
let didNotTimeOut = timedOutSem.wait(timeout: now) != .success
212235
let timeoutWasNotTriggered = semTimedOutOrBlocked.wait(timeout: .now()) == .success
213236
if didNotTimeOut && timeoutWasNotTriggered {
214237
if self.promise.resolveResult(.blockedRunLoop) {
215-
runLoop.stop()
238+
#if canImport(CoreFoundation)
239+
CFRunLoopStop(CFRunLoopGetMain())
240+
#else
241+
RunLoop.main._stop()
242+
#endif
216243
}
217244
}
218245
}
@@ -300,7 +327,11 @@ internal class Awaiter {
300327
if completionCount < 2 {
301328
func completeBlock() {
302329
if promise.resolveResult(.completed(result)) {
303-
RunLoop.main.stop()
330+
#if canImport(CoreFoundation)
331+
CFRunLoopStop(CFRunLoopGetMain())
332+
#else
333+
RunLoop.main._stop()
334+
#endif
304335
}
305336
}
306337

@@ -338,12 +369,20 @@ internal class Awaiter {
338369
do {
339370
if let result = try closure() {
340371
if promise.resolveResult(.completed(result)) {
341-
RunLoop.current.stop()
372+
#if canImport(CoreFoundation)
373+
CFRunLoopStop(CFRunLoopGetCurrent())
374+
#else
375+
RunLoop.current._stop()
376+
#endif
342377
}
343378
}
344379
} catch let error {
345380
if promise.resolveResult(.errorThrown(error)) {
346-
RunLoop.current.stop()
381+
#if canImport(CoreFoundation)
382+
CFRunLoopStop(CFRunLoopGetCurrent())
383+
#else
384+
RunLoop.current._stop()
385+
#endif
347386
}
348387
}
349388
}
@@ -377,23 +416,4 @@ internal func pollBlock(
377416
return result
378417
}
379418

380-
#if canImport(CoreFoundation)
381-
import CoreFoundation
382-
383-
extension RunLoop {
384-
func stop() {
385-
CFRunLoopStop(getCFRunLoop())
386-
}
387-
}
388-
389-
#else
390-
391-
extension RunLoop {
392-
func stop() {
393-
_stop()
394-
}
395-
}
396-
397-
#endif
398-
399419
#endif // #if !os(WASI)

Tests/NimbleTests/PollingTest+Require.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#if !os(WASI)
22

33
import Dispatch
4+
#if canImport(CoreFoundation)
5+
import CoreFoundation
6+
#endif
47
import Foundation
58
import XCTest
69
import Nimble

0 commit comments

Comments
 (0)