summaryrefslogtreecommitdiff
path: root/typedsync
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-05 12:06:30 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-05 13:32:56 -0700
commit2d939c9c6e62395ed924fe7c5cd4c4b294e391a9 (patch)
treef292beebe17f48a56550bea1435808b965ce6764 /typedsync
parentd69037701f6cdd4f5bb98c20af329c02ba89bb90 (diff)
Rename to git.lukeshu.com/go/containers, split in to 2 separate packages
Diffstat (limited to 'typedsync')
-rw-r--r--typedsync/map.go69
-rw-r--r--typedsync/map_go118.go9
-rw-r--r--typedsync/map_go120.go9
-rw-r--r--typedsync/pool.go33
-rw-r--r--typedsync/value.go67
5 files changed, 187 insertions, 0 deletions
diff --git a/typedsync/map.go b/typedsync/map.go
new file mode 100644
index 0000000..6bb1170
--- /dev/null
+++ b/typedsync/map.go
@@ -0,0 +1,69 @@
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package typedsync
+
+import (
+ "sync"
+)
+
+// Map is a type-safe equivalent of the standard library's sync.Map.
+//
+// With versions of Go prior to Go 1.20, Map is specified too loosely,
+// as
+//
+// Map[K any, V any]
+//
+// while with Go 1.20 and later, Map is specified as
+//
+// Map[K comparable, V any]
+//
+// This is because with Go versions prior to 1.20, 'comparable' was
+// overly strict, disallowing many types that are valid map-keys (see
+// https://github.com/golang/go/issues/56548). The type used as K in
+// a Map older versions of Go must be a valid map-key type, even
+// though the type specification of Map does not enforce that.
+type Map[K mapkey, V any] struct {
+ inner sync.Map
+}
+
+func (m *Map[K, V]) Delete(key K) {
+ m.inner.Delete(key)
+}
+
+func (m *Map[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 *Map[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 *Map[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 *Map[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 *Map[K, V]) Store(key K, value V) {
+ m.inner.Store(key, value)
+}
diff --git a/typedsync/map_go118.go b/typedsync/map_go118.go
new file mode 100644
index 0000000..5446c88
--- /dev/null
+++ b/typedsync/map_go118.go
@@ -0,0 +1,9 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+//go:build !go1.20
+
+package typedsync
+
+type mapkey = any
diff --git a/typedsync/map_go120.go b/typedsync/map_go120.go
new file mode 100644
index 0000000..0d4ff5b
--- /dev/null
+++ b/typedsync/map_go120.go
@@ -0,0 +1,9 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+//go:build go1.20
+
+package typedsync
+
+type mapkey = comparable
diff --git a/typedsync/pool.go b/typedsync/pool.go
new file mode 100644
index 0000000..c196085
--- /dev/null
+++ b/typedsync/pool.go
@@ -0,0 +1,33 @@
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package typedsync
+
+import (
+ "sync"
+)
+
+type Pool[T any] struct {
+ New func() T
+
+ inner sync.Pool
+}
+
+func (p *Pool[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 *Pool[T]) Put(val T) {
+ p.inner.Put(val)
+}
diff --git a/typedsync/value.go b/typedsync/value.go
new file mode 100644
index 0000000..99c8876
--- /dev/null
+++ b/typedsync/value.go
@@ -0,0 +1,67 @@
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package typedsync
+
+import (
+ "sync"
+)
+
+// Value is a typed equivalent of sync/atomic.Value.
+//
+// It is not actually a wrapper around sync/atomic.Value for
+// allocation-performance reasons.
+type Value[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 Pool 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 *Value[T]) Load() (val T, ok bool) {
+ v.mu.Lock()
+ defer v.mu.Unlock()
+ return v.val, v.ok
+}
+
+func (v *Value[T]) Store(val T) {
+ v.mu.Lock()
+ defer v.mu.Unlock()
+ v.val, v.ok = val, true
+}
+
+func (v *Value[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 *Value[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
+}