gopkg.in/dotcloud/docker.v1@v1.13.1/daemon/graphdriver/counter.go (about)

     1  package graphdriver
     2  
     3  import "sync"
     4  
     5  type minfo struct {
     6  	check bool
     7  	count int
     8  }
     9  
    10  // RefCounter is a generic counter for use by graphdriver Get/Put calls
    11  type RefCounter struct {
    12  	counts  map[string]*minfo
    13  	mu      sync.Mutex
    14  	checker Checker
    15  }
    16  
    17  // NewRefCounter returns a new RefCounter
    18  func NewRefCounter(c Checker) *RefCounter {
    19  	return &RefCounter{
    20  		checker: c,
    21  		counts:  make(map[string]*minfo),
    22  	}
    23  }
    24  
    25  // Increment increaes the ref count for the given id and returns the current count
    26  func (c *RefCounter) Increment(path string) int {
    27  	c.mu.Lock()
    28  	m := c.counts[path]
    29  	if m == nil {
    30  		m = &minfo{}
    31  		c.counts[path] = m
    32  	}
    33  	// if we are checking this path for the first time check to make sure
    34  	// if it was already mounted on the system and make sure we have a correct ref
    35  	// count if it is mounted as it is in use.
    36  	if !m.check {
    37  		m.check = true
    38  		if c.checker.IsMounted(path) {
    39  			m.count++
    40  		}
    41  	}
    42  	m.count++
    43  	c.mu.Unlock()
    44  	return m.count
    45  }
    46  
    47  // Decrement decreases the ref count for the given id and returns the current count
    48  func (c *RefCounter) Decrement(path string) int {
    49  	c.mu.Lock()
    50  	m := c.counts[path]
    51  	if m == nil {
    52  		m = &minfo{}
    53  		c.counts[path] = m
    54  	}
    55  	// if we are checking this path for the first time check to make sure
    56  	// if it was already mounted on the system and make sure we have a correct ref
    57  	// count if it is mounted as it is in use.
    58  	if !m.check {
    59  		m.check = true
    60  		if c.checker.IsMounted(path) {
    61  			m.count++
    62  		}
    63  	}
    64  	m.count--
    65  	c.mu.Unlock()
    66  	return m.count
    67  }