Browse code

Rearrange layerstore locking

Signed-off-by: Aidan Hobson Sayers <aidanhs@cantab.net>

Aidan Hobson Sayers authored on 2015/11/20 21:35:01
Showing 3 changed files
... ...
@@ -269,7 +269,7 @@ func (ls *layerStore) Register(ts io.Reader, parent ChainID) (Layer, error) {
269 269
 	ls.layerL.Lock()
270 270
 	defer ls.layerL.Unlock()
271 271
 
272
-	if existingLayer := ls.getAndRetainLayer(layer.chainID); existingLayer != nil {
272
+	if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil {
273 273
 		// Set error for cleanup, but do not return the error
274 274
 		err = errors.New("layer already exists")
275 275
 		return existingLayer.getReference(), nil
... ...
@@ -284,18 +284,21 @@ func (ls *layerStore) Register(ts io.Reader, parent ChainID) (Layer, error) {
284 284
 	return layer.getReference(), nil
285 285
 }
286 286
 
287
-func (ls *layerStore) get(l ChainID) *roLayer {
288
-	ls.layerL.Lock()
289
-	defer ls.layerL.Unlock()
290
-
291
-	layer, ok := ls.layerMap[l]
287
+func (ls *layerStore) getWithoutLock(layer ChainID) *roLayer {
288
+	l, ok := ls.layerMap[layer]
292 289
 	if !ok {
293 290
 		return nil
294 291
 	}
295 292
 
296
-	layer.referenceCount++
293
+	l.referenceCount++
294
+
295
+	return l
296
+}
297 297
 
298
-	return layer
298
+func (ls *layerStore) get(l ChainID) *roLayer {
299
+	ls.layerL.Lock()
300
+	defer ls.layerL.Unlock()
301
+	return ls.getWithoutLock(l)
299 302
 }
300 303
 
301 304
 func (ls *layerStore) Get(l ChainID) (Layer, error) {
... ...
@@ -415,17 +418,6 @@ func (ls *layerStore) saveMount(mount *mountedLayer) error {
415 415
 	return nil
416 416
 }
417 417
 
418
-func (ls *layerStore) getAndRetainLayer(layer ChainID) *roLayer {
419
-	l, ok := ls.layerMap[layer]
420
-	if !ok {
421
-		return nil
422
-	}
423
-
424
-	l.referenceCount++
425
-
426
-	return l
427
-}
428
-
429 418
 func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc MountInit) (string, error) {
430 419
 	// Use "<graph-id>-init" to maintain compatibility with graph drivers
431 420
 	// which are expecting this layer with this special name. If all
... ...
@@ -468,9 +460,7 @@ func (ls *layerStore) Mount(name string, parent ChainID, mountLabel string, init
468 468
 	var pid string
469 469
 	var p *roLayer
470 470
 	if string(parent) != "" {
471
-		ls.layerL.Lock()
472
-		p = ls.getAndRetainLayer(parent)
473
-		ls.layerL.Unlock()
471
+		p = ls.get(parent)
474 472
 		if p == nil {
475 473
 			return nil, ErrLayerDoesNotExist
476 474
 		}
... ...
@@ -93,7 +93,7 @@ func (ls *layerStore) RegisterDiffID(graphID string, size int64) (Layer, error)
93 93
 	ls.layerL.Lock()
94 94
 	defer ls.layerL.Unlock()
95 95
 
96
-	if existingLayer := ls.getAndRetainLayer(layer.chainID); existingLayer != nil {
96
+	if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil {
97 97
 		// Set error for cleanup, but do not return
98 98
 		err = errors.New("layer already exists")
99 99
 		return existingLayer.getReference(), nil
... ...
@@ -35,9 +35,7 @@ func (ls *layerStore) MountByGraphID(name string, graphID string, parent ChainID
35 35
 
36 36
 	var p *roLayer
37 37
 	if string(parent) != "" {
38
-		ls.layerL.Lock()
39
-		p = ls.getAndRetainLayer(parent)
40
-		ls.layerL.Unlock()
38
+		p = ls.get(parent)
41 39
 		if p == nil {
42 40
 			return nil, ErrLayerDoesNotExist
43 41
 		}
... ...
@@ -209,7 +207,7 @@ func (ls *layerStore) RegisterByGraphID(graphID string, parent ChainID, tarDataF
209 209
 	ls.layerL.Lock()
210 210
 	defer ls.layerL.Unlock()
211 211
 
212
-	if existingLayer := ls.getAndRetainLayer(layer.chainID); existingLayer != nil {
212
+	if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil {
213 213
 		// Set error for cleanup, but do not return
214 214
 		err = errors.New("layer already exists")
215 215
 		return existingLayer.getReference(), nil