github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/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  
    15  // refcnt provides an atomic reference count. This version is used when the
    16  // "tracing" build tag is not enabled. See refcnt_tracing.go for the "tracing"
    17  // enabled version.
    18  type refcnt int32
    19  
    20  // initialize the reference count to the specified value.
    21  func (v *refcnt) init(val int32) {
    22  	*v = refcnt(val)
    23  }
    24  
    25  func (v *refcnt) refs() int32 {
    26  	return atomic.LoadInt32((*int32)(v))
    27  }
    28  
    29  func (v *refcnt) acquire() {
    30  	switch v := atomic.AddInt32((*int32)(v), 1); {
    31  	case v <= 1:
    32  		panic(fmt.Sprintf("bitalostable: inconsistent reference count: %d", v))
    33  	}
    34  }
    35  
    36  func (v *refcnt) release() bool {
    37  	switch v := atomic.AddInt32((*int32)(v), -1); {
    38  	case v < 0:
    39  		panic(fmt.Sprintf("bitalostable: inconsistent reference count: %d", v))
    40  	case v == 0:
    41  		return true
    42  	default:
    43  		return false
    44  	}
    45  }
    46  
    47  func (v *refcnt) trace(msg string) {
    48  }
    49  
    50  func (v *refcnt) traces() string {
    51  	return ""
    52  }
    53  
    54  // Silence unused warning.
    55  var _ = (*refcnt)(nil).traces