         lock.dealloc(1)
     }
 
+    /**
+    Call `body()` with a reading lock.
+
+    The given function may be called concurrently with reads on other threads.
+
+    :param: body A function that reads a value while locked.
+    :returns: The value returned from the given function.
+    */
     public func withReadLock<T>(@noescape body: () -> T) -> T {
         let result: T
         pthread_rwlock_rdlock(lock)


import Foundation

public protocol ReadWriteLock {
    func withReadLock<T>(@noescape body: () -> T) -> T
    func withWriteLock<T>(@noescape body: () -> T) -> T
}

public struct DispatchLock: ReadWriteLock {
    private let semaphore = dispatch_semaphore_create(1)

    public init() {}

    private func withLock<T>(@noescape body: () -> T) -> T {

        _state.dealloc(1)
    }

    /**
    Call `body()` with a writing lock.

    The given function is guaranteed to be called exclusively.

    :param: body A function that writes a value while locked, then returns some
    value.

    :returns: The value returned from the given function.
    */
    public func withWriteLock<T>(@noescape body: () -> T) -> T {
        // spin until we acquire write lock
        do {
