WriteUnlock releases the exclusive writer side of a readers–writer mutex after a writer finishes its critical section. It unlocks globalLock, allowing a blocked writer or the waiting readers to proceed. section 4.2.2
The last step out of the writer’s entrance
Imagine shared data as a room. WriteLock closes the writer’s entrance while the writer works; WriteUnlock opens that entrance again by releasing globalLock. Readers can then acquire the read side when no writer holds the lock. section 4.2.2
Listing 4.14 Implementation of the ReadUnlock() function
func (rw *ReadWriteMutex) ReadUnlock() { rw.readersLock.Lock() #1 rw.readersCounter-- #2 if rw.readersCounter == 0 { rw.globalLock.Unlock() #3 } rw.readersLock.Unlock() #4 } func (rw *ReadWriteMutex) WriteUnlock() { rw.globalLock.Unlock() #5 } #1 Synchronizes access so that only one goroutine is allowed at any time #2 The reader goroutine decrements readersCounter by 1. #3 If the reader goroutine is the last one out, it unlocks the global lock. #4 Synchronizes access so that only one goroutine is allowed at any time #5 The writer goroutine, finishing its critical section, releases the global lock.
The implementation pairs WriteUnlock with WriteLock: the writer releases globalLock after completing its critical section. section 4.2.2
