CS 350 Concurrency APIs

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

27 Terms

1
New cards

Spinlock

A lock that causes a thread to loop (or spin) when trying to acquire it when locked

2
New cards

Spinlock benefits

  • Sufficient for short critical sections (ie. within other locks)

  • Lightweight


3
New cards

Spinlock drawbacks

  • Requires a lot of CPU to spin

  • No FIFO (some thread may lose every time)

  • No bounded-waiting


4
New cards

Wait channel

Manages a list of sleeping threads, abstracting the details of the thread scheduler

5
New cards

Semaphore

Initialized with NN available permits

6
New cards

Binary semaphore

A semaphore initialized with N=1N=1 available permits, with a paired wait/post

7
New cards

Mutex

A blocking lock for mutual exclusion similar to a semaphore, but with no counter

8
New cards

Condition variables

A mutual-exclusion pattern to wait for changes in a shared state

9
New cards

cond_wait(c, m)

  1. m is atomically unlocked

  2. Wait

  3. Re-acquire m and resume executing automatically


10
New cards

cond_signal(c)

Wait up a single thread waiting on a condition


11
New cards

cond_broadcast(c)

Wake up all threads waiting on a condition

12
New cards

Steps of cond_signal(c)

  1. Call WaitChannel_Wake(c->chan)


13
New cards

Steps of cond_broadcast(c)

  1. Call WaitChanne_WakeAll(c->chan)


14
New cards

Steps of cond_wait(c, m)

  1. Lock the wait channel with WaitChannel_Lock(c->chan)

  2. Unlock m with Mutex_Unlock(m)

  3. Call WaitChannel_Wait(c->chan)

  4. Re-aquire m with Mutex_Lock(m)


15
New cards

acquire()

Atomically check if the lock is already acquired, and acquire it if not

16
New cards

release()

Atomically release the lock and set it as unlocked

17
New cards

WaitChannel_Lock(wc)

Protect the queue registration and disallow new threads from being queued on the wait channel

18
New cards

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


19
New cards

WaitChannel_Wake(wc)

Wake up a sleeping thread on the wait channel, and acquires the channel lock automatically

20
New cards

WaitChannel_WakeAll(wc)

Wakes up all threads sleeping on the wait channel, and acquires the channel lock automatically

21
New cards

wait(sem) (P)

Consume the available permit, and block if none are available

22
New cards

post(sem) (V)

Add a new available permit, allow a waiter to proceed

23
New cards

Steps of Semaphore_Wait(s)

  1. Lock the internal spinlock Spinlock_Lock(s->lock)

  2. 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)

  3. Decrement the permit count

  4. Unlock the internal spinlock Spinlock_Unlock(s->lock)


24
New cards

Steps of Semaphore_Post(s)

  1. Lock the internal spinlock Spinlock_Lock(s->lock)

  2. Increment the permit count

  3. Wake a sleeping thread on the wait channel WaitChannel_Wake(s->wc)

  4. Unlock the internal spinlock Spinlock_Unlock(s->lock)


25
New cards

Steps of Mutex_Lock(m)

  1. Lock the internal spinlock Spinlock_Lock(m->lock)

  2. 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)

  3. Set the status of the mutex as locked

  4. Set the owner of the mutex as the current thread

  5. Unlock the internal spinlock Spinlock_Unlock(m->lock)


26
New cards

Steps of Mutex_TryLock(m)

  1. Lock the internal spinlock Spinlock_Lock(m->lock)

  2. If the mutex is currently locked:

    • Return EBUSY

  3. Set the status of the mutex as locked

  4. Set the owner of the mutex as the current thread

  5. Unlock the internal spinlock Spinlock_Unlock(m->lock)


27
New cards

Steps of Mutex_Unlock(m)

  1. Lock the internal spinlock Spinlock_Lock(m->lock)

  2. Assert the current thread owns the mutex

  3. Assert the mutex is currently locked

  4. Set the owner of the mutex to NULL

  5. Set the status of the mutex to unlocked

  6. Wake a sleeping thread on the wait channel WaitChannel_Wake(m->wc)

  7. Unlock the internal spinlock Spinlock_Unlock(m->lock)