summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 14:53:37 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 22:15:06 -0700
commit86063b41fc1a235930d6c79e6b7cd38ae8d8c147 (patch)
tree5c6b523a1c24062bbaba60bca1b7042b9976701c /lib
parentf4f062d7d4ed730411e04ecd36ee36387e50739c (diff)
Split lib/containers.Sync* to git.lukeshu.com/go/typedsync
Diffstat (limited to 'lib')
-rw-r--r--lib/btrfsprogs/btrfsinspect/mount.go5
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go3
-rw-r--r--lib/containers/syncmap.go53
-rw-r--r--lib/containers/syncpool.go33
-rw-r--r--lib/containers/syncvalue.go67
-rw-r--r--lib/textui/log.go5
-rw-r--r--lib/textui/progress.go5
7 files changed, 9 insertions, 162 deletions
diff --git a/lib/btrfsprogs/btrfsinspect/mount.go b/lib/btrfsprogs/btrfsinspect/mount.go
index ee3c0ec..0ac8497 100644
--- a/lib/btrfsprogs/btrfsinspect/mount.go
+++ b/lib/btrfsprogs/btrfsinspect/mount.go
@@ -14,6 +14,7 @@ import (
"sync/atomic"
"syscall"
+ "git.lukeshu.com/go/typedsync"
"github.com/datawire/dlib/dcontext"
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dlog"
@@ -109,8 +110,8 @@ type subvolume struct {
fuseutil.NotImplementedFileSystem
lastHandle uint64
- dirHandles containers.SyncMap[fuseops.HandleID, *dirState]
- fileHandles containers.SyncMap[fuseops.HandleID, *fileState]
+ dirHandles typedsync.Map[fuseops.HandleID, *dirState]
+ fileHandles typedsync.Map[fuseops.HandleID, *fileState]
subvolMu sync.Mutex
subvols containers.Set[string]
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
index ff6b1c5..45a5bb2 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
@@ -7,6 +7,7 @@ package btrees
import (
"context"
+ "git.lukeshu.com/go/typedsync"
"github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
@@ -62,7 +63,7 @@ type RebuiltForrest struct {
cbLookupUUID func(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool)
// mutable
- trees containers.SyncMap[btrfsprim.ObjID, *RebuiltTree]
+ trees typedsync.Map[btrfsprim.ObjID, *RebuiltTree]
leafs *containers.LRUCache[btrfsprim.ObjID, map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr]]
incItems *containers.LRUCache[btrfsprim.ObjID, *itemIndex]
excItems *containers.LRUCache[btrfsprim.ObjID, *itemIndex]
diff --git a/lib/containers/syncmap.go b/lib/containers/syncmap.go
deleted file mode 100644
index 74da4b3..0000000
--- a/lib/containers/syncmap.go
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package containers
-
-import (
- "sync"
-)
-
-type SyncMap[K comparable, V any] struct {
- inner sync.Map
-}
-
-func (m *SyncMap[K, V]) Delete(key K) {
- m.inner.Delete(key)
-}
-
-func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
- _value, ok := m.inner.Load(key)
- if ok {
- //nolint:forcetypeassert // Typed wrapper around untyped lib.
- value = _value.(V)
- }
- return value, ok
-}
-
-func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
- _value, ok := m.inner.LoadAndDelete(key)
- if ok {
- //nolint:forcetypeassert // Typed wrapper around untyped lib.
- value = _value.(V)
- }
- return value, ok
-}
-
-func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
- _actual, loaded := m.inner.LoadOrStore(key, value)
- //nolint:forcetypeassert // Typed wrapper around untyped lib.
- actual = _actual.(V)
- return actual, loaded
-}
-
-func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) {
- m.inner.Range(func(key, value any) bool {
- //nolint:forcetypeassert // Typed wrapper around untyped lib.
- return f(key.(K), value.(V))
- })
-}
-
-func (m *SyncMap[K, V]) Store(key K, value V) {
- m.inner.Store(key, value)
-}
diff --git a/lib/containers/syncpool.go b/lib/containers/syncpool.go
deleted file mode 100644
index cb5398d..0000000
--- a/lib/containers/syncpool.go
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package containers
-
-import (
- "sync"
-)
-
-type SyncPool[T any] struct {
- New func() T
-
- inner sync.Pool
-}
-
-func (p *SyncPool[T]) Get() (val T, ok bool) {
- _val := p.inner.Get()
- switch {
- case _val != nil:
- //nolint:forcetypeassert // Typed wrapper around untyped lib.
- return _val.(T), true
- case p.New != nil:
- return p.New(), true
- default:
- var zero T
- return zero, false
- }
-}
-
-func (p *SyncPool[T]) Put(val T) {
- p.inner.Put(val)
-}
diff --git a/lib/containers/syncvalue.go b/lib/containers/syncvalue.go
deleted file mode 100644
index 160db3c..0000000
--- a/lib/containers/syncvalue.go
+++ /dev/null
@@ -1,67 +0,0 @@
-// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package containers
-
-import (
- "sync"
-)
-
-// SyncValue is a typed equivalent of sync/atomic.Value.
-//
-// It is not actually a wrapper around sync/atomic.Value for
-// allocation-performance reasons.
-type SyncValue[T comparable] struct {
- mu sync.Mutex
- ok bool
- val T
-}
-
-// This uses a dumb mutex-based solution because
-//
-// 1. Performance is good enough, because in the fast-path mutexes
-// use the same compare-and-swap as sync/atomic.Value; and because
-// all of these methods are short we're unlikely to hit the
-// mutex's slow path.
-//
-// 2. We could use sync/atomic.Pointer[T], which by itself would have
-// the same performance characteristics as sync/atomic.Value but
-// without the benefit of runtime_procPin()/runtime_procUnpin().
-// We want to avoid that because it means we're doing an
-// allocation for every store/swap; avoiding that is our whole
-// reason for not just wraping sync/atomic.Value. So then we'd
-// want to use a SyncPool to reuse allocations; but (1) that adds
-// more sync-overhead, and (2) it also gets trickier because we'd
-// have to be careful about not adding a pointer back to the pool
-// when load has grabbed the pointer but not yet dereferenced it.
-
-func (v *SyncValue[T]) Load() (val T, ok bool) {
- v.mu.Lock()
- defer v.mu.Unlock()
- return v.val, v.ok
-}
-
-func (v *SyncValue[T]) Store(val T) {
- v.mu.Lock()
- defer v.mu.Unlock()
- v.val, v.ok = val, true
-}
-
-func (v *SyncValue[T]) Swap(newV T) (oldV T, oldOK bool) {
- v.mu.Lock()
- defer v.mu.Unlock()
- oldV, oldOK = v.val, v.ok
- v.val, v.ok = newV, true
- return
-}
-
-func (v *SyncValue[T]) CompareAndSwap(oldV, newV T) (swapped bool) {
- v.mu.Lock()
- defer v.mu.Unlock()
- if !v.ok || v.val != oldV {
- return false
- }
- v.val = newV
- return true
-}
diff --git a/lib/textui/log.go b/lib/textui/log.go
index 605755d..2bcd9af 100644
--- a/lib/textui/log.go
+++ b/lib/textui/log.go
@@ -23,10 +23,9 @@ import (
"time"
"unicode"
+ "git.lukeshu.com/go/typedsync"
"github.com/datawire/dlib/dlog"
"github.com/spf13/pflag"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/containers"
)
type LogLevelFlag struct {
@@ -154,7 +153,7 @@ func (l *logger) UnformattedLogf(lvl dlog.LogLevel, format string, args ...any)
}
var (
- logBufPool = containers.SyncPool[*bytes.Buffer]{
+ logBufPool = typedsync.Pool[*bytes.Buffer]{
New: func() *bytes.Buffer {
return new(bytes.Buffer)
},
diff --git a/lib/textui/progress.go b/lib/textui/progress.go
index 1a5e7d8..48a3901 100644
--- a/lib/textui/progress.go
+++ b/lib/textui/progress.go
@@ -9,9 +9,8 @@ import (
"fmt"
"time"
+ "git.lukeshu.com/go/typedsync"
"github.com/datawire/dlib/dlog"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/containers"
)
type Stats interface {
@@ -27,7 +26,7 @@ type Progress[T Stats] struct {
cancel context.CancelFunc
done chan struct{}
- cur containers.SyncValue[T]
+ cur typedsync.Value[T]
oldStat T
oldLine string
}