|
1 | 1 | #if !os(WASI) |
2 | 2 |
|
| 3 | +#if canImport(CoreFoundation) |
| 4 | +import CoreFoundation |
| 5 | +#endif |
3 | 6 | import Dispatch |
4 | 7 | import Foundation |
5 | 8 |
|
@@ -195,24 +198,48 @@ internal class AwaitPromiseBuilder<T> { |
195 | 198 | let timedOutSem = DispatchSemaphore(value: 0) |
196 | 199 | let semTimedOutOrBlocked = DispatchSemaphore(value: 0) |
197 | 200 | 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 |
198 | 220 | let runLoop = RunLoop.main |
199 | 221 | runLoop.perform(inModes: [.default], block: { |
200 | 222 | if semTimedOutOrBlocked.wait(timeout: .now()) == .success { |
201 | 223 | timedOutSem.signal() |
202 | 224 | semTimedOutOrBlocked.signal() |
203 | 225 | if self.promise.resolveResult(.timedOut) { |
204 | | - RunLoop.main.stop() |
| 226 | + RunLoop.main._stop() |
205 | 227 | } |
206 | 228 | } |
207 | 229 | }) |
208 | 230 | // potentially interrupt blocking code on run loop to let timeout code run |
209 | | - runLoop.stop() |
| 231 | + runLoop._stop() |
| 232 | + #endif |
210 | 233 | let now = DispatchTime.now() + forcefullyAbortTimeout.dispatchTimeInterval |
211 | 234 | let didNotTimeOut = timedOutSem.wait(timeout: now) != .success |
212 | 235 | let timeoutWasNotTriggered = semTimedOutOrBlocked.wait(timeout: .now()) == .success |
213 | 236 | if didNotTimeOut && timeoutWasNotTriggered { |
214 | 237 | if self.promise.resolveResult(.blockedRunLoop) { |
215 | | - runLoop.stop() |
| 238 | + #if canImport(CoreFoundation) |
| 239 | + CFRunLoopStop(CFRunLoopGetMain()) |
| 240 | + #else |
| 241 | + RunLoop.main._stop() |
| 242 | + #endif |
216 | 243 | } |
217 | 244 | } |
218 | 245 | } |
@@ -300,7 +327,11 @@ internal class Awaiter { |
300 | 327 | if completionCount < 2 { |
301 | 328 | func completeBlock() { |
302 | 329 | if promise.resolveResult(.completed(result)) { |
303 | | - RunLoop.main.stop() |
| 330 | + #if canImport(CoreFoundation) |
| 331 | + CFRunLoopStop(CFRunLoopGetMain()) |
| 332 | + #else |
| 333 | + RunLoop.main._stop() |
| 334 | + #endif |
304 | 335 | } |
305 | 336 | } |
306 | 337 |
|
@@ -338,12 +369,20 @@ internal class Awaiter { |
338 | 369 | do { |
339 | 370 | if let result = try closure() { |
340 | 371 | if promise.resolveResult(.completed(result)) { |
341 | | - RunLoop.current.stop() |
| 372 | + #if canImport(CoreFoundation) |
| 373 | + CFRunLoopStop(CFRunLoopGetCurrent()) |
| 374 | + #else |
| 375 | + RunLoop.current._stop() |
| 376 | + #endif |
342 | 377 | } |
343 | 378 | } |
344 | 379 | } catch let error { |
345 | 380 | if promise.resolveResult(.errorThrown(error)) { |
346 | | - RunLoop.current.stop() |
| 381 | + #if canImport(CoreFoundation) |
| 382 | + CFRunLoopStop(CFRunLoopGetCurrent()) |
| 383 | + #else |
| 384 | + RunLoop.current._stop() |
| 385 | + #endif |
347 | 386 | } |
348 | 387 | } |
349 | 388 | } |
@@ -377,23 +416,4 @@ internal func pollBlock( |
377 | 416 | return result |
378 | 417 | } |
379 | 418 |
|
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 | 419 | #endif // #if !os(WASI) |
0 commit comments