aboutsummaryrefslogtreecommitdiff
path: root/src/time/tick.go
AgeCommit message (Collapse)Author
2026-03-03time: mention Reset in Ticker.Stop docIan Lance Taylor
Fixes #72968 Change-Id: Ia7bab559738ff66e19a030e031c157d344a35036 Reviewed-on: https://go-review.googlesource.com/c/go/+/659695 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-04-04time: add available godoc linkcui fliter
Change-Id: Idfe9cf2f2e4750d6673455f98deef2efc2d292a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/539837 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org>
2024-03-13time: garbage collect unstopped Tickers and TimersRuss Cox
From the beginning of Go, the time package has had a gotcha: if you use a select on <-time.After(1*time.Minute), even if the select finishes immediately because some other case is ready, the underlying timer from time.After keeps running until the minute is over. This pins the timer in the timer heap, which keeps it from being garbage collected and in extreme cases also slows down timer operations. The lack of garbage collection is the more important problem. The docs for After warn against this scenario and suggest using NewTimer with a call to Stop after the select instead, purely to work around this garbage collection problem. Oddly, the docs for NewTimer and NewTicker do not mention this problem, but they have the same issue: they cannot be collected until either they are Stopped or, in the case of Timer, the timer expires. (Tickers repeat, so they never expire.) People have built up a shared knowledge that timers and tickers need to defer t.Stop even though the docs do not mention this (it is somewhat implied by the After docs). This CL fixes the garbage collection problem, so that a timer that is unreferenced can be GC'ed immediately, even if it is still running. The approach is to only insert the timer into the heap when some channel operation is blocked on it; the last channel operation to stop using the timer takes it back out of the heap. When a timer's channel is no longer referenced, there are no channel operations blocked on it, so it's not in the heap, so it can be GC'ed immediately. This CL adds an undocumented GODEBUG asynctimerchan=1 that will disable the change. The documentation happens in the CL 568341. Fixes #8898. Fixes #61542. Change-Id: Ieb303b6de1fb3527d3256135151a9e983f3c27e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/512355 Reviewed-by: Austin Clements <austin@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Russ Cox <rsc@golang.org>
2024-03-13runtime: clean up timer stateRuss Cox
The timers had evolved to the point where the state was stored as follows: if timer in heap: state has timerHeaped set if heap timer is stale: heap deadline in t.when real deadline in t.nextWhen state has timerNextWhen set else: real deadline in t.when t.nextWhen unset else: real deadline in t.when t.nextWhen unset That made it hard to find the real deadline and just hard to think about everything. The new state is: real deadline in t.when (always) if timer in heap: state has timerHeaped set heap deadline in t.whenHeap if heap timer is stale: state has timerModified set Separately, the 'state' word itself was being used as a lock and state bits because the code started with CAS loops, which we abstracted into the lock/unlock methods step by step. At this point, we can switch to a real lock, making sure to publish the one boolean needed by timers fast paths at each unlock. All this simplifies various logic considerably. Change-Id: I35766204f7a26d999206bd56cc0db60ad1b17cbe Reviewed-on: https://go-review.googlesource.com/c/go/+/570335 Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2024-03-09time: move runtimeTimer out of Timer structRuss Cox
If user code has two timers t1 and t2 and does *t1 = *t2 (or *t1 = Timer{}), it creeps me out that we would be corrupting the runtime data structures inlined in the Timer struct. Replace that field with a pointer to the runtime data structure instead, so that the corruption cannot happen, even in a badly behaved program. In fact, remove the struct definition entirely and linkname a constructor instead. Now the runtime can evolve the struct however it likes without needing to keep package time in sync. Also move the workaround logic for #21874 out of runtime and into package time. Change-Id: Ia30f7802ee7b3a11f5d8a78dd30fd9c8633dc787 Reviewed-on: https://go-review.googlesource.com/c/go/+/568339 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-28runtime: delete addtimerRuss Cox
modtimer can always be used in place of addtimer. Do that and delete addtimer, avoiding duplicated logic. [This is one CL in a refactoring stack making very small changes in each step, so that any subtle bugs that we miss can be more easily pinpointed to a small change.] Change-Id: I70291796bdac3bef5e0850f039f6f4a1da4498ae Reviewed-on: https://go-review.googlesource.com/c/go/+/564118 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-01-20time: no need to wrap error with errors.Newfangguizhen
Change-Id: Ibd519ed6419f8f21c89a111a0326d0788aca9d19 GitHub-Last-Rev: 45e3224f9afef57f49eebf3cbdf6b1d01cfd7346 GitHub-Pull-Request: golang/go#57819 Reviewed-on: https://go-review.googlesource.com/c/go/+/462046 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-04-05all: replace `` and '' with “ (U+201C) and ” (U+201D) in doc commentsRuss Cox
go/doc in all its forms applies this replacement when rendering the comments. We are considering formatting doc comments, including doing this replacement as part of the formatting. Apply it to our source files ahead of time. For #51082. Change-Id: Ifcc1f5861abb57c5d14e7d8c2102dfb31b7a3a19 Reviewed-on: https://go-review.googlesource.com/c/go/+/384262 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-11-05time: make Ticker.Reset(0) paniczhouguangyuan
Fixes #49315 Change-Id: I0887bad1059b25ae0749bfa1ed6ddccbecca7951 Reviewed-on: https://go-review.googlesource.com/c/go/+/361074 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
2021-08-16time: update current time commentastraw99
In the time package, the ticker and timer both send current time to channel C, so this PR update the comment to understand them better. Change-Id: I99846a40bf8ef780bf0062dd84cf721b3b892a1b GitHub-Last-Rev: 535da54b8ebd25be22289699212364df0aa49c7f GitHub-Pull-Request: golang/go#47597 Reviewed-on: https://go-review.googlesource.com/c/go/+/340649 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dmitri Shuralyov <dmitshur@golang.org>
2020-11-18time: in NewTicker, document that the 1st tick comes after dAlberto Donizetti
Fixes #42245 Change-Id: I3b298ab6be65569389873d68bd3c6e49cf892c69 Reviewed-on: https://go-review.googlesource.com/c/go/+/265818 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Rob Pike <r@golang.org> Trust: Alberto Donizetti <alb.donizetti@gmail.com>
2020-10-23all: fix quoting for compress/bzip2 and time's godocsubham sarkar
The existing usage of grave accent (`) and apostrophe (') at some places made godoc to ignore them and show it as it is. So, use both of the characters twice (consecutively) so that godoc can convert it to {left,right} double quotation mark. Fixes #41958 Change-Id: I64fd9b5fa34f416ad595009d09f5482e10bd8b4f Reviewed-on: https://go-review.googlesource.com/c/go/+/262397 Reviewed-by: Russ Cox <rsc@golang.org> Trust: Alberto Donizetti <alb.donizetti@gmail.com>
2020-02-24time: add Ticker.ResetChangkun Ou
This CL implements Ticker.Reset method in time package. Benchmark: name time/op TickerReset-12 6.41µs ±10% TickerResetNaive-12 95.7µs ±12% Fixes #33184 Change-Id: I4cbd31796efa012b2a297bb342158f11a4a31fef Reviewed-on: https://go-review.googlesource.com/c/go/+/220424 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-23Revert "time: add Ticker.Reset"Andrew Bonventre
This reverts CL 217362 (6e5652bebede2d53484a872f6d1dfeb498b0b50c.) Reason for revert: Causing failures on arm64 bots. See #33184 for more info Change-Id: I72ba40047e4138767d95aaa68842893c3508c52f Reviewed-on: https://go-review.googlesource.com/c/go/+/220638 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-23time: add Ticker.ResetChangkun Ou
This CL implements Ticker.Reset method in time package. Benchmark: name time/op TickerReset-12 6.41µs ±10% TickerResetNaive-12 95.7µs ±12% Fixes #33184 Change-Id: I12c651f81e452541bcbbc748b45f038aae1f8dae Reviewed-on: https://go-review.googlesource.com/c/go/+/217362 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-07-11time: add clarification to Timer.Stop documentationCaleb Martinez
Fixes #26220 Change-Id: I05703912be594d985ad3ccb3a9757ae21ec738ab GitHub-Last-Rev: e4649847e015e573ee169c7d6db29b9437841afd GitHub-Pull-Request: golang/go#26293 Reviewed-on: https://go-review.googlesource.com/122715 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-02all: single space after period.Brad Fitzpatrick
The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-29time: document Tick will return nil if d <= 0Shenghou Ma
Fixes #14557. Change-Id: I9610b79aafe9c15f9c998739b586fd0b41b90d70 Reviewed-on: https://go-review.googlesource.com/20031 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-07-13time: document that time.Tick creates an unrecoverable resourceRob Pike
Fixes #11662. Change-Id: I8839b48a1cd3da4c29b6def03cb0c74c1c12942e Reviewed-on: https://go-review.googlesource.com/12066 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2014-09-08build: move package sources from src/pkg to srcRuss Cox
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.