Protected.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Foundation
  2. private protocol Lock {
  3. func lock()
  4. func unlock()
  5. }
  6. extension Lock {
  7. /// Executes a closure returning a value while acquiring the lock.
  8. ///
  9. /// - Parameter closure: The closure to run.
  10. ///
  11. /// - Returns: The value the closure generated.
  12. func around<T>(_ closure: () -> T) -> T {
  13. lock()
  14. defer { unlock() }
  15. return closure()
  16. }
  17. /// Execute a closure while acquiring the lock.
  18. ///
  19. /// - Parameter closure: The closure to run.
  20. func around(_ closure: () -> Void) {
  21. lock()
  22. defer { unlock() }
  23. closure()
  24. }
  25. }
  26. /// An `os_unfair_lock` wrapper.
  27. final class UnfairLock: Lock {
  28. private let unfairLock: os_unfair_lock_t
  29. init() {
  30. unfairLock = .allocate(capacity: 1)
  31. unfairLock.initialize(to: os_unfair_lock())
  32. }
  33. deinit {
  34. unfairLock.deinitialize(count: 1)
  35. unfairLock.deallocate()
  36. }
  37. fileprivate func lock() {
  38. os_unfair_lock_lock(unfairLock)
  39. }
  40. fileprivate func unlock() {
  41. os_unfair_lock_unlock(unfairLock)
  42. }
  43. }
  44. /// A thread-safe wrapper around a value.
  45. @propertyWrapper
  46. @dynamicMemberLookup
  47. final class Protected<T> {
  48. private let lock = UnfairLock()
  49. private var value: T
  50. init(_ value: T) {
  51. self.value = value
  52. }
  53. /// The contained value. Unsafe for anything more than direct read or write.
  54. var wrappedValue: T {
  55. get { lock.around { value } }
  56. set { lock.around { value = newValue } }
  57. }
  58. var projectedValue: Protected<T> { self }
  59. init(wrappedValue: T) {
  60. value = wrappedValue
  61. }
  62. /// Synchronously read or transform the contained value.
  63. ///
  64. /// - Parameter closure: The closure to execute.
  65. ///
  66. /// - Returns: The return value of the closure passed.
  67. func read<U>(_ closure: (T) -> U) -> U {
  68. lock.around { closure(self.value) }
  69. }
  70. /// Synchronously modify the protected value.
  71. ///
  72. /// - Parameter closure: The closure to execute.
  73. ///
  74. /// - Returns: The modified value.
  75. @discardableResult func write<U>(_ closure: (inout T) -> U) -> U {
  76. lock.around { closure(&self.value) }
  77. }
  78. subscript<Property>(dynamicMember keyPath: WritableKeyPath<T, Property>) -> Property {
  79. get { lock.around { value[keyPath: keyPath] } }
  80. set { lock.around { value[keyPath: keyPath] = newValue } }
  81. }
  82. }
  83. extension Protected where T: RangeReplaceableCollection {
  84. /// Adds a new element to the end of this protected collection.
  85. ///
  86. /// - Parameter newElement: The `Element` to append.
  87. func append(_ newElement: T.Element) {
  88. write { (ward: inout T) in
  89. ward.append(newElement)
  90. }
  91. }
  92. /// Adds the elements of a sequence to the end of this protected collection.
  93. ///
  94. /// - Parameter newElements: The `Sequence` to append.
  95. func append<S: Sequence>(contentsOf newElements: S) where S.Element == T.Element {
  96. write { (ward: inout T) in
  97. ward.append(contentsOf: newElements)
  98. }
  99. }
  100. /// Add the elements of a collection to the end of the protected collection.
  101. ///
  102. /// - Parameter newElements: The `Collection` to append.
  103. func append<C: Collection>(contentsOf newElements: C) where C.Element == T.Element {
  104. write { (ward: inout T) in
  105. ward.append(contentsOf: newElements)
  106. }
  107. }
  108. }
  109. extension Protected where T == Data? {
  110. /// Adds the contents of a `Data` value to the end of the protected `Data`.
  111. ///
  112. /// - Parameter data: The `Data` to be appended.
  113. func append(_ data: Data) {
  114. write { (ward: inout T) in
  115. ward?.append(data)
  116. }
  117. }
  118. }