RwMutex.readUnlock
func RwMutex.readUnlock(m: RwMutex): voidRwMutex.readUnlock releases one read lock on m. It panics if m is not read-locked.
Details
- Read locks count: every RwMutex.readLock needs its own
readUnlock, and a writer waits until the last reader has released m. - It panics when no reader holds m, which includes when m is locked for writing; release a write lock with RwMutex.writeUnlock.
- Call it in a
deferright after the read lock, so that areturnor a panic still releases m. RwMutex.withRead does both in one call.
Basic examples
Lock for reading, read, then release the lock:
let m = RwMutex.new()
let scores = Map{ "ann": 3, "bob": 5 }
RwMutex.readLock(m)
let ann = scores["ann"]
RwMutex.readUnlock(m)
println(ann)3Release the lock in a defer right after taking it, so every way out of the function releases it:
type Scores = { lock: RwMutex, byName: Map[string, int] }
func Scores.get(s: Scores, name: string): int {
s.lock.readLock()
defer { s.lock.readUnlock() }
s.byName[name] ?? 0
}
let scores = Scores{ lock: RwMutex.new(), byName: Map{ "ann": 3 } }
println(scores.get("ann"))
println(scores.get("cy"))3
0Scope
Several read locks
Each read lock is released on its own. While one remains, m is still read-locked:
let m = RwMutex.new()
m.readLock()
m.readLock()
m.readUnlock()
m.readUnlock()
m.writeLock()
println("no readers left")
m.writeUnlock()no readers leftA waiting writer
A writer gets m only after the reader releases its lock:
let m = RwMutex.new()
let done: Channel[bool] = Channel.new()
m.readLock()
go {
m.writeLock()
println("writer has the lock")
m.writeUnlock()
Channel.send(done, true)
}
println("reader releases the lock")
m.readUnlock()
Channel.receive(done)reader releases the lock
writer has the lockFrom many goroutines
Readers run side by side, and each releases its own lock:
let m = RwMutex.new()
let prices = Map{ "tea": 4, "cake": 6 }
let items = ["tea", "cake", "tea", "jam"]
let found = Parallel.map(items, func(item: string, _: Context): int {
m.readLock()
defer { m.readUnlock() }
prices[item] ?? 0
}, limit: 4)
println(found)[4, 6, 4, 0]Applications
A read-mostly cache
Lookups share the lock for reading; a store takes it for writing. Cache.find releases its read lock before Cache.store takes the write lock:
type Cache = { lock: RwMutex, entries: Map[string, string] }
func Cache.find(c: Cache, key: string): string | null {
c.lock.readLock()
defer { c.lock.readUnlock() }
c.entries[key]
}
func Cache.store(c: Cache, key: string, value: string) {
c.lock.writeLock()
defer { c.lock.writeUnlock() }
c.entries[key] = value
}
func Cache.load(c: Cache, key: string, compute: func(string): string): string {
let cached = c.find(key)
if cached != null { return cached }
let value = compute(key)
c.store(key, value)
value
}
let cache = Cache{ lock: RwMutex.new(), entries: Map{} }
println(cache.load("ant", { x.toUpper() }))
println(cache.find("ant"))ANT
ANTProperties and relations
A read lock released in a defer is what RwMutex.withRead does:
let m = RwMutex.new()
let xs = [1, 2, 3]
func countLocked(): int {
m.readLock()
defer { m.readUnlock() }
xs.length()
}
assert(countLocked() == m.withRead({ xs.length() }))After as many unlocks as locks, m is unlocked, and one more unlock panics:
let m = RwMutex.new()
for _ in Iter.range(0, 3) {
m.readLock()
}
for _ in Iter.range(0, 3) {
m.readUnlock()
}
assert(Panic.catch({ m.readUnlock() }) is Err)Possible issues
Unlocking a lock you do not hold
A readUnlock with no read lock to release panics:
let m = RwMutex.new()
m.readUnlock() // panic: RwMutex.readUnlock: the RwMutex is not read-lockedReleasing a write lock
readUnlock does not release a write lock; RwMutex.writeUnlock does:
let m = RwMutex.new()
m.writeLock()
m.readUnlock() // panic: RwMutex.readUnlock: the RwMutex is not read-lockedTaking the write lock before the read unlock
A write lock waits for every reader, including the goroutine that asks for it. Release the read lock with readUnlock before you call RwMutex.writeLock:
let m = RwMutex.new()
let done: Channel[bool] = Channel.new()
go {
m.readLock()
m.writeLock()
m.writeUnlock()
m.readUnlock()
Channel.send(done, true)
}
println(Channel.receive(done, Context.new(timeout: 100ms)))nullA path that skips the unlock
A return before a readUnlock that is not deferred leaves m read-locked, and every writer waits for it forever. Here the writer is still waiting when a bounded receive gives up:
let m = RwMutex.new()
let names = ["ann"]
func has(name: string): bool {
m.readLock()
if names.contains(name) { return true }
m.readUnlock()
false
}
println(has("ann"))
let written: Channel[bool] = Channel.new()
go {
m.writeLock()
Channel.send(written, true)
}
println(Channel.receive(written, Context.new(timeout: 100ms)))true
nullA panic skips it in the same way. Put the unlock in a defer, whose block runs on a return and on a panic alike:
let m = RwMutex.new()
func risky() {
m.readLock()
defer { m.readUnlock() }
panic("bad input")
}
println(Panic.catch(risky) is Err)
m.writeLock()
println("the read lock was released")
m.writeUnlock()true
the read lock was releasedSee also
RwMutex.readLock · RwMutex.withRead · RwMutex.writeUnlock · RwMutex.writeLock · RwMutex.new · Mutex.unlock · RwMutex
