Link Search Menu Expand Document

Unstable-Funcons-beta : Locks.cbs | PRETTY | PDF

Outline

Locks

A thread may request locks, and release locks (held by itself or by another thread). A reentrant lock may be held more than once by the same thread. A shared lock may be held by multiple threads at the same time, whereas an exclusive lock can be held by only one thread at the same time. A request for a spinlock that is held by another thread involves busy waiting instead of suspension, and assumes interleaving of the execution of a waiting thread and the holding thread.

Funcon
  is-exclusive-lock-holder(SY:syncs) : =>booleans
   ~> is-equal(current-thread, assigned sync-feature(SY, sync-holder))
Spin locks

Spin locks support mutual exclusion, but not suspension of blocked requests. thread-spin spin-lock-sync SY repeatedly executes the request for the lock until the request is granted, which is called busy waiting. Interleaving of different threads waiting for the same spin lock may result in granting requests out of order.

When the lock is not currently held, granting a request for it sets the holder to the current thread; otherwise the request fails.

Releasing the lock leaves the lock free. Only the thread that holds the lock can release it. Releasing cannot be blocked by other threads, so it is not a request.

Exclusive locks

Exclusive locks support mutual exclusion and suspension of blocked requests. An exclusive lock – also called a mutex – can be held by only one thread at the same time. It can be used to ensure mutual exclusion of so-called critical regions of thread bodies, and to avoid potential interference due to thread interleaving.

When the lock is not currently held, granting a request for it sets the holder to the current thread; otherwise the request fails.

When the request fails, the current thread is added to the waiting list, and suspended until the request can be granted:

When the waiting list is non-empty, releasing the lock grants it to the thread that made the first request in the list, and resumes that thread; otherwise it leaves the lock free. Only the thread that holds the lock can release it. Releasing a lock cannot be blocked by other threads, so it is not a request.

Rentrant locks

Reentrant exclusive locks support mutual exclusion, suspension of blocked requests, and reentry. A reentrant exclusive lock can be held (and subsequently released) multiple times by the same thread.

When the lock is not currently held, granting a request for it sets the holder to the current thread; if it is already held by the current thread, it merely increments the counter; otherwise the request fails.

When the request fails, the current thread is added to the waiting list, and suspended until the request can be granted:

When the waiting list is non-empty, releasing the lock grants it to the thread that made the first request in the list, and resumes that thread; otherwise it leaves the lock free. Only the thread that holds the lock can release it. Releasing a lock cannot be blocked by other threads, so it is not a request.

When the reentered count is positive, an exit merely decrements it. Otherwise it is 0, and the exit releases the lock.

Semaphores

A semaphore is a shared lock with a specified limit on the number of threads that can hold it at the same time. A semaphore can be released by any thread.

When the semaphore is available, granting a request for it decrements the number of further threads that can hold it; otherwise the request fails.

When the request fails, the current thread is added to the waiting list, and suspended until the request can be granted:

When the waiting list is empty, releasing the semaphore increments the counter; otherwise it grants the semaphore to the thread that made the first request in the list, and resumes that thread. Releasing a semaphore cannot be blocked, so it is not a request.

Shared-exclusive locks

A shared-exclusive lock – also called a readers-writer (rw) lock – can be held exclusively by a single thread, or shared by any number of threads at the same time. It can be released by any thread.

When the lock is not currently held at all, it can be granted exclusively:

When the lock is not currently held exclusively, a request to share it is always granted immediately (regardless of any waiting exclusive requests):

If the request fails, the current thread is added to the waiting list, and suspended until the request can be granted.

The waiting list of a shared-exclusive lock records not only the thread but also whether the request is for sharing:

When the waiting list is non-empty, releasing the lock may grant either the first waiting exclusive request, or all waiting shared requests. A scheduler may defer granting one kind of request when there are waiting requests of the other kind, irrespective of the order in which those requests were made. Releasing a lock cannot be blocked by other threads, so it is not a request.

rw-lock-sync(SY) assumes that SY is not held (either exclusively or shared). If the first waiting request is for sharing, any further sharing requests are granted,

rw-lock-sync-all-shared(SY) updates the waiting list by removing and resuming all its sharing requests: