Documentation

Std.Sync.Mutex

Mutual exclusion primitive (a lock).

If you want to guard shared state, use Mutex α instead.

Equations
Instances For
    @[extern lean_io_basemutex_new]

    Creates a new BaseMutex.

    @[extern lean_io_basemutex_lock]

    Locks a BaseMutex. Waits until no other thread has locked the mutex.

    The current thread must not have already locked the mutex. Reentrant locking is undefined behavior (inherited from the C++ implementation). If this is unavoidable in your code, consider using BaseRecursiveMutex.

    @[extern lean_io_basemutex_try_lock]

    Attempts to lock a BaseMutex. If the mutex is not available return false, otherwise lock it and return true.

    This function does not block.

    The current thread must not have already locked the mutex. Reentrant locking is undefined behavior (inherited from the C++ implementation). If this is unavoidable in your code, consider using BaseRecursiveMutex.

    @[extern lean_io_basemutex_unlock]

    Unlocks a BaseMutex.

    The current thread must have already locked the mutex. Unlocking an unlocked mutex is undefined behavior (inherited from the C++ implementation). If this is unavoidable in your code, consider using BaseRecursiveMutex.

    Condition variable, a synchronization primitive to be used with a BaseMutex or Mutex.

    The thread that wants to modify the shared variable must:

    1. Lock the BaseMutex or Mutex
    2. Work on the shared variable
    3. Call Condvar.notifyOne or Condvar.notifyAll after it is done. Note that this may be done before or after the mutex is unlocked.

    If working with a Mutex the thread that waits on the Condvar can use Mutex.atomicallyOnce to wait until a condition is true. If working with a BaseMutex it must:

    1. Lock the BaseMutex.
    2. Do one of the following:
    • Use Condvar.waitUntil to (potentially repeatedly wait) on the condition variable until the condition is true.
    • Implement the waiting manually by:
      1. Checking the condition
      2. Calling Condvar.wait which releases the BaseMutex and suspends execution until the condition variable is notified.
      3. Check the condition and resume waiting if not satisfied.
    Equations
    Instances For
      @[extern lean_io_condvar_new]

      Creates a new condition variable.

      @[extern lean_io_condvar_wait]
      opaque Std.Condvar.wait (condvar : Condvar) (mutex : BaseMutex) :

      Waits until another thread calls notifyOne or notifyAll.

      @[extern lean_io_condvar_notify_one]

      Wakes up a single other thread executing wait.

      @[extern lean_io_condvar_notify_all]

      Wakes up all other threads executing wait.

      def Std.Condvar.waitUntil {m : TypeType u_1} [Monad m] [MonadLift BaseIO m] (condvar : Condvar) (mutex : BaseMutex) (pred : m Bool) :

      Waits on the condition variable until the predicate is true.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        structure Std.Mutex (α : Type) :

        Mutual exclusion primitive (lock) guarding shared state of type α.

        The type Mutex α is similar to IO.Ref α, except that concurrent accesses are guarded by a mutex instead of atomic pointer operations and busy-waiting.

        Instances For
          instance Std.instNonemptyMutex {α✝ : Type} [Nonempty α✝] :
          Nonempty (Mutex α✝)
          def Std.Mutex.new {α : Type} (a : α) :

          Creates a new mutex.

          Equations
          Instances For
            def Std.Mutex.atomically {m : TypeType} {α β : Type} [Monad m] [MonadLiftT BaseIO m] [MonadFinally m] (mutex : Mutex α) (k : AtomicT α m β) :
            m β

            mutex.atomically k runs k with access to the mutex's state while locking the mutex.

            Calling mutex.atomically while already holding the underlying BaseMutex in the same thread is undefined behavior. If this is unavoidable in your code, consider using RecursiveMutex.

            Equations
            Instances For
              def Std.Mutex.tryAtomically {m : TypeType} {α β : Type} [Monad m] [MonadLiftT BaseIO m] [MonadFinally m] (mutex : Mutex α) (k : AtomicT α m β) :
              m (Option β)

              mutex.tryAtomically k tries to lock mutex and runs k on it if it succeeds. On success the return value of k is returned as some, on failure none is returned.

              This function does not block on the mutex. Additionally calling mutex.tryAtomically while already holding the underlying BaseMutex in the same thread is undefined behavior. If this is unavoidable in your code, consider using RecursiveMutex.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                def Std.Mutex.atomicallyOnce {m : TypeType} {α β : Type} [Monad m] [MonadLiftT BaseIO m] [MonadFinally m] (mutex : Mutex α) (condvar : Condvar) (pred : AtomicT α m Bool) (k : AtomicT α m β) :
                m β

                mutex.atomicallyOnce condvar pred k runs k, waiting on condvar until pred returns true. Both k and pred have access to the mutex's state.

                Calling mutex.atomicallyOnce while already holding the underlying BaseMutex in the same thread is undefined behavior. If this is unavoidable in your code, consider using RecursiveMutex.

                Equations
                Instances For