github.com/iaintshine/docker@v1.8.2/graph/mutex.go (about) 1 package graph 2 3 import "sync" 4 5 // imageMutex provides a lock per image id to protect shared resources in the 6 // graph. This is only used with registration but should be used when 7 // manipulating the layer store. 8 type imageMutex struct { 9 mus map[string]*sync.Mutex // mutexes by image id. 10 mu sync.Mutex // protects lock map 11 12 // NOTE(stevvooe): The map above will grow to the size of all images ever 13 // registered during a daemon run. To free these resources, we must 14 // deallocate after unlock. Doing this safely is non-trivial in the face 15 // of a very minor leak. 16 } 17 18 // Lock the provided id. 19 func (im *imageMutex) Lock(id string) { 20 im.getImageLock(id).Lock() 21 } 22 23 // Unlock the provided id. 24 func (im *imageMutex) Unlock(id string) { 25 im.getImageLock(id).Unlock() 26 } 27 28 // getImageLock returns the mutex for the given id. This method will never 29 // return nil. 30 func (im *imageMutex) getImageLock(id string) *sync.Mutex { 31 im.mu.Lock() 32 defer im.mu.Unlock() 33 34 if im.mus == nil { // lazy 35 im.mus = make(map[string]*sync.Mutex) 36 } 37 38 mu, ok := im.mus[id] 39 if !ok { 40 mu = new(sync.Mutex) 41 im.mus[id] = mu 42 } 43 44 return mu 45 }