1/26
Looks like no tags are added yet.
Spinlock
A lock that causes a thread to loop (or spin) when trying to acquire it when locked
Spinlock benefits
Sufficient for short critical sections (ie. within other locks)
Lightweight
Spinlock drawbacks
Requires a lot of CPU to spin
No FIFO (some thread may lose every time)
No bounded-waiting
Wait channel
Manages a list of sleeping threads, abstracting the details of the thread scheduler
Semaphore
Initialized with N available permits
Binary semaphore
A semaphore initialized with N=1 available permits, with a paired wait/post
Mutex
A blocking lock for mutual exclusion similar to a semaphore, but with no counter
Condition variables
A mutual-exclusion pattern to wait for changes in a shared state
cond_wait(c, m)
m is atomically unlocked
Wait
Re-acquire m and resume executing automatically
cond_signal(c)
Wait up a single thread waiting on a condition
cond_broadcast(c)
Wake up all threads waiting on a condition
Steps of cond_signal(c)
Call WaitChannel_Wake(c->chan)
Steps of cond_broadcast(c)
Call WaitChanne_WakeAll(c->chan)
Steps of cond_wait(c, m)
Lock the wait channel with WaitChannel_Lock(c->chan)
Unlock m with Mutex_Unlock(m)
Call WaitChannel_Wait(c->chan)
Re-aquire m with Mutex_Lock(m)
acquire()
Atomically check if the lock is already acquired, and acquire it if not
release()
Atomically release the lock and set it as unlocked
WaitChannel_Lock(wc)
Protect the queue registration and disallow new threads from being queued on the wait channel
WaitChannel_Sleep(wc)
Queues the caller, releases the lock, and puts the calling thead to sleep
Caller must hold the channel lock
Caller does not re-acquire the lock
WaitChannel_Wake(wc)
Wake up a sleeping thread on the wait channel, and acquires the channel lock automatically
WaitChannel_WakeAll(wc)
Wakes up all threads sleeping on the wait channel, and acquires the channel lock automatically
wait(sem) (P)
Consume the available permit, and block if none are available
post(sem) (V)
Add a new available permit, allow a waiter to proceed
Steps of Semaphore_Wait(s)
Lock the internal spinlock Spinlock_Lock(s->lock)
While there are no permits available (count = 0):
Lock the wait channel WaitChannel_Lock(s->wc)
Unlock the internal spinlock Spinlock_Unlock(s->lock)
Sleep on the wait channel WaitChannel_Sleep(s->wc)
Lock the internal spinlock Spinlock_Lock(s->lock)
Decrement the permit count
Unlock the internal spinlock Spinlock_Unlock(s->lock)
Steps of Semaphore_Post(s)
Lock the internal spinlock Spinlock_Lock(s->lock)
Increment the permit count
Wake a sleeping thread on the wait channel WaitChannel_Wake(s->wc)
Unlock the internal spinlock Spinlock_Unlock(s->lock)
Steps of Mutex_Lock(m)
Lock the internal spinlock Spinlock_Lock(m->lock)
While the mutex is currently locked:
Lock the wait channel WaitChannel_Lock(m->wc)
Unlock the internal spinlock Spinlock_Unlock(m->lock)
Sleep on the wait channel WaitChannel_Sleep(m->wc)
Lock the internal spinlock Spinlock_Lock(m->lock)
Set the status of the mutex as locked
Set the owner of the mutex as the current thread
Unlock the internal spinlock Spinlock_Unlock(m->lock)
Steps of Mutex_TryLock(m)
Lock the internal spinlock Spinlock_Lock(m->lock)
If the mutex is currently locked:
Return EBUSY
Set the status of the mutex as locked
Set the owner of the mutex as the current thread
Unlock the internal spinlock Spinlock_Unlock(m->lock)
Steps of Mutex_Unlock(m)
Lock the internal spinlock Spinlock_Lock(m->lock)
Assert the current thread owns the mutex
Assert the mutex is currently locked
Set the owner of the mutex to NULL
Set the status of the mutex to unlocked
Wake a sleeping thread on the wait channel WaitChannel_Wake(m->wc)
Unlock the internal spinlock Spinlock_Unlock(m->lock)