Skip to content

Commit

Permalink
chore(sync): improved
Browse files Browse the repository at this point in the history
Change-Id: Ia96d0e564c448c58b3a2bde71c141b2fe8d4a3f2
  • Loading branch information
andeya committed Jan 9, 2023
1 parent 032a2e3 commit 4e384c3
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ func (m *Mutex[T]) Unlock(newData ...T) {
m.inner.Unlock()
}

// LockScope securely read and write the data in the mutex.
func (m *Mutex[T]) LockScope(f func(T) T) {
// LockScope securely read and write the data in the Mutex[T].
func (m *Mutex[T]) LockScope(f func(old T) (new T)) {
m.inner.Lock()
defer m.inner.Unlock()
m.data = f(m.data)
}

// TryLockScope tries to securely read and write the data in the mutex.
func (m *Mutex[T]) TryLockScope(f func(T) T) {
// TryLockScope tries to securely read and write the data in the Mutex[T].
func (m *Mutex[T]) TryLockScope(f func(old T) (new T)) {
if m.inner.TryLock() {
defer m.inner.Unlock()
m.data = f(m.data)
Expand Down Expand Up @@ -138,19 +138,19 @@ func (m *RWMutex[T]) Unlock(newData ...T) {
m.inner.Unlock()
}

// TryLockScope tries to securely read and write the data in the mutex.
func (m *RWMutex[T]) TryLockScope(f func(T) T) {
// TryLockScope tries to securely read and write the data in the RWMutex[T].
func (m *RWMutex[T]) TryLockScope(write func(old T) (new T)) {
if m.inner.TryLock() {
defer m.inner.Unlock()
m.data = f(m.data)
m.data = write(m.data)
}
}

// LockScope securely read and write the data in the rwmutex.
func (m *RWMutex[T]) LockScope(f func(T) T) {
// LockScope securely read and write the data in the RWMutex[T].
func (m *RWMutex[T]) LockScope(write func(old T) (new T)) {
m.inner.Lock()
defer m.inner.Unlock()
m.data = f(m.data)
m.data = write(m.data)
}

// Happens-before relationships are indicated to the race detector via:
Expand Down Expand Up @@ -196,19 +196,19 @@ func (m *RWMutex[T]) RUnlock() {
m.inner.RUnlock()
}

// TryRLockScope tries to securely read the data in the mutex.
func (m *RWMutex[T]) TryRLockScope(f func(T) T) {
// TryRLockScope tries to securely read the data in the RWMutex[T].
func (m *RWMutex[T]) TryRLockScope(read func(T)) {
if m.inner.TryRLock() {
defer m.inner.RUnlock()
m.data = f(m.data)
read(m.data)
}
}

// RLockScope securely read the data in the rwmutex.
func (m *RWMutex[T]) RLockScope(f func(T) T) {
// RLockScope securely read the data in the RWMutex[T].
func (m *RWMutex[T]) RLockScope(read func(T)) {
m.inner.RLock()
defer m.inner.RUnlock()
m.data = f(m.data)
read(m.data)
}

// SyncMap is a better generic-type wrapper for `sync.Map`.
Expand Down

0 comments on commit 4e384c3

Please sign in to comment.