6c9628cdb1
* Rename 'vendor/src' -> 'vendor' * Ignore vendor/ instead of vendor/src/ for lint * Rename 'cmd/client' -> 'cmd/ocic' to make it 'go install'able * Rename 'cmd/server' -> 'cmd/ocid' to make it 'go install'able * Update Makefile to build and install from GOPATH * Update tests to locate ocid/ocic in GOPATH/bin * Search for binaries in GOPATH/bin instead of PATH * Install tools using `go get -u`, so they are updated on each run Signed-off-by: Jonathan Yu <jawnsy@redhat.com>
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package graphdriver
|
|
|
|
import "sync"
|
|
|
|
type minfo struct {
|
|
check bool
|
|
count int
|
|
}
|
|
|
|
// RefCounter is a generic counter for use by graphdriver Get/Put calls
|
|
type RefCounter struct {
|
|
counts map[string]*minfo
|
|
mu sync.Mutex
|
|
checker Checker
|
|
}
|
|
|
|
// NewRefCounter returns a new RefCounter
|
|
func NewRefCounter(c Checker) *RefCounter {
|
|
return &RefCounter{
|
|
checker: c,
|
|
counts: make(map[string]*minfo),
|
|
}
|
|
}
|
|
|
|
// Increment increaes the ref count for the given id and returns the current count
|
|
func (c *RefCounter) Increment(path string) int {
|
|
c.mu.Lock()
|
|
m := c.counts[path]
|
|
if m == nil {
|
|
m = &minfo{}
|
|
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
|
|
if c.checker.IsMounted(path) {
|
|
m.count++
|
|
}
|
|
}
|
|
m.count++
|
|
c.mu.Unlock()
|
|
return m.count
|
|
}
|
|
|
|
// Decrement decreases the ref count for the given id and returns the current count
|
|
func (c *RefCounter) Decrement(path string) int {
|
|
c.mu.Lock()
|
|
m := c.counts[path]
|
|
if m == nil {
|
|
m = &minfo{}
|
|
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
|
|
if c.checker.IsMounted(path) {
|
|
m.count++
|
|
}
|
|
}
|
|
m.count--
|
|
c.mu.Unlock()
|
|
return m.count
|
|
}
|