github.com/m3db/m3@v1.5.0/src/x/checked/types.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package checked implements reference counted resources.
    22  package checked
    23  
    24  import (
    25  	xresource "github.com/m3db/m3/src/x/resource"
    26  )
    27  
    28  // Ref is an entity that checks ref counts.
    29  type Ref interface {
    30  	// IncRef increments the ref count to this entity.
    31  	IncRef()
    32  
    33  	// DecRef decrements the ref count to this entity.
    34  	DecRef()
    35  
    36  	// MoveRef signals a move of the ref to this entity.
    37  	MoveRef()
    38  
    39  	// NumRef returns the ref count to this entity.
    40  	NumRef() int
    41  
    42  	// DelayFinalizer will delay calling the finalizer on this entity
    43  	// until the closer returned by the method is called at least once.
    44  	// This is useful for dependent resources requiring the lifetime of this
    45  	// entityt to be extended.
    46  	DelayFinalizer() xresource.SimpleCloser
    47  
    48  	// Finalize will call the finalizer if any, ref count must be zero.
    49  	Finalize()
    50  }
    51  
    52  // OnFinalize is callback to cleanup resources on a call to finalize.
    53  type OnFinalize interface {
    54  	OnFinalize()
    55  }
    56  
    57  // OnFinalizeFn is a function literal that is a finalizer callback.
    58  type OnFinalizeFn func()
    59  
    60  // OnFinalize will call the function literal as a finalizer callback.
    61  func (fn OnFinalizeFn) OnFinalize() {
    62  	fn()
    63  }
    64  
    65  // Read is an entity that checks reads.
    66  type Read interface {
    67  	// IncReads increments the reads count to this entity.
    68  	IncReads()
    69  
    70  	// DecReads decrements the reads count to this entity.
    71  	DecReads()
    72  
    73  	// NumReaders returns the active reads count to this entity.
    74  	NumReaders() int
    75  }
    76  
    77  // Write is an entity that checks writes.
    78  type Write interface {
    79  	// IncWrites increments the writes count to this entity.
    80  	IncWrites()
    81  
    82  	// DecWrites decrements the writes count to this entity.
    83  	DecWrites()
    84  
    85  	// NumWriters returns the active writes count to this entity.
    86  	NumWriters() int
    87  }
    88  
    89  // ReadWriteRef is an entity that checks ref counts, reads and writes.
    90  type ReadWriteRef interface {
    91  	Ref
    92  	Read
    93  	Write
    94  }
    95  
    96  // BytesFinalizer finalizes a checked byte slice.
    97  type BytesFinalizer interface {
    98  	FinalizeBytes(b Bytes)
    99  }
   100  
   101  // BytesFinalizerFn is a function literal that is a bytes finalizer.
   102  type BytesFinalizerFn func(b Bytes)
   103  
   104  // FinalizeBytes will call the function literal as a bytes finalizer.
   105  func (fn BytesFinalizerFn) FinalizeBytes(b Bytes) {
   106  	fn(b)
   107  }
   108  
   109  // BytesOptions is a bytes option
   110  type BytesOptions interface {
   111  	// Finalizer is a bytes finalizer to call when finalized.
   112  	Finalizer() BytesFinalizer
   113  
   114  	// SetFinalizer sets a bytes finalizer to call when finalized.
   115  	SetFinalizer(value BytesFinalizer) BytesOptions
   116  }