0001 // 0002 // LockProtected.swift 0003 // ReadWriteLock 0004 // 0005 // Created by John Gallagher on 7/17/14. 0006 // Copyright © 2014-2015 Big Nerd Ranch. Licensed under MIT. 0007 // 0008 0009 /// A `LockProtected` holds onto a value of type T, but only allows access to it 0010 /// from within a locking statement. This prevents accidental unsafe access when 0011 /// thread safety is desired. 0012 public final class LockProtected<T> { 0013 private var lock
LockProtected.swift:14 private var item: TLockProtected.swift:17 public convenience init(item: T) {LockProtected.swift:23 public init(item: T, lock: ReadWriteLock) {LockProtected.swift:31 public func withReadLock<Return>(@noescape body: T throws -> Return) rethrows -> Return {LockProtected.swift:40 public func withWriteLock<Return>(@noescape body: (inout T) throws -> Return) rethrows -> Return {: ReadWriteLock 0014 private var item
LockProtected.swift:25 self.lock = lockLockProtected.swift:32 return try lock.withReadLock {LockProtected.swift:41 return try lock.withWriteLock {: T 0015 0016 /// Create the protected value with an initial item and a default lock. 0017 public convenience init(item: T) { 0018 self.init(item: item, lock: CASSpinLock()) 0019 } 0020 0021 /// Create the protected value with an initial item and a type implementing 0022 /// a lock. 0023 public init
LockProtected.swift:24 self.item = itemLockProtected.swift:33 try body(self.item)LockProtected.swift:42 try body(&self.item)(item: T, lock: ReadWriteLock) { 0024 self.item = item 0025 self.lock = lock 0026 } 0027 0028 /// Give read access to the item within `body`. 0029 /// - parameter body: A function that reads from the contained item. 0030 /// - returns: The value returned from the given function. 0031 public func withReadLock<Return>(@noescape body: T throws -> Return) rethrows -> Return { 0032 return try lock.withReadLock { 0033 try body(self.item) 0034 } 0035 } 0036 0037 /// Give write access to the item within the given function. 0038 /// - parameter body: A function that writes to the contained item, and returns some value. 0039 /// - returns: The value returned from the given function. 0040 public func withWriteLock<Return>(@noescape body: (inout T) throws -> Return) rethrows -> Return { 0041 return try lock.withWriteLock { 0042 try body(&self.item) 0043 } 0044 } 0045 } 0046
LockProtected.swift:18 self.init(item: item, lock: CASSpinLock())