Przeglądaj źródła

libnetwork/bitseq: make mutex an unexported field

*bitseq.Handle should not implement sync.Locker. The mutex is a private
implementation detail which external consumers should not be able to
manipulate.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 2 lat temu
rodzic
commit
89ae725d23
2 zmienionych plików z 48 dodań i 48 usunięć
  1. 26 26
      libnetwork/bitseq/sequence.go
  2. 22 22
      libnetwork/bitseq/store.go

+ 26 - 26
libnetwork/bitseq/sequence.go

@@ -29,7 +29,7 @@ type Handle struct {
 	dbExists bool
 	dbExists bool
 	store    datastore.DataStore
 	store    datastore.DataStore
 	bm       *bitmap.Bitmap
 	bm       *bitmap.Bitmap
-	sync.Mutex
+	mu       sync.Mutex
 }
 }
 
 
 // NewHandle returns a thread-safe instance of the bitmask handler
 // NewHandle returns a thread-safe instance of the bitmask handler
@@ -96,8 +96,8 @@ func (h *Handle) Unset(ordinal uint64) error {
 // IsSet atomically checks if the ordinal bit is set. In case ordinal
 // IsSet atomically checks if the ordinal bit is set. In case ordinal
 // is outside of the bit sequence limits, false is returned.
 // is outside of the bit sequence limits, false is returned.
 func (h *Handle) IsSet(ordinal uint64) bool {
 func (h *Handle) IsSet(ordinal uint64) bool {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return h.bm.IsSet(ordinal)
 	return h.bm.IsSet(ordinal)
 }
 }
 
 
@@ -105,9 +105,9 @@ func (h *Handle) IsSet(ordinal uint64) bool {
 // It looks for a corruption signature that may happen in docker 1.9.0 and 1.9.1.
 // It looks for a corruption signature that may happen in docker 1.9.0 and 1.9.1.
 func (h *Handle) CheckConsistency() error {
 func (h *Handle) CheckConsistency() error {
 	for {
 	for {
-		h.Lock()
+		h.mu.Lock()
 		store := h.store
 		store := h.store
-		h.Unlock()
+		h.mu.Unlock()
 
 
 		if store != nil {
 		if store != nil {
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
@@ -115,9 +115,9 @@ func (h *Handle) CheckConsistency() error {
 			}
 			}
 		}
 		}
 
 
-		h.Lock()
+		h.mu.Lock()
 		nh := h.getCopy()
 		nh := h.getCopy()
-		h.Unlock()
+		h.mu.Unlock()
 
 
 		if !nh.bm.CheckConsistency() {
 		if !nh.bm.CheckConsistency() {
 			return nil
 			return nil
@@ -132,9 +132,9 @@ func (h *Handle) CheckConsistency() error {
 
 
 		logrus.Infof("Fixed inconsistent bit sequence in datastore:\n%s\n%s", h, nh)
 		logrus.Infof("Fixed inconsistent bit sequence in datastore:\n%s\n%s", h, nh)
 
 
-		h.Lock()
+		h.mu.Lock()
 		h.bm = nh.bm
 		h.bm = nh.bm
-		h.Unlock()
+		h.mu.Unlock()
 
 
 		return nil
 		return nil
 	}
 	}
@@ -144,14 +144,14 @@ func (h *Handle) CheckConsistency() error {
 func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error) {
 func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error) {
 	for {
 	for {
 		var store datastore.DataStore
 		var store datastore.DataStore
-		h.Lock()
+		h.mu.Lock()
 		store = h.store
 		store = h.store
 		if store != nil {
 		if store != nil {
-			h.Unlock() // The lock is acquired in the GetObject
+			h.mu.Unlock() // The lock is acquired in the GetObject
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
 				return 0, err
 				return 0, err
 			}
 			}
-			h.Lock() // Acquire the lock back
+			h.mu.Lock() // Acquire the lock back
 		}
 		}
 
 
 		// Create a private copy of h and work on it
 		// Create a private copy of h and work on it
@@ -159,12 +159,12 @@ func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error)
 
 
 		ret, err := op(nh.bm)
 		ret, err := op(nh.bm)
 		if err != nil {
 		if err != nil {
-			h.Unlock()
+			h.mu.Unlock()
 			return ret, err
 			return ret, err
 		}
 		}
 
 
 		if h.store != nil {
 		if h.store != nil {
-			h.Unlock()
+			h.mu.Unlock()
 			// Attempt to write private copy to store
 			// Attempt to write private copy to store
 			if err := nh.writeToStore(); err != nil {
 			if err := nh.writeToStore(); err != nil {
 				if _, ok := err.(types.RetryError); !ok {
 				if _, ok := err.(types.RetryError); !ok {
@@ -173,14 +173,14 @@ func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error)
 				// Retry
 				// Retry
 				continue
 				continue
 			}
 			}
-			h.Lock()
+			h.mu.Lock()
 		}
 		}
 
 
 		// Previous atomic push was successful. Save private copy to local copy
 		// Previous atomic push was successful. Save private copy to local copy
 		h.bm = nh.bm
 		h.bm = nh.bm
 		h.dbExists = nh.dbExists
 		h.dbExists = nh.dbExists
 		h.dbIndex = nh.dbIndex
 		h.dbIndex = nh.dbIndex
-		h.Unlock()
+		h.mu.Unlock()
 		return ret, nil
 		return ret, nil
 	}
 	}
 }
 }
@@ -207,21 +207,21 @@ func (h *Handle) Destroy() error {
 
 
 // Bits returns the length of the bit sequence
 // Bits returns the length of the bit sequence
 func (h *Handle) Bits() uint64 {
 func (h *Handle) Bits() uint64 {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return h.bm.Bits()
 	return h.bm.Bits()
 }
 }
 
 
 // Unselected returns the number of bits which are not selected
 // Unselected returns the number of bits which are not selected
 func (h *Handle) Unselected() uint64 {
 func (h *Handle) Unselected() uint64 {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return h.bm.Unselected()
 	return h.bm.Unselected()
 }
 }
 
 
 func (h *Handle) String() string {
 func (h *Handle) String() string {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, %s",
 	return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, %s",
 		h.app, h.id, h.dbIndex, h.bm)
 		h.app, h.id, h.dbIndex, h.bm)
 }
 }
@@ -233,8 +233,8 @@ func (h *Handle) MarshalJSON() ([]byte, error) {
 	}
 	}
 
 
 	b, err := func() ([]byte, error) {
 	b, err := func() ([]byte, error) {
-		h.Lock()
-		defer h.Unlock()
+		h.mu.Lock()
+		defer h.mu.Unlock()
 		return h.bm.MarshalBinary()
 		return h.bm.MarshalBinary()
 	}()
 	}()
 	if err != nil {
 	if err != nil {
@@ -260,8 +260,8 @@ func (h *Handle) UnmarshalJSON(data []byte) error {
 		return err
 		return err
 	}
 	}
 
 
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	if err := h.bm.UnmarshalBinary(b); err != nil {
 	if err := h.bm.UnmarshalBinary(b); err != nil {
 		return err
 		return err
 	}
 	}

+ 22 - 22
libnetwork/bitseq/store.go

@@ -10,15 +10,15 @@ import (
 
 
 // Key provides the Key to be used in KV Store
 // Key provides the Key to be used in KV Store
 func (h *Handle) Key() []string {
 func (h *Handle) Key() []string {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return []string{h.app, h.id}
 	return []string{h.app, h.id}
 }
 }
 
 
 // KeyPrefix returns the immediate parent key that can be used for tree walk
 // KeyPrefix returns the immediate parent key that can be used for tree walk
 func (h *Handle) KeyPrefix() []string {
 func (h *Handle) KeyPrefix() []string {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return []string{h.app}
 	return []string{h.app}
 }
 }
 
 
@@ -38,30 +38,30 @@ func (h *Handle) SetValue(value []byte) error {
 
 
 // Index returns the latest DB Index as seen by this object
 // Index returns the latest DB Index as seen by this object
 func (h *Handle) Index() uint64 {
 func (h *Handle) Index() uint64 {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return h.dbIndex
 	return h.dbIndex
 }
 }
 
 
 // SetIndex method allows the datastore to store the latest DB Index into this object
 // SetIndex method allows the datastore to store the latest DB Index into this object
 func (h *Handle) SetIndex(index uint64) {
 func (h *Handle) SetIndex(index uint64) {
-	h.Lock()
+	h.mu.Lock()
 	h.dbIndex = index
 	h.dbIndex = index
 	h.dbExists = true
 	h.dbExists = true
-	h.Unlock()
+	h.mu.Unlock()
 }
 }
 
 
 // Exists method is true if this object has been stored in the DB.
 // Exists method is true if this object has been stored in the DB.
 func (h *Handle) Exists() bool {
 func (h *Handle) Exists() bool {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 	return h.dbExists
 	return h.dbExists
 }
 }
 
 
 // New method returns a handle based on the receiver handle
 // New method returns a handle based on the receiver handle
 func (h *Handle) New() datastore.KVObject {
 func (h *Handle) New() datastore.KVObject {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 
 
 	return &Handle{
 	return &Handle{
 		app:   h.app,
 		app:   h.app,
@@ -71,15 +71,15 @@ func (h *Handle) New() datastore.KVObject {
 
 
 // CopyTo deep copies the handle into the passed destination object
 // CopyTo deep copies the handle into the passed destination object
 func (h *Handle) CopyTo(o datastore.KVObject) error {
 func (h *Handle) CopyTo(o datastore.KVObject) error {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 
 
 	dstH := o.(*Handle)
 	dstH := o.(*Handle)
 	if h == dstH {
 	if h == dstH {
 		return nil
 		return nil
 	}
 	}
-	dstH.Lock()
-	defer dstH.Unlock()
+	dstH.mu.Lock()
+	defer dstH.mu.Unlock()
 	dstH.bm = bitmap.Copy(h.bm)
 	dstH.bm = bitmap.Copy(h.bm)
 	dstH.app = h.app
 	dstH.app = h.app
 	dstH.id = h.id
 	dstH.id = h.id
@@ -97,16 +97,16 @@ func (h *Handle) Skip() bool {
 
 
 // DataScope method returns the storage scope of the datastore
 // DataScope method returns the storage scope of the datastore
 func (h *Handle) DataScope() string {
 func (h *Handle) DataScope() string {
-	h.Lock()
-	defer h.Unlock()
+	h.mu.Lock()
+	defer h.mu.Unlock()
 
 
 	return h.store.Scope()
 	return h.store.Scope()
 }
 }
 
 
 func (h *Handle) writeToStore() error {
 func (h *Handle) writeToStore() error {
-	h.Lock()
+	h.mu.Lock()
 	store := h.store
 	store := h.store
-	h.Unlock()
+	h.mu.Unlock()
 	if store == nil {
 	if store == nil {
 		return nil
 		return nil
 	}
 	}
@@ -118,9 +118,9 @@ func (h *Handle) writeToStore() error {
 }
 }
 
 
 func (h *Handle) deleteFromStore() error {
 func (h *Handle) deleteFromStore() error {
-	h.Lock()
+	h.mu.Lock()
 	store := h.store
 	store := h.store
-	h.Unlock()
+	h.mu.Unlock()
 	if store == nil {
 	if store == nil {
 		return nil
 		return nil
 	}
 	}