2018-02-05 16:05:59 -05:00
|
|
|
package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
|
2016-04-18 19:41:29 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-16 07:08:57 -05:00
|
|
|
// Increment increases 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 {
|
2017-02-28 05:12:02 -05:00
|
|
|
return c.incdec(path, func(minfo *minfo) {
|
|
|
|
minfo.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 {
|
2017-02-28 05:12:02 -05:00
|
|
|
return c.incdec(path, func(minfo *minfo) {
|
|
|
|
minfo.count--
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) 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++
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 05:12:02 -05:00
|
|
|
infoOp(m)
|
2017-02-03 11:47:55 -05:00
|
|
|
count := m.count
|
2018-02-08 02:44:20 -05:00
|
|
|
if count <= 0 {
|
|
|
|
delete(c.counts, path)
|
|
|
|
}
|
2016-04-18 19:41:29 -04:00
|
|
|
c.mu.Unlock()
|
2017-02-03 11:47:55 -05:00
|
|
|
return count
|
2017-02-28 05:12:06 -05:00
|
|
|
}
|