sync: Proposal for barrier implementation
As discussed in the mailing list, this adds a simple barrier
implementation to the sync package which enables one or more
goroutines to wait for a counter to go down to zero.
Added Done() and DoneWait() to the API, using the same primitives. The former is just ...
14 years, 2 months ago
(2011-01-05 12:52:25 UTC)
#2
Added Done() and DoneWait() to the API, using the same primitives. The former is
just Add(-1), but it's much nicer as a name (and was suggested in the ticket).
The latter is useful on computations which have to wait for all goroutines to
reach the same point, and is error prone to do without (Done() + Wait() is not
correct due to lack of atomicity).
I've reduced the proposal to reduce the API to the part that is not being ...
14 years, 2 months ago
(2011-01-05 22:14:01 UTC)
#3
I've reduced the proposal to reduce the API to the part that is
not being disputed in issue 1382, and included some proper testing.
I will post these further enhancements in a separate CL once the
agreed bits go in.
http://codereview.appspot.com/3770045/diff/17001/src/pkg/sync/barrier_test.go File src/pkg/sync/barrier_test.go (right): http://codereview.appspot.com/3770045/diff/17001/src/pkg/sync/barrier_test.go#newcode16 src/pkg/sync/barrier_test.go:16: max = i what's this trying to test? it's ...
14 years, 2 months ago
(2011-01-06 15:22:53 UTC)
#4
Problem fixed in the latest version, and other improvements. http://codereview.appspot.com/3770045/diff/17001/src/pkg/sync/barrier_test.go File src/pkg/sync/barrier_test.go (right): http://codereview.appspot.com/3770045/diff/17001/src/pkg/sync/barrier_test.go#newcode16 src/pkg/sync/barrier_test.go:16: ...
14 years, 2 months ago
(2011-01-06 16:39:43 UTC)
#6
http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go File src/pkg/sync/waitgroup.go (right): http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go#newcode9 src/pkg/sync/waitgroup.go:9: // to wait for a task which is being ...
14 years, 2 months ago
(2011-01-06 20:38:43 UTC)
#7
http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go File src/pkg/sync/waitgroup.go (right): http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go#newcode42 src/pkg/sync/waitgroup.go:42: // becomes zero, all goroutines blocked on Wait() are ...
14 years, 2 months ago
(2011-01-06 22:44:03 UTC)
#10
http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go
File src/pkg/sync/waitgroup.go (right):
http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go#ne...
src/pkg/sync/waitgroup.go:42: // becomes zero, all goroutines blocked on Wait()
are released.
On 2011/01/06 21:31:39, niemeyer wrote:
> > is there a good use case for allowing negative deltas?
>
> Yes, Done() is Add(-1) which looks clearer and more concise.
that's an implementation decision. i was asking from an API point of view.
http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go#ne...
src/pkg/sync/waitgroup.go:50: if wg.counter == 0 && wg.waiters > 0 {
On 2011/01/06 21:31:39, niemeyer wrote:
> > if wg.counter == 0 {
>
> No reason to execute the block if waiters is 0.
no need to check the condition since the code inside the block deals with
waiters==0 fine. premature optimisation IMHO - it's easier to read the code
without the check.
http://codereview.appspot.com/3770045/diff/39001/src/pkg/sync/waitgroup.go#ne...
src/pkg/sync/waitgroup.go:66: // In case the WaitGroup counter is greater than
zero, block the
On 2011/01/06 21:31:39, niemeyer wrote:
> > // Wait blocks until the WaitGroup counter is zero.
>
> The original text looks better in this case.
see russ's comment.
http://codereview.appspot.com/3770045/diff/47001/src/pkg/sync/waitgroup.go File src/pkg/sync/waitgroup.go (right): http://codereview.appspot.com/3770045/diff/47001/src/pkg/sync/waitgroup.go#newcode12 src/pkg/sync/waitgroup.go:12: // given number of goroutines have finished. Although this ...
14 years, 2 months ago
(2011-01-07 10:49:01 UTC)
#12
http://codereview.appspot.com/3770045/diff/47001/src/pkg/sync/waitgroup.go
File src/pkg/sync/waitgroup.go (right):
http://codereview.appspot.com/3770045/diff/47001/src/pkg/sync/waitgroup.go#ne...
src/pkg/sync/waitgroup.go:12: // given number of goroutines have finished.
Although this is Russ's original text, it's a perhaps little too specific for
this implementation, which is more general (for instance Add need not be called
by the main goroutine, and Wait can be called several times).
how about this:
// A WaitGroup waits for a collection of goroutines to finish.
// Add is called to add goroutines to the collection.
// Then each of the goroutines runs and calls Done when finished.
// At the same time, Wait can be used to block until all the goroutines finish.
http://codereview.appspot.com/3770045/diff/47001/src/pkg/sync/waitgroup.go#ne...
src/pkg/sync/waitgroup.go:63: // Done decrements the WaitGroup counter by one.
When the counter
// Done decrements the WaitGroup goroutine count. If it becomes zero,
// any calls to Wait are unblocked.
(The main description doesn't mention a counter)
http://codereview.appspot.com/3770045/diff/47001/src/pkg/sync/waitgroup.go#ne...
src/pkg/sync/waitgroup.go:69: // Wait blocks the calling goroutine while the
WaitGroup counter is
// Wait blocks until the WaitGroup goroutine count is zero.
(matching the language in Mutex.Lock)
All addressed, with the following details. PTAL http://codereview.appspot.com/3770045/diff/13001/src/pkg/sync/barrier.go File src/pkg/sync/barrier.go (right): http://codereview.appspot.com/3770045/diff/13001/src/pkg/sync/barrier.go#newcode4 src/pkg/sync/barrier.go:4: // Synchronization ...
14 years, 1 month ago
(2011-02-03 14:28:54 UTC)
#15
http://codereview.appspot.com/3770045/diff/59001/src/pkg/sync/waitgroup.go File src/pkg/sync/waitgroup.go (right): http://codereview.appspot.com/3770045/diff/59001/src/pkg/sync/waitgroup.go#newcode39 src/pkg/sync/waitgroup.go:39: // G3: Add(1) G3 still comes out of nowhere. ...
14 years, 1 month ago
(2011-02-03 15:32:19 UTC)
#16
> There needs to be a reason that G3 would think that > the waitgroup ...
14 years, 1 month ago
(2011-02-03 16:27:28 UTC)
#17
> There needs to be a reason that G3 would think that
> the waitgroup was available for use. That is, it has
Ah, I misunderstood your concern.
I've managed to introduce the G3 Wait() while maintaining
the bug, and also clarified the state.
PTAL
looks good but the package comment will need to be updated http://codereview.appspot.com/3770045/diff/61001/src/pkg/sync/waitgroup.go File src/pkg/sync/waitgroup.go (right): ...
14 years, 1 month ago
(2011-02-03 20:18:07 UTC)
#19
*** Submitted as http://code.google.com/p/go/source/detail?r=85f202d81c64 *** sync: Proposal for barrier implementation As discussed in the mailing ...
14 years, 1 month ago
(2011-02-03 20:39:27 UTC)
#22
Issue 3770045: code review 3770045: sync: Proposal for barrier implementation
(Closed)
Created 14 years, 2 months ago by niemeyer
Modified 14 years, 1 month ago
Reviewers:
Base URL:
Comments: 35