github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/cache/lrucache/refcnt_tracing.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build tracing 16 // +build tracing 17 18 package lrucache 19 20 import ( 21 "fmt" 22 "runtime/debug" 23 "strings" 24 "sync" 25 "sync/atomic" 26 ) 27 28 type refcnt struct { 29 val int32 30 sync.Mutex 31 msgs []string 32 } 33 34 func (v *refcnt) init(val int32) { 35 v.val = val 36 v.trace("init") 37 } 38 39 func (v *refcnt) refs() int32 { 40 return atomic.LoadInt32(&v.val) 41 } 42 43 func (v *refcnt) acquire() { 44 switch n := atomic.AddInt32(&v.val, 1); { 45 case n <= 1: 46 panic(fmt.Sprintf("cache: inconsistent reference count: %d", n)) 47 } 48 v.trace("acquire") 49 } 50 51 func (v *refcnt) release() bool { 52 n := atomic.AddInt32(&v.val, -1) 53 switch { 54 case n < 0: 55 panic(fmt.Sprintf("cache: inconsistent reference count: %d", n)) 56 } 57 v.trace("release") 58 return n == 0 59 } 60 61 func (v *refcnt) trace(msg string) { 62 s := fmt.Sprintf("%s: refs=%d\n%s", msg, v.refs(), debug.Stack()) 63 v.Lock() 64 v.msgs = append(v.msgs, s) 65 v.Unlock() 66 } 67 68 func (v *refcnt) traces() string { 69 v.Lock() 70 s := strings.Join(v.msgs, "\n") 71 v.Unlock() 72 return s 73 }