]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/par: actually make par.Work run things in parallel
authorRuss Cox <rsc@golang.org>
Thu, 19 Jul 2018 17:23:32 +0000 (13:23 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 19 Jul 2018 18:16:00 +0000 (18:16 +0000)
This was an unfortunate debugging print introduced
while working on the unfortunately large CL 123576.
At least now we're done with that awfulness.

Change-Id: Ib83db59382a799f649832d22d3c6f039d2ef9d2c
Reviewed-on: https://go-review.googlesource.com/125015
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/par/work.go
src/cmd/go/internal/par/work_test.go

index 6543f1a35c51bf5dece282ecd200face375649bd..a568c86f60b77c171ec438322e5bb980be38cd5b 100644 (file)
@@ -55,7 +55,6 @@ func (w *Work) Do(n int, f func(item interface{})) {
        if n < 1 {
                panic("par.Work.Do: n < 1")
        }
-       n = 1
        if w.running >= 1 {
                panic("par.Work.Do: already called Do")
        }
index 71c0395d3b37f3f2fdeeb8ff608cd7166cc685f9..53a715ea81c73e6ecfbe4fc4f0455effd88a78c7 100644 (file)
@@ -7,6 +7,7 @@ package par
 import (
        "sync/atomic"
        "testing"
+       "time"
 )
 
 func TestWork(t *testing.T) {
@@ -30,6 +31,30 @@ func TestWork(t *testing.T) {
        }
 }
 
+func TestWorkParallel(t *testing.T) {
+       var w Work
+
+       for tries := 0; tries < 10; tries++ {
+               const N = 100
+               for i := 0; i < N; i++ {
+                       w.Add(i)
+               }
+               start := time.Now()
+               var n int32
+               w.Do(N, func(x interface{}) {
+                       time.Sleep(1 * time.Millisecond)
+                       atomic.AddInt32(&n, +1)
+               })
+               if n != N {
+                       t.Fatalf("par.Work.Do did not do all the work")
+               }
+               if time.Since(start) < N/2*time.Millisecond {
+                       return
+               }
+       }
+       t.Fatalf("par.Work.Do does not seem to be parallel")
+}
+
 func TestCache(t *testing.T) {
        var cache Cache