inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/refsvfs2/refs.go (about)

     1  // Copyright 2020 The gVisor Authors.
     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  // Package refsvfs2 defines an interface for a reference-counted object.
    16  package refsvfs2
    17  
    18  import (
    19  	"inet.af/netstack/context"
    20  )
    21  
    22  // RefCounter is the interface to be implemented by objects that are reference
    23  // counted.
    24  type RefCounter interface {
    25  	// IncRef increments the reference counter on the object.
    26  	IncRef()
    27  
    28  	// DecRef decrements the object's reference count. Users of refs_template.Refs
    29  	// may specify a destructor to be called once the reference count reaches zero.
    30  	DecRef(ctx context.Context)
    31  }
    32  
    33  // TryRefCounter is like RefCounter but allow the ref increment to be tried.
    34  type TryRefCounter interface {
    35  	RefCounter
    36  
    37  	// TryIncRef attempts to increment the reference count, but may fail if all
    38  	// references have already been dropped, in which case it returns false. If
    39  	// true is returned, then a valid reference is now held on the object.
    40  	TryIncRef() bool
    41  }