|
1 | 1 | #if !os(WASI) |
2 | 2 |
|
| 3 | +import CoreFoundation |
3 | 4 | import Dispatch |
4 | 5 | import Foundation |
5 | 6 |
|
@@ -195,24 +196,33 @@ internal class AwaitPromiseBuilder<T> { |
195 | 196 | let timedOutSem = DispatchSemaphore(value: 0) |
196 | 197 | let semTimedOutOrBlocked = DispatchSemaphore(value: 0) |
197 | 198 | 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) { |
200 | 206 | if semTimedOutOrBlocked.wait(timeout: .now()) == .success { |
201 | 207 | timedOutSem.signal() |
202 | 208 | semTimedOutOrBlocked.signal() |
203 | 209 | if self.promise.resolveResult(.timedOut) { |
204 | | - RunLoop.main.stop() |
| 210 | + CFRunLoopStop(CFRunLoopGetMain()) |
205 | 211 | } |
206 | 212 | } |
207 | | - }) |
| 213 | + } |
208 | 214 | // potentially interrupt blocking code on run loop to let timeout code run |
209 | | - runLoop.stop() |
| 215 | + CFRunLoopStop(runLoop) |
210 | 216 | let now = DispatchTime.now() + forcefullyAbortTimeout.dispatchTimeInterval |
211 | 217 | let didNotTimeOut = timedOutSem.wait(timeout: now) != .success |
212 | 218 | let timeoutWasNotTriggered = semTimedOutOrBlocked.wait(timeout: .now()) == .success |
213 | 219 | if didNotTimeOut && timeoutWasNotTriggered { |
214 | 220 | if self.promise.resolveResult(.blockedRunLoop) { |
215 | | - runLoop.stop() |
| 221 | + #if canImport(CoreFoundation) |
| 222 | + CFRunLoopStop(CFRunLoopGetMain()) |
| 223 | + #else |
| 224 | + RunLoop.main._stop() |
| 225 | + #endif |
216 | 226 | } |
217 | 227 | } |
218 | 228 | } |
@@ -300,7 +310,11 @@ internal class Awaiter { |
300 | 310 | if completionCount < 2 { |
301 | 311 | func completeBlock() { |
302 | 312 | if promise.resolveResult(.completed(result)) { |
303 | | - RunLoop.main.stop() |
| 313 | + #if canImport(CoreFoundation) |
| 314 | + CFRunLoopStop(CFRunLoopGetMain()) |
| 315 | + #else |
| 316 | + RunLoop.main._stop() |
| 317 | + #endif |
304 | 318 | } |
305 | 319 | } |
306 | 320 |
|
@@ -338,12 +352,20 @@ internal class Awaiter { |
338 | 352 | do { |
339 | 353 | if let result = try closure() { |
340 | 354 | if promise.resolveResult(.completed(result)) { |
341 | | - RunLoop.current.stop() |
| 355 | + #if canImport(CoreFoundation) |
| 356 | + CFRunLoopStop(CFRunLoopGetCurrent()) |
| 357 | + #else |
| 358 | + RunLoop.current._stop() |
| 359 | + #endif |
342 | 360 | } |
343 | 361 | } |
344 | 362 | } catch let error { |
345 | 363 | if promise.resolveResult(.errorThrown(error)) { |
346 | | - RunLoop.current.stop() |
| 364 | + #if canImport(CoreFoundation) |
| 365 | + CFRunLoopStop(CFRunLoopGetCurrent()) |
| 366 | + #else |
| 367 | + RunLoop.current._stop() |
| 368 | + #endif |
347 | 369 | } |
348 | 370 | } |
349 | 371 | } |
@@ -377,23 +399,4 @@ internal func pollBlock( |
377 | 399 | return result |
378 | 400 | } |
379 | 401 |
|
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 | | - |
399 | 402 | #endif // #if !os(WASI) |
0 commit comments