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