github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/cache/lrucache/refcnt_normal.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  	"sync/atomic"
    23  )
    24  
    25  type refcnt int32
    26  
    27  func (v *refcnt) init(val int32) {
    28  	*v = refcnt(val)
    29  }
    30  
    31  func (v *refcnt) refs() int32 {
    32  	return atomic.LoadInt32((*int32)(v))
    33  }
    34  
    35  func (v *refcnt) acquire() {
    36  	switch v := atomic.AddInt32((*int32)(v), 1); {
    37  	case v <= 1:
    38  		panic(fmt.Sprintf("cache: inconsistent reference count: %d", v))
    39  	}
    40  }
    41  
    42  func (v *refcnt) release() bool {
    43  	switch v := atomic.AddInt32((*int32)(v), -1); {
    44  	case v < 0:
    45  		panic(fmt.Sprintf("cache: inconsistent reference count: %d", v))
    46  	case v == 0:
    47  		return true
    48  	default:
    49  		return false
    50  	}
    51  }
    52  
    53  func (v *refcnt) trace(msg string) {
    54  }
    55  
    56  func (v *refcnt) traces() string {
    57  	return ""
    58  }
    59  
    60  var _ = (*refcnt)(nil).traces