Skip to content

Commit

Permalink
feat(sync): add RWMutex. TryBest
Browse files Browse the repository at this point in the history
Change-Id: I1aa412ddab4748dfa232db808a906bab632e9206
  • Loading branch information
andeya committed Jan 9, 2023
1 parent 4e384c3 commit 425a013
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ func (m *RWMutex[T]) RLockScope(read func(T)) {
read(m.data)
}

// TryBest tries to read and do the data in the RWMutex[T] safely,
// swapping the data when readAndDo returns false and then trying to do again.
func (m *RWMutex[T]) TryBest(readAndDo func(T) bool, swapWhenFalse func(old T) (new Option[T])) {
if readAndDo == nil {
return
}
var ok bool
m.RLockScope(func(old T) {
ok = readAndDo(old)
})
if ok || swapWhenFalse == nil {
return
}
m.inner.Lock()
defer m.inner.Unlock()
if readAndDo(m.data) {
return
}
swapWhenFalse(m.data).Inspect(func(newT T) {
m.data = newT
readAndDo(newT)
})
}

// SyncMap is a better generic-type wrapper for `sync.Map`.
// A SyncMap is like a Go map[interface{}]interface{} but is safe for concurrent use
// by multiple goroutines without additional locking or coordination.
Expand Down

0 comments on commit 425a013

Please sign in to comment.