Rx.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Rx.swift
  3. // RxSwift
  4. //
  5. // Created by Krunoslav Zaher on 2/14/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. #if TRACE_RESOURCES
  9. private let resourceCount = AtomicInt(0)
  10. /// Resource utilization information
  11. public struct Resources {
  12. /// Counts internal Rx resource allocations (Observables, Observers, Disposables, etc.). This provides a simple way to detect leaks during development.
  13. public static var total: Int32 {
  14. return load(resourceCount)
  15. }
  16. /// Increments `Resources.total` resource count.
  17. ///
  18. /// - returns: New resource count
  19. public static func incrementTotal() -> Int32 {
  20. return increment(resourceCount)
  21. }
  22. /// Decrements `Resources.total` resource count
  23. ///
  24. /// - returns: New resource count
  25. public static func decrementTotal() -> Int32 {
  26. return decrement(resourceCount)
  27. }
  28. }
  29. #endif
  30. /// Swift does not implement abstract methods. This method is used as a runtime check to ensure that methods which intended to be abstract (i.e., they should be implemented in subclasses) are not called directly on the superclass.
  31. func rxAbstractMethod(file: StaticString = #file, line: UInt = #line) -> Swift.Never {
  32. rxFatalError("Abstract method", file: file, line: line)
  33. }
  34. func rxFatalError(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) -> Swift.Never {
  35. fatalError(lastMessage(), file: file, line: line)
  36. }
  37. func rxFatalErrorInDebug(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) {
  38. #if DEBUG
  39. fatalError(lastMessage(), file: file, line: line)
  40. #else
  41. print("\(file):\(line): \(lastMessage())")
  42. #endif
  43. }
  44. func incrementChecked(_ i: inout Int) throws -> Int {
  45. if i == Int.max {
  46. throw RxError.overflow
  47. }
  48. defer { i += 1 }
  49. return i
  50. }
  51. func decrementChecked(_ i: inout Int) throws -> Int {
  52. if i == Int.min {
  53. throw RxError.overflow
  54. }
  55. defer { i -= 1 }
  56. return i
  57. }
  58. #if DEBUG
  59. import class Foundation.Thread
  60. final class SynchronizationTracker {
  61. private let _lock = RecursiveLock()
  62. public enum SynchronizationErrorMessages: String {
  63. case variable = "Two different threads are trying to assign the same `Variable.value` unsynchronized.\n This is undefined behavior because the end result (variable value) is nondeterministic and depends on the \n operating system thread scheduler. This will cause random behavior of your program.\n"
  64. case `default` = "Two different unsynchronized threads are trying to send some event simultaneously.\n This is undefined behavior because the ordering of the effects caused by these events is nondeterministic and depends on the \n operating system thread scheduler. This will result in a random behavior of your program.\n"
  65. }
  66. private var _threads = [UnsafeMutableRawPointer: Int]()
  67. private func synchronizationError(_ message: String) {
  68. #if FATAL_SYNCHRONIZATION
  69. rxFatalError(message)
  70. #else
  71. print(message)
  72. #endif
  73. }
  74. func register(synchronizationErrorMessage: SynchronizationErrorMessages) {
  75. self._lock.lock(); defer { self._lock.unlock() }
  76. let pointer = Unmanaged.passUnretained(Thread.current).toOpaque()
  77. let count = (self._threads[pointer] ?? 0) + 1
  78. if count > 1 {
  79. self.synchronizationError(
  80. "⚠️ Reentrancy anomaly was detected.\n" +
  81. " > Debugging: To debug this issue you can set a breakpoint in \(#file):\(#line) and observe the call stack.\n" +
  82. " > Problem: This behavior is breaking the observable sequence grammar. `next (error | completed)?`\n" +
  83. " This behavior breaks the grammar because there is overlapping between sequence events.\n" +
  84. " Observable sequence is trying to send an event before sending of previous event has finished.\n" +
  85. " > Interpretation: This could mean that there is some kind of unexpected cyclic dependency in your code,\n" +
  86. " or that the system is not behaving in the expected way.\n" +
  87. " > Remedy: If this is the expected behavior this message can be suppressed by adding `.observeOn(MainScheduler.asyncInstance)`\n" +
  88. " or by enqueuing sequence events in some other way.\n"
  89. )
  90. }
  91. self._threads[pointer] = count
  92. if self._threads.count > 1 {
  93. self.synchronizationError(
  94. "⚠️ Synchronization anomaly was detected.\n" +
  95. " > Debugging: To debug this issue you can set a breakpoint in \(#file):\(#line) and observe the call stack.\n" +
  96. " > Problem: This behavior is breaking the observable sequence grammar. `next (error | completed)?`\n" +
  97. " This behavior breaks the grammar because there is overlapping between sequence events.\n" +
  98. " Observable sequence is trying to send an event before sending of previous event has finished.\n" +
  99. " > Interpretation: " + synchronizationErrorMessage.rawValue +
  100. " > Remedy: If this is the expected behavior this message can be suppressed by adding `.observeOn(MainScheduler.asyncInstance)`\n" +
  101. " or by synchronizing sequence events in some other way.\n"
  102. )
  103. }
  104. }
  105. func unregister() {
  106. self._lock.lock(); defer { self._lock.unlock() }
  107. let pointer = Unmanaged.passUnretained(Thread.current).toOpaque()
  108. self._threads[pointer] = (self._threads[pointer] ?? 1) - 1
  109. if self._threads[pointer] == 0 {
  110. self._threads[pointer] = nil
  111. }
  112. }
  113. }
  114. #endif
  115. /// RxSwift global hooks
  116. public enum Hooks {
  117. // Should capture call stack
  118. public static var recordCallStackOnError: Bool = false
  119. }