github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/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 count := m.count 44 c.mu.Unlock() 45 return count 46 } 47 48 // Decrement decreases the ref count for the given id and returns the current count 49 func (c *RefCounter) Decrement(path string) int { 50 c.mu.Lock() 51 m := c.counts[path] 52 if m == nil { 53 m = &minfo{} 54 c.counts[path] = m 55 } 56 // if we are checking this path for the first time check to make sure 57 // if it was already mounted on the system and make sure we have a correct ref 58 // count if it is mounted as it is in use. 59 if !m.check { 60 m.check = true 61 if c.checker.IsMounted(path) { 62 m.count++ 63 } 64 } 65 m.count-- 66 count := m.count 67 c.mu.Unlock() 68 return count 69 }