Mutex+Protector.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Mutex+Protector.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. // MARK: -
  26. /// An `os_unfair_lock` wrapper.
  27. final class UnfairLock {
  28. private var unfairLock = os_unfair_lock()
  29. fileprivate func lock() {
  30. os_unfair_lock_lock(&unfairLock)
  31. }
  32. fileprivate func unlock() {
  33. os_unfair_lock_unlock(&unfairLock)
  34. }
  35. ///
  36. /// - Parameter closure: The closure to run.
  37. /// - Returns: The value the closure generated.
  38. func around<T>(_ closure: () -> T) -> T {
  39. lock(); defer { unlock() }
  40. return closure()
  41. }
  42. /// Execute a closure while aquiring the lock.
  43. ///
  44. /// - Parameter closure: The closure to run.
  45. func around(_ closure: () -> Void) {
  46. lock(); defer { unlock() }
  47. return closure()
  48. }
  49. }
  50. /// A thread-safe wrapper around a value.
  51. final class Protector<T> {
  52. private let lock = UnfairLock()
  53. private var value: T
  54. init(_ value: T) {
  55. self.value = value
  56. }
  57. /// The contained value. Unsafe for anything more than direct read or write.
  58. var directValue: T {
  59. get { return lock.around { value } }
  60. set { lock.around { value = newValue } }
  61. }
  62. /// Synchronously read or transform the contained value.
  63. ///
  64. /// - Parameter closure: The closure to execute.
  65. /// - Returns: The return value of the closure passed.
  66. func read<U>(_ closure: (T) -> U) -> U {
  67. return lock.around { closure(self.value) }
  68. }
  69. /// Synchronously modify the protected value.
  70. ///
  71. /// - Parameter closure: The closure to execute.
  72. /// - Returns: The modified value.
  73. @discardableResult
  74. func write<U>(_ closure: (inout T) -> U) -> U {
  75. return lock.around { closure(&self.value) }
  76. }
  77. }
  78. extension Protector where T: RangeReplaceableCollection {
  79. func append(_ newElement: T.Iterator.Element) {
  80. write { (ward: inout T) in
  81. ward.append(newElement)
  82. }
  83. }
  84. func append<S: Sequence>(contentsOf newElements: S) where S.Iterator.Element == T.Iterator.Element {
  85. write { (ward: inout T) in
  86. ward.append(contentsOf: newElements)
  87. }
  88. }
  89. func append<C: Collection>(contentsOf newElements: C) where C.Iterator.Element == T.Iterator.Element {
  90. write { (ward: inout T) in
  91. ward.append(contentsOf: newElements)
  92. }
  93. }
  94. }
  95. extension Protector where T: Strideable {
  96. func advance(by stride: T.Stride) {
  97. write { (ward: inout T) in
  98. ward = ward.advanced(by: stride)
  99. }
  100. }
  101. }