Skip to content

sync: add atomic UnlockAndRLock to RWMutex #4026

@dsymonds

Description

@dsymonds
A couple of times I've run into a situation where I have RLocked an RWMutex and then
need to Lock it to modify the thing it is protecting, and then want to go back to an
RLock to use it. A typical example is grabbing the RLock to examine cached data, then
noting it is stale and then needing to Lock to update it.

Unfortunately the obvious approach (RUnlock + Lock, then modify, then Unlock + RLock) is
racy because the condition that caused you to promote to a write lock may have changed
between the Unlock and the RLock. This necessitates a loop structure.

If there were Promote and Demote methods then that shuffle could be avoided. The
semantics are:
  - Promote is equivalent to mu.Lock() with a concurrent mu.RUnlock(), where the Lock happens-before the RUnlock.
  - Demote is equivalent to mu.RLock() with a concurrent mu.Unlock(), where the RLock happens-before the Unlock.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions