Locking

public protocol Locking

A type that mutually excludes execution of code such that only one unit of code is running at any given time. An implementing type may choose to have readers-writer semantics, such that many readers can read at once, or lock around all reads and writes the same way.

  • Call body with a reading lock.

    If the implementing type models a readers-writer lock, this function may behave differently to withWriteLock(_:).

    Declaration

    Swift

    func withReadLock<Return>(_ body: () throws -> Return) rethrows -> Return

    Parameters

    body

    A function that reads a value while locked.

    Return Value

    The value returned from the given function.

  • withAttemptedReadLock(_:) Default implementation

    Attempt to call body with a reading lock.

    If the lock cannot immediately be taken, return nil instead of executing body.

    See

    withReadLock(_:)

    Default Implementation

    Declaration

    Swift

    func withAttemptedReadLock<Return>(_ body: () throws -> Return) rethrows -> Return?

    Return Value

    The value returned from the given function, or nil.

  • withWriteLock(_:) Default implementation

    Call body with a writing lock.

    If the implementing type models a readers-writer lock, this function may behave differently to withReadLock(_:).

    Default Implementation

    Declaration

    Swift

    func withWriteLock<Return>(_ body: () throws -> Return) rethrows -> Return

    Parameters

    body

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

    Return Value

    The value returned from the given function.