2016-04-18 19:41:29 -04:00
|
|
|
package graphdriver
|
|
|
|
|
2016-05-06 15:04:26 -04:00
|
|
|
import "sync"
|
2016-05-02 18:44:20 -04:00
|
|
|
|
|
|
|
type minfo struct {
|
|
|
|
check bool
|
|
|
|
count int
|
|
|
|
}
|
2016-04-18 19:41:29 -04:00
|
|
|
|
|
|
|
// RefCounter is a generic counter for use by graphdriver Get/Put calls
|
|
|
|
type RefCounter struct {
|
2016-05-06 15:04:26 -04:00
|
|
|
counts map[string]*minfo
|
|
|
|
mu sync.Mutex
|
|
|
|
checker Checker
|
2016-04-18 19:41:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRefCounter returns a new RefCounter
|
2016-05-06 15:04:26 -04:00
|
|
|
func NewRefCounter(c Checker) *RefCounter {
|
|
|
|
return &RefCounter{
|
|
|
|
checker: c,
|
|
|
|
counts: make(map[string]*minfo),
|
|
|
|
}
|
2016-04-18 19:41:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment increaes the ref count for the given id and returns the current count
|
2016-05-02 18:44:20 -04:00
|
|
|
func (c *RefCounter) Increment(path string) int {
|
2016-04-18 19:41:29 -04:00
|
|
|
c.mu.Lock()
|
2016-05-02 18:44:20 -04:00
|
|
|
m := c.counts[path]
|
|
|
|
if m == nil {
|
2016-05-02 20:22:11 -04:00
|
|
|
m = &minfo{}
|
2016-05-02 18:44:20 -04:00
|
|
|
c.counts[path] = m
|
|
|
|
}
|
|
|
|
// if we are checking this path for the first time check to make sure
|
|
|
|
// if it was already mounted on the system and make sure we have a correct ref
|
|
|
|
// count if it is mounted as it is in use.
|
|
|
|
if !m.check {
|
|
|
|
m.check = true
|
2016-05-06 15:04:26 -04:00
|
|
|
if c.checker.IsMounted(path) {
|
2016-05-02 18:44:20 -04:00
|
|
|
m.count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.count++
|
2016-04-18 19:41:29 -04:00
|
|
|
c.mu.Unlock()
|
2016-05-02 18:44:20 -04:00
|
|
|
return m.count
|
2016-04-18 19:41:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement decreases the ref count for the given id and returns the current count
|
2016-05-02 18:44:20 -04:00
|
|
|
func (c *RefCounter) Decrement(path string) int {
|
2016-04-18 19:41:29 -04:00
|
|
|
c.mu.Lock()
|
2016-05-02 18:44:20 -04:00
|
|
|
m := c.counts[path]
|
|
|
|
if m == nil {
|
2016-05-02 20:22:11 -04:00
|
|
|
m = &minfo{}
|
2016-05-02 18:44:20 -04:00
|
|
|
c.counts[path] = m
|
|
|
|
}
|
|
|
|
// if we are checking this path for the first time check to make sure
|
|
|
|
// if it was already mounted on the system and make sure we have a correct ref
|
|
|
|
// count if it is mounted as it is in use.
|
|
|
|
if !m.check {
|
|
|
|
m.check = true
|
2016-05-06 15:04:26 -04:00
|
|
|
if c.checker.IsMounted(path) {
|
2016-05-02 18:44:20 -04:00
|
|
|
m.count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.count--
|
2016-04-18 19:41:29 -04:00
|
|
|
c.mu.Unlock()
|
2016-05-02 18:44:20 -04:00
|
|
|
return m.count
|
2016-04-18 19:41:29 -04:00
|
|
|
}
|