Browse code

Make Broadcaster Wait function wait for all writers to finish before returning

Also, use the channel to determine if the broadcaster is closed,
removing the redundant isClosed variable.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2015/09/11 02:08:02
Showing 1 changed files
... ...
@@ -24,9 +24,6 @@ type Broadcaster struct {
24 24
 	history [][]byte
25 25
 	// wg is a WaitGroup used to wait for all writes to finish on Close
26 26
 	wg sync.WaitGroup
27
-	// isClosed is set to true when Close is called to avoid closing c
28
-	// multiple times.
29
-	isClosed bool
30 27
 	// result is the argument passed to the first call of Close, and
31 28
 	// returned to callers of Wait
32 29
 	result error
... ...
@@ -141,11 +138,10 @@ func (broadcaster *Broadcaster) Add(w io.Writer) error {
141 141
 // argument is a result that should be returned to waiters blocking on Wait.
142 142
 func (broadcaster *Broadcaster) CloseWithError(result error) {
143 143
 	broadcaster.Lock()
144
-	if broadcaster.isClosed {
144
+	if broadcaster.closed() {
145 145
 		broadcaster.Unlock()
146 146
 		return
147 147
 	}
148
-	broadcaster.isClosed = true
149 148
 	broadcaster.result = result
150 149
 	close(broadcaster.c)
151 150
 	broadcaster.cond.Broadcast()
... ...
@@ -161,9 +157,11 @@ func (broadcaster *Broadcaster) Close() {
161 161
 	broadcaster.CloseWithError(nil)
162 162
 }
163 163
 
164
-// Wait blocks until the operation is marked as completed by the Done method.
165
-// It returns the argument that was passed to Close.
164
+// Wait blocks until the operation is marked as completed by the Close method,
165
+// and all writer goroutines have completed. It returns the argument that was
166
+// passed to Close.
166 167
 func (broadcaster *Broadcaster) Wait() error {
167 168
 	<-broadcaster.c
169
+	broadcaster.wg.Wait()
168 170
 	return broadcaster.result
169 171
 }