summaryrefslogtreecommitdiff
path: root/lib/containers/syncpool.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-05 19:20:25 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-25 17:28:49 -0700
commit774689062b4ac1921434a6c7a2ac78b8f29ac85a (patch)
tree1957c0d184096af1b7d0a8ed489820c10a48466f /lib/containers/syncpool.go
parent55823533d19dad96df909a3a46eef341c63f4cd4 (diff)
containers: Add SyncValue and SyncPool types
Diffstat (limited to 'lib/containers/syncpool.go')
-rw-r--r--lib/containers/syncpool.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/containers/syncpool.go b/lib/containers/syncpool.go
new file mode 100644
index 0000000..cb5398d
--- /dev/null
+++ b/lib/containers/syncpool.go
@@ -0,0 +1,33 @@
+// 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)
+}