CallbackQueue.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // CallbackQueue.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/10/15.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. public typealias ExecutionQueue = CallbackQueue
  28. /// Represents the behavior of the callback queue selection when a closure is dispatched.
  29. public enum CallbackQueue: Sendable {
  30. /// Dispatches the closure to `DispatchQueue.main` with an `async` behavior.
  31. case mainAsync
  32. /// Dispatches the closure to `DispatchQueue.main` with an `async` behavior if the current queue is not `.main`.
  33. /// Otherwise, it calls the closure immediately on the current main queue.
  34. case mainCurrentOrAsync
  35. /// Does not change the calling queue for the closure.
  36. case untouch
  37. /// Dispatches the closure to a specified `DispatchQueue`.
  38. case dispatch(DispatchQueue)
  39. /// Executes the `block` in a dispatch queue defined by `self`.
  40. /// - Parameter block: The block needs to be executed.
  41. public func execute(_ block: @Sendable @escaping () -> Void) {
  42. switch self {
  43. case .mainAsync:
  44. CallbackQueueMain.async { block() }
  45. case .mainCurrentOrAsync:
  46. CallbackQueueMain.currentOrAsync { block() }
  47. case .untouch:
  48. block()
  49. case .dispatch(let queue):
  50. queue.async { block() }
  51. }
  52. }
  53. var queue: DispatchQueue {
  54. switch self {
  55. case .mainAsync: return .main
  56. case .mainCurrentOrAsync: return .main
  57. case .untouch: return OperationQueue.current?.underlyingQueue ?? .main
  58. case .dispatch(let queue): return queue
  59. }
  60. }
  61. }
  62. enum CallbackQueueMain {
  63. static func currentOrAsync(_ block: @MainActor @Sendable @escaping () -> Void) {
  64. if Thread.isMainThread {
  65. MainActor.runUnsafely { block() }
  66. } else {
  67. DispatchQueue.main.async { block() }
  68. }
  69. }
  70. static func async(_ block: @MainActor @Sendable @escaping () -> Void) {
  71. DispatchQueue.main.async { block() }
  72. }
  73. }
  74. extension MainActor {
  75. @_unavailableFromAsync
  76. static func runUnsafely<T: Sendable>(_ body: @MainActor () throws -> T) rethrows -> T {
  77. #if swift(>=5.10)
  78. return try MainActor.assumeIsolated(body)
  79. #else
  80. dispatchPrecondition(condition: .onQueue(.main))
  81. return try withoutActuallyEscaping(body) { fn in
  82. try unsafeBitCast(fn, to: (() throws -> T).self)()
  83. }
  84. #endif
  85. }
  86. }