github.com/ojongerius/docker@v1.11.2/daemon/graphdriver/counter.go (about)

     1  package graphdriver
     2  
     3  import "sync"
     4  
     5  // RefCounter is a generic counter for use by graphdriver Get/Put calls
     6  type RefCounter struct {
     7  	counts map[string]int
     8  	mu     sync.Mutex
     9  }
    10  
    11  // NewRefCounter returns a new RefCounter
    12  func NewRefCounter() *RefCounter {
    13  	return &RefCounter{counts: make(map[string]int)}
    14  }
    15  
    16  // Increment increaes the ref count for the given id and returns the current count
    17  func (c *RefCounter) Increment(id string) int {
    18  	c.mu.Lock()
    19  	c.counts[id]++
    20  	count := c.counts[id]
    21  	c.mu.Unlock()
    22  	return count
    23  }
    24  
    25  // Decrement decreases the ref count for the given id and returns the current count
    26  func (c *RefCounter) Decrement(id string) int {
    27  	c.mu.Lock()
    28  	c.counts[id]--
    29  	count := c.counts[id]
    30  	c.mu.Unlock()
    31  	return count
    32  }