]> Cypherpunks repositories - gostls13.git/commitdiff
time: clarify when draining a Timer's channel is needed
authorAllen Li <ayatane@google.com>
Thu, 11 Jul 2019 00:57:57 +0000 (00:57 +0000)
committerRob Pike <r@golang.org>
Thu, 18 Jul 2019 20:43:28 +0000 (20:43 +0000)
Updates #27169

Change-Id: I22a6194c06529ba70b1ec648e3188c191224e321
GitHub-Last-Rev: 457b2a61a8ee4a745371c87756e5b6f38058af28
GitHub-Pull-Request: golang/go#32996
Reviewed-on: https://go-review.googlesource.com/c/go/+/185245
Reviewed-by: Rob Pike <r@golang.org>
src/time/sleep.go

index 10edf6fe0e0dd70ef866fe99e2f0bf98d3912ac1..2cc908da55e4fa767e37be542a857207ee4fcdbe 100644 (file)
@@ -54,8 +54,8 @@ type Timer struct {
 // Stop does not close the channel, to prevent a read from the channel succeeding
 // incorrectly.
 //
-// To prevent a timer created with NewTimer from firing after a call to Stop,
-// check the return value and drain the channel.
+// To ensure the channel is empty after a call to Stop, check the
+// return value and drain the channel.
 // For example, assuming the program has not received from t.C already:
 //
 //     if !t.Stop() {
@@ -97,10 +97,9 @@ func NewTimer(d Duration) *Timer {
 // It returns true if the timer had been active, false if the timer had
 // expired or been stopped.
 //
-// Resetting a timer must take care not to race with the send into t.C
-// that happens when the current timer expires.
+// Reset should be invoked only on stopped or expired timers with drained channels.
 // If a program has already received a value from t.C, the timer is known
-// to have expired, and t.Reset can be used directly.
+// to have expired and the channel drained, so t.Reset can be used directly.
 // If a program has not yet received a value from t.C, however,
 // the timer must be stopped and—if Stop reports that the timer expired
 // before being stopped—the channel explicitly drained: