]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: don't send preemption signal if there is a signal pending
authorCherry Zhang <cherryyz@google.com>
Tue, 17 Mar 2020 00:08:00 +0000 (20:08 -0400)
committerCherry Zhang <cherryyz@google.com>
Wed, 18 Mar 2020 16:00:44 +0000 (16:00 +0000)
If multiple threads call preemptone to preempt the same M, it may
send many signals to the same M such that it hardly make
progress, causing live-lock problem. Only send a signal if there
isn't already one pending.

Fixes #37741.

Change-Id: Id94adb0b95acbd18b23abe637a8dcd81ab41b452
Reviewed-on: https://go-review.googlesource.com/c/go/+/223737
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/runtime2.go
src/runtime/signal_unix.go

index 9e3ccb2e40442aa6c529af134efd712df959a70c..1a98927647d4c82f40a281307ec36c223facb3ac 100644 (file)
@@ -539,6 +539,10 @@ type m struct {
        // requested, but fails. Accessed atomically.
        preemptGen uint32
 
+       // Whether this is a pending preemption signal on this M.
+       // Accessed atomically.
+       signalPending uint32
+
        dlogPerM
 
        mOS
index 32b192c97770e7801c19b3e1151fcf0813b172ec..b8f27d1147bc38d85e5fef61e6c659aa3e8cda06 100644 (file)
@@ -333,6 +333,7 @@ func doSigPreempt(gp *g, ctxt *sigctxt) {
 
        // Acknowledge the preemption.
        atomic.Xadd(&gp.m.preemptGen, 1)
+       atomic.Store(&gp.m.signalPending, 0)
 }
 
 const preemptMSupported = pushCallSupported
@@ -359,7 +360,14 @@ func preemptM(mp *m) {
                // required).
                return
        }
-       signalM(mp, sigPreempt)
+       if atomic.Cas(&mp.signalPending, 0, 1) {
+               // If multiple threads are preempting the same M, it may send many
+               // signals to the same M such that it hardly make progress, causing
+               // live-lock problem. Apparently this could happen on darwin. See
+               // issue #37741.
+               // Only send a signal if there isn't already one pending.
+               signalM(mp, sigPreempt)
+       }
 }
 
 // sigFetchG fetches the value of G safely when running in a signal handler.