Browse code

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 authored on 2023/01/21 03:54:26
Showing 2 changed files
... ...
@@ -29,7 +29,7 @@ type Handle struct {
29 29
 	dbExists bool
30 30
 	store    datastore.DataStore
31 31
 	bm       *bitmap.Bitmap
32
-	sync.Mutex
32
+	mu       sync.Mutex
33 33
 }
34 34
 
35 35
 // NewHandle returns a thread-safe instance of the bitmask handler
... ...
@@ -96,8 +96,8 @@ func (h *Handle) Unset(ordinal uint64) error {
96 96
 // IsSet atomically checks if the ordinal bit is set. In case ordinal
97 97
 // is outside of the bit sequence limits, false is returned.
98 98
 func (h *Handle) IsSet(ordinal uint64) bool {
99
-	h.Lock()
100
-	defer h.Unlock()
99
+	h.mu.Lock()
100
+	defer h.mu.Unlock()
101 101
 	return h.bm.IsSet(ordinal)
102 102
 }
103 103
 
... ...
@@ -105,9 +105,9 @@ func (h *Handle) IsSet(ordinal uint64) bool {
105 105
 // It looks for a corruption signature that may happen in docker 1.9.0 and 1.9.1.
106 106
 func (h *Handle) CheckConsistency() error {
107 107
 	for {
108
-		h.Lock()
108
+		h.mu.Lock()
109 109
 		store := h.store
110
-		h.Unlock()
110
+		h.mu.Unlock()
111 111
 
112 112
 		if store != nil {
113 113
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
... ...
@@ -115,9 +115,9 @@ func (h *Handle) CheckConsistency() error {
115 115
 			}
116 116
 		}
117 117
 
118
-		h.Lock()
118
+		h.mu.Lock()
119 119
 		nh := h.getCopy()
120
-		h.Unlock()
120
+		h.mu.Unlock()
121 121
 
122 122
 		if !nh.bm.CheckConsistency() {
123 123
 			return nil
... ...
@@ -132,9 +132,9 @@ func (h *Handle) CheckConsistency() error {
132 132
 
133 133
 		logrus.Infof("Fixed inconsistent bit sequence in datastore:\n%s\n%s", h, nh)
134 134
 
135
-		h.Lock()
135
+		h.mu.Lock()
136 136
 		h.bm = nh.bm
137
-		h.Unlock()
137
+		h.mu.Unlock()
138 138
 
139 139
 		return nil
140 140
 	}
... ...
@@ -144,14 +144,14 @@ func (h *Handle) CheckConsistency() error {
144 144
 func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error) {
145 145
 	for {
146 146
 		var store datastore.DataStore
147
-		h.Lock()
147
+		h.mu.Lock()
148 148
 		store = h.store
149 149
 		if store != nil {
150
-			h.Unlock() // The lock is acquired in the GetObject
150
+			h.mu.Unlock() // The lock is acquired in the GetObject
151 151
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
152 152
 				return 0, err
153 153
 			}
154
-			h.Lock() // Acquire the lock back
154
+			h.mu.Lock() // Acquire the lock back
155 155
 		}
156 156
 
157 157
 		// 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)
159 159
 
160 160
 		ret, err := op(nh.bm)
161 161
 		if err != nil {
162
-			h.Unlock()
162
+			h.mu.Unlock()
163 163
 			return ret, err
164 164
 		}
165 165
 
166 166
 		if h.store != nil {
167
-			h.Unlock()
167
+			h.mu.Unlock()
168 168
 			// Attempt to write private copy to store
169 169
 			if err := nh.writeToStore(); err != nil {
170 170
 				if _, ok := err.(types.RetryError); !ok {
... ...
@@ -173,14 +173,14 @@ func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error)
173 173
 				// Retry
174 174
 				continue
175 175
 			}
176
-			h.Lock()
176
+			h.mu.Lock()
177 177
 		}
178 178
 
179 179
 		// Previous atomic push was successful. Save private copy to local copy
180 180
 		h.bm = nh.bm
181 181
 		h.dbExists = nh.dbExists
182 182
 		h.dbIndex = nh.dbIndex
183
-		h.Unlock()
183
+		h.mu.Unlock()
184 184
 		return ret, nil
185 185
 	}
186 186
 }
... ...
@@ -207,21 +207,21 @@ func (h *Handle) Destroy() error {
207 207
 
208 208
 // Bits returns the length of the bit sequence
209 209
 func (h *Handle) Bits() uint64 {
210
-	h.Lock()
211
-	defer h.Unlock()
210
+	h.mu.Lock()
211
+	defer h.mu.Unlock()
212 212
 	return h.bm.Bits()
213 213
 }
214 214
 
215 215
 // Unselected returns the number of bits which are not selected
216 216
 func (h *Handle) Unselected() uint64 {
217
-	h.Lock()
218
-	defer h.Unlock()
217
+	h.mu.Lock()
218
+	defer h.mu.Unlock()
219 219
 	return h.bm.Unselected()
220 220
 }
221 221
 
222 222
 func (h *Handle) String() string {
223
-	h.Lock()
224
-	defer h.Unlock()
223
+	h.mu.Lock()
224
+	defer h.mu.Unlock()
225 225
 	return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, %s",
226 226
 		h.app, h.id, h.dbIndex, h.bm)
227 227
 }
... ...
@@ -233,8 +233,8 @@ func (h *Handle) MarshalJSON() ([]byte, error) {
233 233
 	}
234 234
 
235 235
 	b, err := func() ([]byte, error) {
236
-		h.Lock()
237
-		defer h.Unlock()
236
+		h.mu.Lock()
237
+		defer h.mu.Unlock()
238 238
 		return h.bm.MarshalBinary()
239 239
 	}()
240 240
 	if err != nil {
... ...
@@ -260,8 +260,8 @@ func (h *Handle) UnmarshalJSON(data []byte) error {
260 260
 		return err
261 261
 	}
262 262
 
263
-	h.Lock()
264
-	defer h.Unlock()
263
+	h.mu.Lock()
264
+	defer h.mu.Unlock()
265 265
 	if err := h.bm.UnmarshalBinary(b); err != nil {
266 266
 		return err
267 267
 	}
... ...
@@ -10,15 +10,15 @@ import (
10 10
 
11 11
 // Key provides the Key to be used in KV Store
12 12
 func (h *Handle) Key() []string {
13
-	h.Lock()
14
-	defer h.Unlock()
13
+	h.mu.Lock()
14
+	defer h.mu.Unlock()
15 15
 	return []string{h.app, h.id}
16 16
 }
17 17
 
18 18
 // KeyPrefix returns the immediate parent key that can be used for tree walk
19 19
 func (h *Handle) KeyPrefix() []string {
20
-	h.Lock()
21
-	defer h.Unlock()
20
+	h.mu.Lock()
21
+	defer h.mu.Unlock()
22 22
 	return []string{h.app}
23 23
 }
24 24
 
... ...
@@ -38,30 +38,30 @@ func (h *Handle) SetValue(value []byte) error {
38 38
 
39 39
 // Index returns the latest DB Index as seen by this object
40 40
 func (h *Handle) Index() uint64 {
41
-	h.Lock()
42
-	defer h.Unlock()
41
+	h.mu.Lock()
42
+	defer h.mu.Unlock()
43 43
 	return h.dbIndex
44 44
 }
45 45
 
46 46
 // SetIndex method allows the datastore to store the latest DB Index into this object
47 47
 func (h *Handle) SetIndex(index uint64) {
48
-	h.Lock()
48
+	h.mu.Lock()
49 49
 	h.dbIndex = index
50 50
 	h.dbExists = true
51
-	h.Unlock()
51
+	h.mu.Unlock()
52 52
 }
53 53
 
54 54
 // Exists method is true if this object has been stored in the DB.
55 55
 func (h *Handle) Exists() bool {
56
-	h.Lock()
57
-	defer h.Unlock()
56
+	h.mu.Lock()
57
+	defer h.mu.Unlock()
58 58
 	return h.dbExists
59 59
 }
60 60
 
61 61
 // New method returns a handle based on the receiver handle
62 62
 func (h *Handle) New() datastore.KVObject {
63
-	h.Lock()
64
-	defer h.Unlock()
63
+	h.mu.Lock()
64
+	defer h.mu.Unlock()
65 65
 
66 66
 	return &Handle{
67 67
 		app:   h.app,
... ...
@@ -71,15 +71,15 @@ func (h *Handle) New() datastore.KVObject {
71 71
 
72 72
 // CopyTo deep copies the handle into the passed destination object
73 73
 func (h *Handle) CopyTo(o datastore.KVObject) error {
74
-	h.Lock()
75
-	defer h.Unlock()
74
+	h.mu.Lock()
75
+	defer h.mu.Unlock()
76 76
 
77 77
 	dstH := o.(*Handle)
78 78
 	if h == dstH {
79 79
 		return nil
80 80
 	}
81
-	dstH.Lock()
82
-	defer dstH.Unlock()
81
+	dstH.mu.Lock()
82
+	defer dstH.mu.Unlock()
83 83
 	dstH.bm = bitmap.Copy(h.bm)
84 84
 	dstH.app = h.app
85 85
 	dstH.id = h.id
... ...
@@ -97,16 +97,16 @@ func (h *Handle) Skip() bool {
97 97
 
98 98
 // DataScope method returns the storage scope of the datastore
99 99
 func (h *Handle) DataScope() string {
100
-	h.Lock()
101
-	defer h.Unlock()
100
+	h.mu.Lock()
101
+	defer h.mu.Unlock()
102 102
 
103 103
 	return h.store.Scope()
104 104
 }
105 105
 
106 106
 func (h *Handle) writeToStore() error {
107
-	h.Lock()
107
+	h.mu.Lock()
108 108
 	store := h.store
109
-	h.Unlock()
109
+	h.mu.Unlock()
110 110
 	if store == nil {
111 111
 		return nil
112 112
 	}
... ...
@@ -118,9 +118,9 @@ func (h *Handle) writeToStore() error {
118 118
 }
119 119
 
120 120
 func (h *Handle) deleteFromStore() error {
121
-	h.Lock()
121
+	h.mu.Lock()
122 122
 	store := h.store
123
-	h.Unlock()
123
+	h.mu.Unlock()
124 124
 	if store == nil {
125 125
 		return nil
126 126
 	}