Title: CPS110: Reader-writer locks
1CPS110 Reader-writer locks
- Landon Cox
- January 26, 2009
2Locks thus far
- Lock anytime shared data is read/written
- Ensures correctness
- Only one thread can read/write at a time
- Would like more concurrency
- How, without exposing violated invariants?
- Allow multiple threads to read
- (as long as none are writing)
3Problem definition
- Shared state that many threads want to access
- Many more reads than writes
- ACES, Ebay, any large database
- When no writers, allow multiple readers
- One writer at a time, when no readers
- Goal build reader-writer locks using monitors
4Layers of synchronization
Queue, TMM, Soda machine, Disk scheduler
Concurrent program
Higher-level synchronization (reader-writer
functions)
High-level synchronization (locks, monitors,
semaphores)
Today
Coming lectures Project 1 lib
Hardware (load/store, interrupt enable/disable,
testset)
Advanced HW course
5Reader-writer interface
- readerStart (called when thread begins reading)
- readerFinish (called when thread is finished
reading) - writerStart (called when thread begins writing)
- writerFinish (called when thread is finished
writing) - If no threads between writerStart/writerFinish
- Many threads between readerStart/readerFinish
- Only 1 thread between writerStart/writerFinish
6Solving reader-writer locks
- What are the variables/shared state?
- Number of readers (numReaders)
- Number of writers (numWriters)
- Locks?
- 1 to protect all shared state (rwLock)
- Mutual exclusion?
- Only one thread in functions at a time
- Ordering constraints?
- readerStart must wait if there are writers
- writerStart must wait if there are readers or
writers - Due to overlap, use one CV (readerOrWriterFinishCV
)
7Reader-writer code
readerStart () lock (RWLock) while
(numWriters gt 0) wait (RWlock, rowfCV)
numReaders unlock (RWLock)
writerStart () lock (RWLock) while
(numWriters gt 0 numReaders gt 0)
wait (RWlock, rowfCV) numWriters
unlock (RWLock)
readerFinish () lock (RWLock) numReaders--
broadcast (rowfCV) unlock (RWLock)
writerFinish () lock (RWLock) numWriters--
broadcast (rowfCV) unlock (RWLock)
8Reader-writer code
readerStart () lock (RWLock) while
(numWriters gt 0) wait (RWlock, rowfCV)
numReaders unlock (RWLock)
writerStart () lock (RWLock) while
(numWriters gt 0 numReaders gt 0)
wait (RWlock, rowfCV) numWriters
unlock (RWLock)
readerFinish () lock (RWLock) broadcast
(rowfCV) numReaders-- unlock (RWLock)
writerFinish () lock (RWLock) numWriters--
broadcast (rowfCV) unlock (RWLock)
Sure, both are protected by RWLock.
9Reader-writer code
readerStart () lock (RWLock) while
(numWriters gt 0) wait (RWlock, rowfCV)
numReaders unlock (RWLock)
writerStart () lock (RWLock) while
(numWriters gt 0 numReaders gt 0)
wait (RWlock, rowfCV) numWriters
unlock (RWLock)
readerFinish () lock (RWLock) numReaders--
broadcast (rowfCV) unlock (RWLock)
writerFinish () lock (RWLock) numWriters--
broadcast (rowfCV) unlock (RWLock)
If writer leaves with waiting readers and
writers, who wins?
10Reader-writer code
readerStart () lock (RWLock) while
(numWriters gt 0) wait (RWlock, rowfCV)
numReaders unlock (RWLock)
writerStart () lock (RWLock) while
(numWriters gt 0 numReaders gt 0)
wait (RWlock, rowfCV) numWriters
unlock (RWLock)
readerFinish () lock (RWLock) numReaders--
broadcast (rowfCV) unlock (RWLock)
writerFinish () lock (RWLock) numWriters--
broadcast (rowfCV) unlock (RWLock)
How long might a writer have to wait?
11Prioritizing waiting writers
readerStart () lock (RWLock) while
((actWriters waitWriters) gt 0)
wait (RWlock, rowfCV) numReaders unlock
(RWLock)
writerStart () lock (RWLock) while
(actWriters gt 0 numReaders gt 0)
waitWriters wait (RWlock, rowfCV)
waitWriters-- actWriters unlock
(RWLock)
readerFinish () lock (RWLock) numReaders--
broadcast (rowfCV) unlock (RWLock)
writerFinish () lock (RWLock) actWriters--
broadcast (rowfCV) unlock (RWLock)
12When to use broadcast
- More than one thread might need to run
- If writer leaves, waiting readers should be woken
- Signal could wakeup wrong thread
- Wrong thread checks condition, re-sleeps
- (producer-consumer problem)
- Still have the spurious wakeup problem
- (lets try to fix that in our no-prior. sol.)
13Removing spurious wakeups
readerStart () lock (RWLock) while
(numWriters gt 0) wait (RWlock, rowfCV)
numReaders unlock (RWLock)
writerStart () lock (RWLock) while
(numWriters gt 0 numReaders gt 0)
wait (RWlock, rowfCV) numWriters
unlock (RWLock)
readerFinish () lock (RWLock) numReaders
if (numReaders 0) broadcast (rowfCV)
unlock (RWLock)
writerFinish () lock (RWLock) numWriters--
broadcast (rowfCV) unlock (RWLock)
14Is it ok to use signal?
readerStart () lock (RWLock) while
(numWriters gt 0) wait (RWlock, rowfCV)
numReaders unlock (RWLock)
writerStart () lock (RWLock) while
(numWriters gt 0 numReaders gt 0)
wait (RWlock, rowfCV) numWriters
unlock (RWLock)
readerFinish () lock (RWLock) numReaders
if (numReaders 0) signal (rowfCV) unlock
(RWLock)
writerFinish () lock (RWLock) numWriters--
broadcast (rowfCV) unlock (RWLock)
Sure wont wake up any readers, only one writer
can run anyway
15Reader-writer interface vs locks
- R-W interface looks a lot like locking
- Start lock
- Finish unlock
- Standard terminology
- Four functions called reader-writer locks
- Between readStart/readFinish has read lock
- Between writeStart/writeFinish has write lock
- Pros/cons of R-W vs standard locks?
- Trade-off concurrency for complexity
- Must know how data is being accessed in critical
section
16Course administration
- Project 1
- Due February 18
- Should be able to do disk scheduler
- Can start thread library next week
- Start early (i.e., this week)!
- Other questions/concerns?
17Semaphores
- First defined by Dijkstra in late 60s
- Two operations up and down
// aka P (proberen) down () do //
begin atomic if (value gt 0) value--
break // end atomic while (1)
// aka V (verhogen) up () // begin
atomic value // end atomic
What is going on here?
Can value ever be lt 0?
18More semaphores
- Key state of a semaphore is its value
- Initial value determines semaphores behavior
- Value cannot be accessed outside semaphor
- (i.e. there is no semaphore.getValue() call)
- Semaphores can be both synchronization types
- Mutual exclusion (like locks)
- Ordering constraints (like monitors)
19Semaphore mutual exclusion
- Ensure that 1 (or lt N) thread is in critical
section - How do we make a semaphore act like a lock?
- Set initial value to 1 (or N)
- Like lock/unlock, but more general
- (could allow 2 threads in critical section if
initial value 2) - Lock is equivalent to a binary semaphore
s.down () // critical section s.up ()
20Semaphore ordering constraints
- Thread A waits for B before proceeding
- How to make a semaphore behave like a monitor?
- Set initial value of semaphore to 0
- A is guaranteed to wait for B to finish
- Doesnt matter if A or B run first
- Like a CV in which condition is sem.value0
- Can think of as a prefab condition variable
// Thread A s.down () // continue
// Thread B // do task s.up ()
21Prod.-cons. with semaphores
- Same before-after constraints
- If buffer empty, consumer waits for producer
- If buffer full, producer waits for consumer
- Semaphore assignments
- mutex (binary semaphore)
- fullBuffers (counts number of full slots)
- emptyBuffers (counts number of empty slots)
22Prod.-cons. with semaphores
- Initial semaphore values?
- Mutual exclusion
- sem mutex (?)
- Machine is initially empty
- sem fullBuffers (?)
- sem emptyBuffers (?)
23Prod.-cons. with semaphores
- Initial semaphore values
- Mutual exclusion
- sem mutex (1)
- Machine is initially empty
- sem fullBuffers (0)
- sem emptyBuffers (MaxSodas)
24Prod.-cons. with semaphores
Semaphore mutex(1),fullBuffers(0),emptyBuffers(Max
Sodas)
producer () down (emptyBuffers) down
(mutex) put soda in up (mutex) up
(fullBuffers)
consumer () down (fullBuffers) down
(mutex) take soda out up (mutex) up
(emptyBuffers)
Use one semaphore for fullBuffers and
emptyBuffers? Remember semaphores are like prefab
CVs.
25Prod.-cons. with semaphores
Semaphore mutex(1),fullBuffers(0),emptyBuffers(Max
Sodas)
producer () down (mutex) down
(emptyBuffers) put soda in up
(fullBuffers) up (mutex)
consumer () down (mutex) down
(fullBuffers) take soda out up
(emptyBuffers) up (mutex)
2
1
Does the order of the down calls matter? Yes. Can
cause deadlock.
26Prod.-cons. with semaphores
Semaphore mutex(1),fullBuffers(0),emptyBuffers(Max
Sodas)
producer () down (emptyBuffers) down
(mutex) put soda in up (fullBuffers) up
(mutex)
consumer () down (fullBuffers) down
(mutex) take soda out up (emptyBuffers)
up (mutex)
Does the order of the up calls matter? Not for
correctness (possible efficiency issues).
27Prod.-cons. with semaphores
Semaphore mutex(1),fullBuffers(0),emptyBuffers(Max
Sodas)
producer () down (emptyBuffers) down
(mutex) put soda in up (mutex) up
(fullBuffers)
consumer () down (fullBuffers) down
(mutex) take soda out up (mutex) up
(emptyBuffers)
What about multiple consumers and/or
producers? Doesnt matter solution stands.
28Prod.-cons. with semaphores
Semaphore mtx(1),fullBuffers(1),emptyBuffers(MaxSo
das-1)
producer () down (emptyBuffers) down
(mutex) put soda in up (mutex) up
(fullBuffers)
consumer () down (fullBuffers) down
(mutex) take soda out up (mutex) up
(emptyBuffers)
What if 1 full buffer and multiple consumers call
down? Only one will see semaphore at 1, rest see
at 0.
29Monitors vs. semaphores
- Monitors
- Separate mutual exclusion and ordering
- Semaphores
- Provide both with same mechanism
- Semaphores are more elegant
- Single mechanism
- Can be harder to program
30Monitors vs. semaphores
- Where are the conditions in both?
- Which is more flexible?
- Why do monitors need a lock, but not semaphores?
// Monitors lock (mutex) while (condition)
wait (CV, mutex) unlock (mutex)
// Semaphores down (semaphore)
31Monitors vs. semaphores
- When are semaphores appropriate?
- When shared integer maps naturally to problem at
hand - (i.e. when the condition involves a count of one
thing)
// Monitors lock (mutex) while (condition)
wait (CV, mutex) unlock (mutex)
// Semaphores down (semaphore)