Skip to content

Commit 15106f1

Browse files
authored
Fix linux cfrunloop build (#1200)
* Revert "Remove duplicated runloop code (#1193)" This reverts commit ae0648d. * Re-remove duplicated runloop code
1 parent ae0648d commit 15106f1

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

Sources/Nimble/Utils/AsyncAwait.swift

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

3+
import CoreFoundation
34
import Dispatch
45
import Foundation
56

Sources/Nimble/Utils/AsyncTimerSequence.swift

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

3+
import CoreFoundation
34
import Dispatch
45
import Foundation
56

Sources/Nimble/Utils/PollAwait.swift

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

3+
import CoreFoundation
34
import Dispatch
45
import Foundation
56

@@ -195,24 +196,33 @@ internal class AwaitPromiseBuilder<T> {
195196
let timedOutSem = DispatchSemaphore(value: 0)
196197
let semTimedOutOrBlocked = DispatchSemaphore(value: 0)
197198
semTimedOutOrBlocked.signal()
198-
let runLoop = RunLoop.main
199-
runLoop.perform(inModes: [.default], block: {
199+
let runLoop = CFRunLoopGetMain()
200+
#if canImport(Darwin)
201+
let runLoopMode = CFRunLoopMode.defaultMode.rawValue
202+
#else
203+
let runLoopMode = kCFRunLoopDefaultMode
204+
#endif
205+
CFRunLoopPerformBlock(runLoop, runLoopMode) {
200206
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
201207
timedOutSem.signal()
202208
semTimedOutOrBlocked.signal()
203209
if self.promise.resolveResult(.timedOut) {
204-
RunLoop.main.stop()
210+
CFRunLoopStop(CFRunLoopGetMain())
205211
}
206212
}
207-
})
213+
}
208214
// potentially interrupt blocking code on run loop to let timeout code run
209-
runLoop.stop()
215+
CFRunLoopStop(runLoop)
210216
let now = DispatchTime.now() + forcefullyAbortTimeout.dispatchTimeInterval
211217
let didNotTimeOut = timedOutSem.wait(timeout: now) != .success
212218
let timeoutWasNotTriggered = semTimedOutOrBlocked.wait(timeout: .now()) == .success
213219
if didNotTimeOut && timeoutWasNotTriggered {
214220
if self.promise.resolveResult(.blockedRunLoop) {
215-
runLoop.stop()
221+
#if canImport(CoreFoundation)
222+
CFRunLoopStop(CFRunLoopGetMain())
223+
#else
224+
RunLoop.main._stop()
225+
#endif
216226
}
217227
}
218228
}
@@ -300,7 +310,11 @@ internal class Awaiter {
300310
if completionCount < 2 {
301311
func completeBlock() {
302312
if promise.resolveResult(.completed(result)) {
303-
RunLoop.main.stop()
313+
#if canImport(CoreFoundation)
314+
CFRunLoopStop(CFRunLoopGetMain())
315+
#else
316+
RunLoop.main._stop()
317+
#endif
304318
}
305319
}
306320

@@ -338,12 +352,20 @@ internal class Awaiter {
338352
do {
339353
if let result = try closure() {
340354
if promise.resolveResult(.completed(result)) {
341-
RunLoop.current.stop()
355+
#if canImport(CoreFoundation)
356+
CFRunLoopStop(CFRunLoopGetCurrent())
357+
#else
358+
RunLoop.current._stop()
359+
#endif
342360
}
343361
}
344362
} catch let error {
345363
if promise.resolveResult(.errorThrown(error)) {
346-
RunLoop.current.stop()
364+
#if canImport(CoreFoundation)
365+
CFRunLoopStop(CFRunLoopGetCurrent())
366+
#else
367+
RunLoop.current._stop()
368+
#endif
347369
}
348370
}
349371
}
@@ -377,23 +399,4 @@ internal func pollBlock(
377399
return result
378400
}
379401

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-
399402
#endif // #if !os(WASI)

Tests/NimbleTests/PollingTest+Require.swift

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

3+
import CoreFoundation
34
import Dispatch
45
import Foundation
56
import XCTest

Tests/NimbleTests/PollingTest.swift

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

3-
import Dispatch
4-
#if canImport(CoreFoundation)
53
import CoreFoundation
6-
#endif
4+
import Dispatch
75
import Foundation
86
import XCTest
97
import Nimble

0 commit comments

Comments
 (0)