github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/cache/refcnt_normal.go (about) 1 // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 //go:build !tracing 6 // +build !tracing 7 8 package cache 9 10 import ( 11 "fmt" 12 "sync/atomic" 13 14 "github.com/cockroachdb/redact" 15 ) 16 17 // refcnt provides an atomic reference count. This version is used when the 18 // "tracing" build tag is not enabled. See refcnt_tracing.go for the "tracing" 19 // enabled version. 20 type refcnt struct { 21 val atomic.Int32 22 } 23 24 // initialize the reference count to the specified value. 25 func (v *refcnt) init(val int32) { 26 v.val.Store(val) 27 } 28 29 func (v *refcnt) refs() int32 { 30 return v.val.Load() 31 } 32 33 func (v *refcnt) acquire() { 34 switch v := v.val.Add(1); { 35 case v <= 1: 36 panic(redact.Safe(fmt.Sprintf("pebble: inconsistent reference count: %d", v))) 37 } 38 } 39 40 func (v *refcnt) release() bool { 41 switch v := v.val.Add(-1); { 42 case v < 0: 43 panic(redact.Safe(fmt.Sprintf("pebble: inconsistent reference count: %d", v))) 44 case v == 0: 45 return true 46 default: 47 return false 48 } 49 } 50 51 func (v *refcnt) trace(msg string) { 52 } 53 54 func (v *refcnt) traces() string { 55 return "" 56 } 57 58 // Silence unused warning. 59 var _ = (*refcnt)(nil).traces