]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: simplify the control flow in sweepone
authorAustin Clements <austin@google.com>
Wed, 19 Sep 2018 20:42:13 +0000 (16:42 -0400)
committerAustin Clements <austin@google.com>
Thu, 27 Sep 2018 18:04:50 +0000 (18:04 +0000)
Ending a loop with a break is confusing. Rewrite the loop so the
default behavior is to loop and then do the "post-loop" work outside
of the loop.

Change-Id: Ie49b4132541dfb5124c31a8163f2c883aa4abc75
Reviewed-on: https://go-review.googlesource.com/138155
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
src/runtime/mgcsweep.go

index ecfdee59f483b5f39e09cfba2e4f5e241aa4babe..5cdede002a188ebd86c65a1d81df9dd0eaced9d5 100644 (file)
@@ -88,10 +88,11 @@ func sweepone() uintptr {
        }
        atomic.Xadd(&mheap_.sweepers, +1)
 
-       npages := ^uintptr(0)
+       // Find a span to sweep.
+       var s *mspan
        sg := mheap_.sweepgen
        for {
-               s := mheap_.sweepSpans[1-sg/2%2].pop()
+               s = mheap_.sweepSpans[1-sg/2%2].pop()
                if s == nil {
                        atomic.Store(&mheap_.sweepdone, 1)
                        break
@@ -106,9 +107,14 @@ func sweepone() uintptr {
                        }
                        continue
                }
-               if s.sweepgen != sg-2 || !atomic.Cas(&s.sweepgen, sg-2, sg-1) {
-                       continue
+               if s.sweepgen == sg-2 && atomic.Cas(&s.sweepgen, sg-2, sg-1) {
+                       break
                }
+       }
+
+       // Sweep the span we found.
+       npages := ^uintptr(0)
+       if s != nil {
                npages = s.npages
                if !s.sweep(false) {
                        // Span is still in-use, so this returned no
@@ -116,7 +122,6 @@ func sweepone() uintptr {
                        // move to the swept in-use list.
                        npages = 0
                }
-               break
        }
 
        // Decrement the number of active sweepers and if this is the