summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-structr/cache.go
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-08-21 16:41:50 +0200
committerLibravatar kim <gruf@noreply.codeberg.org>2025-08-21 16:41:50 +0200
commita79f83cbded3fb1165b338ce316dc9f2217467ff (patch)
tree4b4a2b7084aced51c0a691cee6352b4ecae90df8 /vendor/codeberg.org/gruf/go-structr/cache.go
parent[chore] bump golangci-lint version in CI, disable var-naming package name che... (diff)
downloadgotosocial-a79f83cbded3fb1165b338ce316dc9f2217467ff.tar.xz
[chore] update dependencies (#4386)
- codeberg.org/gruf/go-bytesize v1.0.3 -> v1.0.4 - codeberg.org/gruf/go-kv/v2 v2.0.6 -> v2.0.7 - codeberg.org/gruf/go-mutexes v1.5.2 -> v1.5.3 - codeberg.org/gruf/go-structr v0.9.7 -> v0.9.8 - codeberg.org/gruf/go-ffmpreg v0.6.8 -> v0.6.9 - github.com/tomnomnom/linkheader HEAD@2018 -> HEAD@2025 all of the above codeberg.org/gruf updates are in preparation for Go1.25, except for bytesize, and also ffmpreg which is a rebuild with the latest version of ffmpeg (v5.1.7) Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4386 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-structr/cache.go')
-rw-r--r--vendor/codeberg.org/gruf/go-structr/cache.go60
1 files changed, 39 insertions, 21 deletions
diff --git a/vendor/codeberg.org/gruf/go-structr/cache.go b/vendor/codeberg.org/gruf/go-structr/cache.go
index 4b32d6821..6ae6ff3ed 100644
--- a/vendor/codeberg.org/gruf/go-structr/cache.go
+++ b/vendor/codeberg.org/gruf/go-structr/cache.go
@@ -3,7 +3,6 @@ package structr
import (
"context"
"errors"
- "reflect"
"sync"
"unsafe"
)
@@ -83,7 +82,7 @@ type Cache[StructType any] struct {
// Init initializes the cache with given configuration
// including struct fields to index, and necessary fns.
func (c *Cache[T]) Init(config CacheConfig[T]) {
- t := reflect.TypeOf((*T)(nil)).Elem()
+ t := get_type_iter[T]()
if len(config.Indices) == 0 {
panic("no indices provided")
@@ -182,9 +181,14 @@ func (c *Cache[T]) Put(values ...T) {
// Acquire lock.
c.mutex.Lock()
- // Wrap unlock to only do once.
- unlock := once(c.mutex.Unlock)
- defer unlock()
+ // Ensure mutex
+ // gets unlocked.
+ var unlocked bool
+ defer func() {
+ if !unlocked {
+ c.mutex.Unlock()
+ }
+ }()
// Check cache init.
if c.copy == nil {
@@ -202,9 +206,9 @@ func (c *Cache[T]) Put(values ...T) {
// Get func ptrs.
invalid := c.invalid
- // Done with
- // the lock.
- unlock()
+ // Done with lock.
+ c.mutex.Unlock()
+ unlocked = true
if invalid != nil {
// Pass all invalidated values
@@ -241,9 +245,14 @@ func (c *Cache[T]) LoadOne(index *Index, key Key, load func() (T, error)) (T, er
// Acquire lock.
c.mutex.Lock()
- // Wrap unlock to only do once.
- unlock := once(c.mutex.Unlock)
- defer unlock()
+ // Ensure mutex
+ // gets unlocked.
+ var unlocked bool
+ defer func() {
+ if !unlocked {
+ c.mutex.Unlock()
+ }
+ }()
// Check init'd.
if c.copy == nil ||
@@ -276,9 +285,9 @@ func (c *Cache[T]) LoadOne(index *Index, key Key, load func() (T, error)) (T, er
// Get func ptrs.
ignore := c.ignore
- // Done with
- // the lock.
- unlock()
+ // Done with lock.
+ c.mutex.Unlock()
+ unlocked = true
if ok {
// item found!
@@ -295,6 +304,7 @@ func (c *Cache[T]) LoadOne(index *Index, key Key, load func() (T, error)) (T, er
// Acquire lock.
c.mutex.Lock()
+ unlocked = false
// Index this new loaded item.
// Note this handles copying of
@@ -308,6 +318,7 @@ func (c *Cache[T]) LoadOne(index *Index, key Key, load func() (T, error)) (T, er
// Done with lock.
c.mutex.Unlock()
+ unlocked = true
return val, err
}
@@ -328,9 +339,14 @@ func (c *Cache[T]) Load(index *Index, keys []Key, load func([]Key) ([]T, error))
// Acquire lock.
c.mutex.Lock()
- // Wrap unlock to only do once.
- unlock := once(c.mutex.Unlock)
- defer unlock()
+ // Ensure mutex
+ // gets unlocked.
+ var unlocked bool
+ defer func() {
+ if !unlocked {
+ c.mutex.Unlock()
+ }
+ }()
// Check init'd.
if c.copy == nil {
@@ -366,9 +382,9 @@ func (c *Cache[T]) Load(index *Index, keys []Key, load func([]Key) ([]T, error))
}
}
- // Done with
- // the lock.
- unlock()
+ // Done with lock.
+ c.mutex.Unlock()
+ unlocked = true
if len(toLoad) == 0 {
// We loaded everything!
@@ -383,6 +399,7 @@ func (c *Cache[T]) Load(index *Index, keys []Key, load func([]Key) ([]T, error))
// Acquire lock.
c.mutex.Lock()
+ unlocked = false
// Store all uncached values.
for i := range uncached {
@@ -394,6 +411,7 @@ func (c *Cache[T]) Load(index *Index, keys []Key, load func([]Key) ([]T, error))
// Done with lock.
c.mutex.Unlock()
+ unlocked = true
// Append uncached to return values.
values = append(values, uncached...)
@@ -685,7 +703,7 @@ func (c *Cache[T]) store_error(index *Index, key string, err error) {
}
func (c *Cache[T]) delete(i *indexed_item) {
- for len(i.indexed) != 0 {
+ for len(i.indexed) > 0 {
// Pop last indexed entry from list.
entry := i.indexed[len(i.indexed)-1]
i.indexed[len(i.indexed)-1] = nil