Browse code

Mutex style change.

For structs protected by a single mutex, embed the mutex for more
concise usage.

Also use a sync.Mutex directly, rather than a pointer, to avoid the
need for initialization (because a Mutex's zero-value is valid and
ready to be used).

Caleb Spare authored on 2013/07/03 07:46:32
Showing 8 changed files
... ...
@@ -3,7 +3,6 @@ package docker
3 3
 import (
4 4
 	"fmt"
5 5
 	"io/ioutil"
6
-	"sync"
7 6
 	"testing"
8 7
 )
9 8
 
... ...
@@ -105,7 +104,6 @@ func TestBuild(t *testing.T) {
105 105
 
106 106
 		srv := &Server{
107 107
 			runtime:     runtime,
108
-			lock:        &sync.Mutex{},
109 108
 			pullingPool: make(map[string]struct{}),
110 109
 			pushingPool: make(map[string]struct{}),
111 110
 		}
... ...
@@ -466,8 +466,8 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
466 466
 }
467 467
 
468 468
 func (container *Container) Start(hostConfig *HostConfig) error {
469
-	container.State.lock()
470
-	defer container.State.unlock()
469
+	container.State.Lock()
470
+	defer container.State.Unlock()
471 471
 
472 472
 	if container.State.Running {
473 473
 		return fmt.Errorf("The container %s is already running.", container.ID)
... ...
@@ -821,8 +821,8 @@ func (container *Container) kill() error {
821 821
 }
822 822
 
823 823
 func (container *Container) Kill() error {
824
-	container.State.lock()
825
-	defer container.State.unlock()
824
+	container.State.Lock()
825
+	defer container.State.Unlock()
826 826
 	if !container.State.Running {
827 827
 		return nil
828 828
 	}
... ...
@@ -830,8 +830,8 @@ func (container *Container) Kill() error {
830 830
 }
831 831
 
832 832
 func (container *Container) Stop(seconds int) error {
833
-	container.State.lock()
834
-	defer container.State.unlock()
833
+	container.State.Lock()
834
+	defer container.State.Unlock()
835 835
 	if !container.State.Running {
836 836
 		return nil
837 837
 	}
... ...
@@ -301,9 +301,9 @@ func newPortMapper() (*PortMapper, error) {
301 301
 
302 302
 // Port allocator: Atomatically allocate and release networking ports
303 303
 type PortAllocator struct {
304
+	sync.Mutex
304 305
 	inUse    map[int]struct{}
305 306
 	fountain chan (int)
306
-	lock     sync.Mutex
307 307
 }
308 308
 
309 309
 func (alloc *PortAllocator) runFountain() {
... ...
@@ -317,9 +317,9 @@ func (alloc *PortAllocator) runFountain() {
317 317
 // FIXME: Release can no longer fail, change its prototype to reflect that.
318 318
 func (alloc *PortAllocator) Release(port int) error {
319 319
 	utils.Debugf("Releasing %d", port)
320
-	alloc.lock.Lock()
320
+	alloc.Lock()
321 321
 	delete(alloc.inUse, port)
322
-	alloc.lock.Unlock()
322
+	alloc.Unlock()
323 323
 	return nil
324 324
 }
325 325
 
... ...
@@ -334,8 +334,8 @@ func (alloc *PortAllocator) Acquire(port int) (int, error) {
334 334
 		}
335 335
 		return -1, fmt.Errorf("Port generator ended unexpectedly")
336 336
 	}
337
-	alloc.lock.Lock()
338
-	defer alloc.lock.Unlock()
337
+	alloc.Lock()
338
+	defer alloc.Unlock()
339 339
 	if _, inUse := alloc.inUse[port]; inUse {
340 340
 		return -1, fmt.Errorf("Port already in use: %d", port)
341 341
 	}
... ...
@@ -108,9 +108,6 @@ func (runtime *Runtime) Register(container *Container) error {
108 108
 	// init the wait lock
109 109
 	container.waitLock = make(chan struct{})
110 110
 
111
-	// Even if not running, we init the lock (prevents races in start/stop/kill)
112
-	container.State.initLock()
113
-
114 111
 	container.runtime = runtime
115 112
 
116 113
 	// Attach to stdout and stderr
... ...
@@ -89,7 +89,6 @@ func init() {
89 89
 	srv := &Server{
90 90
 		runtime:     runtime,
91 91
 		enableCors:  false,
92
-		lock:        &sync.Mutex{},
93 92
 		pullingPool: make(map[string]struct{}),
94 93
 		pushingPool: make(map[string]struct{}),
95 94
 	}
... ...
@@ -450,8 +450,8 @@ func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, local, re
450 450
 }
451 451
 
452 452
 func (srv *Server) poolAdd(kind, key string) error {
453
-	srv.lock.Lock()
454
-	defer srv.lock.Unlock()
453
+	srv.Lock()
454
+	defer srv.Unlock()
455 455
 
456 456
 	if _, exists := srv.pullingPool[key]; exists {
457 457
 		return fmt.Errorf("%s %s is already in progress", key, kind)
... ...
@@ -1119,7 +1119,6 @@ func NewServer(flGraphPath string, autoRestart, enableCors bool, dns ListOpts) (
1119 1119
 	srv := &Server{
1120 1120
 		runtime:     runtime,
1121 1121
 		enableCors:  enableCors,
1122
-		lock:        &sync.Mutex{},
1123 1122
 		pullingPool: make(map[string]struct{}),
1124 1123
 		pushingPool: make(map[string]struct{}),
1125 1124
 	}
... ...
@@ -1128,9 +1127,9 @@ func NewServer(flGraphPath string, autoRestart, enableCors bool, dns ListOpts) (
1128 1128
 }
1129 1129
 
1130 1130
 type Server struct {
1131
+	sync.Mutex
1131 1132
 	runtime     *Runtime
1132 1133
 	enableCors  bool
1133
-	lock        *sync.Mutex
1134 1134
 	pullingPool map[string]struct{}
1135 1135
 	pushingPool map[string]struct{}
1136 1136
 }
... ...
@@ -8,11 +8,11 @@ import (
8 8
 )
9 9
 
10 10
 type State struct {
11
+	sync.Mutex
11 12
 	Running   bool
12 13
 	Pid       int
13 14
 	ExitCode  int
14 15
 	StartedAt time.Time
15
-	l         *sync.Mutex
16 16
 	Ghost     bool
17 17
 }
18 18
 
... ...
@@ -39,15 +39,3 @@ func (s *State) setStopped(exitCode int) {
39 39
 	s.Pid = 0
40 40
 	s.ExitCode = exitCode
41 41
 }
42
-
43
-func (s *State) initLock() {
44
-	s.l = &sync.Mutex{}
45
-}
46
-
47
-func (s *State) lock() {
48
-	s.l.Lock()
49
-}
50
-
51
-func (s *State) unlock() {
52
-	s.l.Unlock()
53
-}
... ...
@@ -188,10 +188,10 @@ func NopWriteCloser(w io.Writer) io.WriteCloser {
188 188
 }
189 189
 
190 190
 type bufReader struct {
191
+	sync.Mutex
191 192
 	buf    *bytes.Buffer
192 193
 	reader io.Reader
193 194
 	err    error
194
-	l      sync.Mutex
195 195
 	wait   sync.Cond
196 196
 }
197 197
 
... ...
@@ -200,7 +200,7 @@ func NewBufReader(r io.Reader) *bufReader {
200 200
 		buf:    &bytes.Buffer{},
201 201
 		reader: r,
202 202
 	}
203
-	reader.wait.L = &reader.l
203
+	reader.wait.L = &reader.Mutex
204 204
 	go reader.drain()
205 205
 	return reader
206 206
 }
... ...
@@ -209,14 +209,14 @@ func (r *bufReader) drain() {
209 209
 	buf := make([]byte, 1024)
210 210
 	for {
211 211
 		n, err := r.reader.Read(buf)
212
-		r.l.Lock()
212
+		r.Lock()
213 213
 		if err != nil {
214 214
 			r.err = err
215 215
 		} else {
216 216
 			r.buf.Write(buf[0:n])
217 217
 		}
218 218
 		r.wait.Signal()
219
-		r.l.Unlock()
219
+		r.Unlock()
220 220
 		if err != nil {
221 221
 			break
222 222
 		}
... ...
@@ -224,8 +224,8 @@ func (r *bufReader) drain() {
224 224
 }
225 225
 
226 226
 func (r *bufReader) Read(p []byte) (n int, err error) {
227
-	r.l.Lock()
228
-	defer r.l.Unlock()
227
+	r.Lock()
228
+	defer r.Unlock()
229 229
 	for {
230 230
 		n, err = r.buf.Read(p)
231 231
 		if n > 0 {
... ...
@@ -247,27 +247,27 @@ func (r *bufReader) Close() error {
247 247
 }
248 248
 
249 249
 type WriteBroadcaster struct {
250
-	mu      sync.Mutex
250
+	sync.Mutex
251 251
 	writers map[io.WriteCloser]struct{}
252 252
 }
253 253
 
254 254
 func (w *WriteBroadcaster) AddWriter(writer io.WriteCloser) {
255
-	w.mu.Lock()
255
+	w.Lock()
256 256
 	w.writers[writer] = struct{}{}
257
-	w.mu.Unlock()
257
+	w.Unlock()
258 258
 }
259 259
 
260 260
 // FIXME: Is that function used?
261 261
 // FIXME: This relies on the concrete writer type used having equality operator
262 262
 func (w *WriteBroadcaster) RemoveWriter(writer io.WriteCloser) {
263
-	w.mu.Lock()
263
+	w.Lock()
264 264
 	delete(w.writers, writer)
265
-	w.mu.Unlock()
265
+	w.Unlock()
266 266
 }
267 267
 
268 268
 func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
269
-	w.mu.Lock()
270
-	defer w.mu.Unlock()
269
+	w.Lock()
270
+	defer w.Unlock()
271 271
 	for writer := range w.writers {
272 272
 		if n, err := writer.Write(p); err != nil || n != len(p) {
273 273
 			// On error, evict the writer
... ...
@@ -278,8 +278,8 @@ func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
278 278
 }
279 279
 
280 280
 func (w *WriteBroadcaster) CloseWriters() error {
281
-	w.mu.Lock()
282
-	defer w.mu.Unlock()
281
+	w.Lock()
282
+	defer w.Unlock()
283 283
 	for writer := range w.writers {
284 284
 		writer.Close()
285 285
 	}