Source file src/runtime/time.go
1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Time-related runtime and pieces of package time. 6 7 package runtime 8 9 import ( 10 "internal/abi" 11 "internal/runtime/atomic" 12 "internal/runtime/sys" 13 "unsafe" 14 ) 15 16 //go:linkname time_runtimeNow time.runtimeNow 17 func time_runtimeNow() (sec int64, nsec int32, mono int64) { 18 if bubble := getg().bubble; bubble != nil { 19 sec = bubble.now / (1000 * 1000 * 1000) 20 nsec = int32(bubble.now % (1000 * 1000 * 1000)) 21 // Don't return a monotonic time inside a synctest bubble. 22 // If we return a monotonic time based on the fake clock, 23 // arithmetic on times created inside/outside bubbles is confusing. 24 // If we return a monotonic time based on the real monotonic clock, 25 // arithmetic on times created in the same bubble is confusing. 26 // Simplest is to omit the monotonic time within a bubble. 27 return sec, nsec, 0 28 } 29 return time_now() 30 } 31 32 //go:linkname crypto_internal_fips140deps_time_monoTime crypto/internal/fips140deps/time.monoTime 33 func crypto_internal_fips140deps_time_monoTime() (mono int64) { 34 _, _, mono = time_now() 35 return mono 36 } 37 38 //go:linkname time_runtimeNano time.runtimeNano 39 func time_runtimeNano() int64 { 40 gp := getg() 41 if gp.bubble != nil { 42 return gp.bubble.now 43 } 44 return nanotime() 45 } 46 47 //go:linkname time_runtimeIsBubbled time.runtimeIsBubbled 48 func time_runtimeIsBubbled() bool { 49 return getg().bubble != nil 50 } 51 52 // A timer is a potentially repeating trigger for calling t.f(t.arg, t.seq). 53 // Timers are allocated by client code, often as part of other data structures. 54 // Each P has a heap of pointers to timers that it manages. 55 // 56 // A timer is expected to be used by only one client goroutine at a time, 57 // but there will be concurrent access by the P managing that timer. 58 // Timer accesses are protected by the lock t.mu, with a snapshot of 59 // t's state bits published in t.astate to enable certain fast paths to make 60 // decisions about a timer without acquiring the lock. 61 type timer struct { 62 // mu protects reads and writes to all fields, with exceptions noted below. 63 mu mutex 64 65 astate atomic.Uint8 // atomic copy of state bits at last unlock 66 state uint8 // state bits 67 isChan bool // timer has a channel; immutable; can be read without lock 68 isFake bool // timer is using fake time; immutable; can be read without lock 69 70 blocked uint32 // number of goroutines blocked on timer's channel 71 rand uint32 // randomizes order of timers at same instant; only set when isFake 72 73 // Timer wakes up at when, and then at when+period, ... (period > 0 only) 74 // each time calling f(arg, seq, delay) in the timer goroutine, so f must be 75 // a well-behaved function and not block. 76 // 77 // The arg and seq are client-specified opaque arguments passed back to f. 78 // When used from netpoll, arg and seq have meanings defined by netpoll 79 // and are completely opaque to this code; in that context, seq is a sequence 80 // number to recognize and squelch stale function invocations. 81 // When used from package time, arg is a channel (for After, NewTicker) 82 // or the function to call (for AfterFunc) and seq is unused (0). 83 // 84 // Package time does not know about seq, but if this is a channel timer (t.isChan == true), 85 // this file uses t.seq as a sequence number to recognize and squelch 86 // sends that correspond to an earlier (stale) timer configuration, 87 // similar to its use in netpoll. In this usage (that is, when t.isChan == true), 88 // writes to seq are protected by both t.mu and t.sendLock, 89 // so reads are allowed when holding either of the two mutexes. 90 // 91 // The delay argument is nanotime() - t.when, meaning the delay in ns between 92 // when the timer should have gone off and now. Normally that amount is 93 // small enough not to matter, but for channel timers that are fed lazily, 94 // the delay can be arbitrarily long; package time subtracts it out to make 95 // it look like the send happened earlier than it actually did. 96 // (No one looked at the channel since then, or the send would have 97 // not happened so late, so no one can tell the difference.) 98 when int64 99 period int64 100 f func(arg any, seq uintptr, delay int64) 101 arg any 102 seq uintptr 103 104 // If non-nil, the timers containing t. 105 ts *timers 106 107 // sendLock protects sends on the timer's channel. 108 sendLock mutex 109 110 // isSending is used to handle races between running a 111 // channel timer and stopping or resetting the timer. 112 // It is used only for channel timers (t.isChan == true). 113 // It is not used for tickers. 114 // The value is incremented when about to send a value on the channel, 115 // and decremented after sending the value. 116 // The stop/reset code uses this to detect whether it 117 // stopped the channel send. 118 // 119 // isSending is incremented only when t.mu is held. 120 // isSending is decremented only when t.sendLock is held. 121 // isSending is read only when both t.mu and t.sendLock are held. 122 isSending atomic.Int32 123 } 124 125 // init initializes a newly allocated timer t. 126 // Any code that allocates a timer must call t.init before using it. 127 // The arg and f can be set during init, or they can be nil in init 128 // and set by a future call to t.modify. 129 func (t *timer) init(f func(arg any, seq uintptr, delay int64), arg any) { 130 lockInit(&t.mu, lockRankTimer) 131 t.f = f 132 t.arg = arg 133 } 134 135 // A timers is a per-P set of timers. 136 type timers struct { 137 // mu protects timers; timers are per-P, but the scheduler can 138 // access the timers of another P, so we have to lock. 139 mu mutex 140 141 // heap is the set of timers, ordered by heap[i].when. 142 // Must hold lock to access. 143 heap []timerWhen 144 145 // len is an atomic copy of len(heap). 146 len atomic.Uint32 147 148 // zombies is the number of timers in the heap 149 // that are marked for removal. 150 zombies atomic.Int32 151 152 // raceCtx is the race context used while executing timer functions. 153 raceCtx uintptr 154 155 // minWhenHeap is the minimum heap[i].when value (= heap[0].when). 156 // The wakeTime method uses minWhenHeap and minWhenModified 157 // to determine the next wake time. 158 // If minWhenHeap = 0, it means there are no timers in the heap. 159 minWhenHeap atomic.Int64 160 161 // minWhenModified is a lower bound on the minimum 162 // heap[i].when over timers with the timerModified bit set. 163 // If minWhenModified = 0, it means there are no timerModified timers in the heap. 164 minWhenModified atomic.Int64 165 } 166 167 type timerWhen struct { 168 timer *timer 169 when int64 170 } 171 172 // less reports whether tw is less than other. 173 func (tw timerWhen) less(other timerWhen) bool { 174 switch { 175 case tw.when < other.when: 176 return true 177 case tw.when > other.when: 178 return false 179 default: 180 // When timers wake at the same time, use a per-timer random value to order them. 181 // We only set the random value for timers using fake time, since there's 182 // no practical way to schedule real-time timers for the same instant. 183 return tw.timer.rand < other.timer.rand 184 } 185 } 186 187 func (ts *timers) lock() { 188 lock(&ts.mu) 189 } 190 191 func (ts *timers) unlock() { 192 // Update atomic copy of len(ts.heap). 193 // We only update at unlock so that the len is always 194 // the most recent unlocked length, not an ephemeral length. 195 // This matters if we lock ts, delete the only timer from the heap, 196 // add it back, and unlock. We want ts.len.Load to return 1 the 197 // entire time, never 0. This is important for pidleput deciding 198 // whether ts is empty. 199 ts.len.Store(uint32(len(ts.heap))) 200 201 unlock(&ts.mu) 202 } 203 204 // Timer state field. 205 const ( 206 // timerHeaped is set when the timer is stored in some P's heap. 207 timerHeaped uint8 = 1 << iota 208 209 // timerModified is set when t.when has been modified 210 // but the heap's heap[i].when entry still needs to be updated. 211 // That change waits until the heap in which 212 // the timer appears can be locked and rearranged. 213 // timerModified is only set when timerHeaped is also set. 214 timerModified 215 216 // timerZombie is set when the timer has been stopped 217 // but is still present in some P's heap. 218 // Only set when timerHeaped is also set. 219 // It is possible for timerModified and timerZombie to both 220 // be set, meaning that the timer was modified and then stopped. 221 // A timer sending to a channel may be placed in timerZombie 222 // to take it out of the heap even though the timer is not stopped, 223 // as long as nothing is reading from the channel. 224 timerZombie 225 ) 226 227 // timerDebug enables printing a textual debug trace of all timer operations to stderr. 228 const timerDebug = false 229 230 func (t *timer) trace(op string) { 231 if timerDebug { 232 t.trace1(op) 233 } 234 } 235 236 func (t *timer) trace1(op string) { 237 if !timerDebug { 238 return 239 } 240 bits := [4]string{"h", "m", "z", "c"} 241 for i := range 3 { 242 if t.state&(1<<i) == 0 { 243 bits[i] = "-" 244 } 245 } 246 if !t.isChan { 247 bits[3] = "-" 248 } 249 print("T ", t, " ", bits[0], bits[1], bits[2], bits[3], " b=", t.blocked, " ", op, "\n") 250 } 251 252 func (ts *timers) trace(op string) { 253 if timerDebug { 254 println("TS", ts, op) 255 } 256 } 257 258 // lock locks the timer, allowing reading or writing any of the timer fields. 259 func (t *timer) lock() { 260 lock(&t.mu) 261 t.trace("lock") 262 } 263 264 // unlock updates t.astate and unlocks the timer. 265 func (t *timer) unlock() { 266 t.trace("unlock") 267 // Let heap fast paths know whether heap[i].when is accurate. 268 // Also let maybeRunChan know whether channel is in heap. 269 t.astate.Store(t.state) 270 unlock(&t.mu) 271 } 272 273 // hchan returns the channel in t.arg. 274 // t must be a timer with a channel. 275 func (t *timer) hchan() *hchan { 276 if !t.isChan { 277 badTimer() 278 } 279 // Note: t.arg is a chan time.Time, 280 // and runtime cannot refer to that type, 281 // so we cannot use a type assertion. 282 return (*hchan)(efaceOf(&t.arg).data) 283 } 284 285 // updateHeap updates t as directed by t.state, updating t.state 286 // and returning a bool indicating whether the state (and ts.heap[0].when) changed. 287 // The caller must hold t's lock, or the world can be stopped instead. 288 // The timer set t.ts must be non-nil and locked, t must be t.ts.heap[0], and updateHeap 289 // takes care of moving t within the timers heap to preserve the heap invariants. 290 // If ts == nil, then t must not be in a heap (or is in a heap that is 291 // temporarily not maintaining its invariant, such as during timers.adjust). 292 func (t *timer) updateHeap() (updated bool) { 293 assertWorldStoppedOrLockHeld(&t.mu) 294 t.trace("updateHeap") 295 ts := t.ts 296 if ts == nil || t != ts.heap[0].timer { 297 badTimer() 298 } 299 assertLockHeld(&ts.mu) 300 if t.state&timerZombie != 0 { 301 // Take timer out of heap. 302 t.state &^= timerHeaped | timerZombie | timerModified 303 ts.zombies.Add(-1) 304 ts.deleteMin() 305 return true 306 } 307 308 if t.state&timerModified != 0 { 309 // Update ts.heap[0].when and move within heap. 310 t.state &^= timerModified 311 ts.heap[0].when = t.when 312 ts.siftDown(0) 313 ts.updateMinWhenHeap() 314 return true 315 } 316 317 return false 318 } 319 320 // maxWhen is the maximum value for timer's when field. 321 const maxWhen = 1<<63 - 1 322 323 // verifyTimers can be set to true to add debugging checks that the 324 // timer heaps are valid. 325 const verifyTimers = false 326 327 // Package time APIs. 328 // Godoc uses the comments in package time, not these. 329 330 // time.now is implemented in assembly. 331 332 // timeSleep puts the current goroutine to sleep for at least ns nanoseconds. 333 // 334 //go:linkname timeSleep time.Sleep 335 func timeSleep(ns int64) { 336 if ns <= 0 { 337 return 338 } 339 340 gp := getg() 341 t := gp.timer 342 if t == nil { 343 t = new(timer) 344 t.init(goroutineReady, gp) 345 if gp.bubble != nil { 346 t.isFake = true 347 } 348 gp.timer = t 349 } 350 var now int64 351 if bubble := gp.bubble; bubble != nil { 352 now = bubble.now 353 } else { 354 now = nanotime() 355 } 356 when := now + ns 357 if when < 0 { // check for overflow. 358 when = maxWhen 359 } 360 gp.sleepWhen = when 361 if t.isFake { 362 // Call timer.reset in this goroutine, since it's the one in a bubble. 363 // We don't need to worry about the timer function running before the goroutine 364 // is parked, because time won't advance until we park. 365 resetForSleep(gp, nil) 366 gopark(nil, nil, waitReasonSleep, traceBlockSleep, 1) 367 } else { 368 gopark(resetForSleep, nil, waitReasonSleep, traceBlockSleep, 1) 369 } 370 } 371 372 // resetForSleep is called after the goroutine is parked for timeSleep. 373 // We can't call timer.reset in timeSleep itself because if this is a short 374 // sleep and there are many goroutines then the P can wind up running the 375 // timer function, goroutineReady, before the goroutine has been parked. 376 func resetForSleep(gp *g, _ unsafe.Pointer) bool { 377 gp.timer.reset(gp.sleepWhen, 0) 378 return true 379 } 380 381 // A timeTimer is a runtime-allocated time.Timer or time.Ticker 382 // with the additional runtime state following it. 383 // The runtime state is inaccessible to package time. 384 type timeTimer struct { 385 c unsafe.Pointer // <-chan time.Time 386 self *timeTimer // pointer to self, used by time to detect bad initialization 387 timer 388 } 389 390 // newTimer allocates and returns a new time.Timer or time.Ticker (same layout) 391 // with the given parameters. 392 // 393 //go:linkname newTimer time.newTimer 394 func newTimer(when, period int64, f func(arg any, seq uintptr, delay int64), arg any, c *hchan) *timeTimer { 395 t := new(timeTimer) 396 t.timer.init(nil, nil) 397 t.trace("new") 398 if raceenabled { 399 racerelease(unsafe.Pointer(&t.timer)) 400 } 401 if c != nil { 402 lockInit(&t.sendLock, lockRankTimerSend) 403 t.isChan = true 404 c.timer = &t.timer 405 if c.dataqsiz == 0 { 406 throw("invalid timer channel: no capacity") 407 } 408 } 409 if bubble := getg().bubble; bubble != nil { 410 t.isFake = true 411 } 412 t.modify(when, period, f, arg, 0) 413 t.self = t 414 return t 415 } 416 417 // stopTimer stops a timer. 418 // It reports whether t was stopped before being run. 419 // 420 //go:linkname stopTimer time.stopTimer 421 func stopTimer(t *timeTimer) bool { 422 if t.isFake && getg().bubble == nil { 423 fatal("stop of synctest timer from outside bubble") 424 } 425 return t.stop() 426 } 427 428 // resetTimer resets an inactive timer, adding it to the timer heap. 429 // 430 // Reports whether the timer was modified before it was run. 431 // 432 //go:linkname resetTimer time.resetTimer 433 func resetTimer(t *timeTimer, when, period int64) bool { 434 if raceenabled { 435 racerelease(unsafe.Pointer(&t.timer)) 436 } 437 if t.isFake && getg().bubble == nil { 438 fatal("reset of synctest timer from outside bubble") 439 } 440 return t.reset(when, period) 441 } 442 443 // Go runtime. 444 445 // Ready the goroutine arg. 446 func goroutineReady(arg any, _ uintptr, _ int64) { 447 goready(arg.(*g), 0) 448 } 449 450 // addHeap adds t to the timers heap. 451 // The caller must hold ts.lock or the world must be stopped. 452 // The caller must also have checked that t belongs in the heap. 453 // Callers that are not sure can call t.maybeAdd instead, 454 // but note that maybeAdd has different locking requirements. 455 func (ts *timers) addHeap(t *timer) { 456 assertWorldStoppedOrLockHeld(&ts.mu) 457 // Timers rely on the network poller, so make sure the poller 458 // has started. 459 if netpollInited.Load() == 0 { 460 netpollGenericInit() 461 } 462 463 if t.ts != nil { 464 throw("ts set in timer") 465 } 466 t.ts = ts 467 ts.heap = append(ts.heap, timerWhen{t, t.when}) 468 ts.siftUp(len(ts.heap) - 1) 469 if t == ts.heap[0].timer { 470 ts.updateMinWhenHeap() 471 } 472 } 473 474 // stop stops the timer t. It may be on some other P, so we can't 475 // actually remove it from the timers heap. We can only mark it as stopped. 476 // It will be removed in due course by the P whose heap it is on. 477 // Reports whether the timer was stopped before it was run. 478 func (t *timer) stop() bool { 479 if t.isChan { 480 lock(&t.sendLock) 481 } 482 483 t.lock() 484 t.trace("stop") 485 if t.state&timerHeaped != 0 { 486 t.state |= timerModified 487 if t.state&timerZombie == 0 { 488 t.state |= timerZombie 489 t.ts.zombies.Add(1) 490 } 491 } 492 pending := t.when > 0 493 t.when = 0 494 495 if t.isChan { 496 // Stop any future sends with stale values. 497 // See timer.unlockAndRun. 498 t.seq++ 499 500 // If there is currently a send in progress, 501 // incrementing seq is going to prevent that 502 // send from actually happening. That means 503 // that we should return true: the timer was 504 // stopped, even though t.when may be zero. 505 if t.period == 0 && t.isSending.Load() > 0 { 506 pending = true 507 } 508 } 509 t.unlock() 510 if t.isChan { 511 unlock(&t.sendLock) 512 if timerchandrain(t.hchan()) { 513 pending = true 514 } 515 } 516 517 return pending 518 } 519 520 // deleteMin removes timer 0 from ts. 521 // ts must be locked. 522 func (ts *timers) deleteMin() { 523 assertLockHeld(&ts.mu) 524 t := ts.heap[0].timer 525 if t.ts != ts { 526 throw("wrong timers") 527 } 528 t.ts = nil 529 last := len(ts.heap) - 1 530 if last > 0 { 531 ts.heap[0] = ts.heap[last] 532 } 533 ts.heap[last] = timerWhen{} 534 ts.heap = ts.heap[:last] 535 if last > 0 { 536 ts.siftDown(0) 537 } 538 ts.updateMinWhenHeap() 539 if last == 0 { 540 // If there are no timers, then clearly there are no timerModified timers. 541 ts.minWhenModified.Store(0) 542 } 543 } 544 545 // modify modifies an existing timer. 546 // This is called by the netpoll code or time.Ticker.Reset or time.Timer.Reset. 547 // Reports whether the timer was modified before it was run. 548 // If f == nil, then t.f, t.arg, and t.seq are not modified. 549 func (t *timer) modify(when, period int64, f func(arg any, seq uintptr, delay int64), arg any, seq uintptr) bool { 550 if when <= 0 { 551 throw("timer when must be positive") 552 } 553 if period < 0 { 554 throw("timer period must be non-negative") 555 } 556 557 if t.isChan { 558 lock(&t.sendLock) 559 } 560 561 t.lock() 562 t.trace("modify") 563 oldPeriod := t.period 564 t.period = period 565 if f != nil { 566 t.f = f 567 t.arg = arg 568 t.seq = seq 569 } 570 571 wake := false 572 pending := t.when > 0 573 t.when = when 574 if t.state&timerHeaped != 0 { 575 t.state |= timerModified 576 if t.state&timerZombie != 0 { 577 // In the heap but marked for removal (by a Stop). 578 // Unmark it, since it has been Reset and will be running again. 579 t.ts.zombies.Add(-1) 580 t.state &^= timerZombie 581 } 582 // The corresponding heap[i].when is updated later. 583 // See comment in type timer above and in timers.adjust below. 584 if min := t.ts.minWhenModified.Load(); min == 0 || when < min { 585 wake = true 586 // Force timerModified bit out to t.astate before updating t.minWhenModified, 587 // to synchronize with t.ts.adjust. See comment in adjust. 588 t.astate.Store(t.state) 589 t.ts.updateMinWhenModified(when) 590 } 591 } 592 593 add := t.needsAdd() 594 595 if add && t.isFake { 596 // If this is a bubbled timer scheduled to fire immediately, 597 // run it now rather than waiting for the bubble's timer scheduler. 598 // This avoids deferring timer execution until after the bubble 599 // becomes durably blocked. 600 // 601 // Don't do this for non-bubbled timers: It isn't necessary, 602 // and there may be cases where the runtime executes timers with 603 // the expectation the timer func will not run in the current goroutine. 604 // Bubbled timers are always created by the time package, and are 605 // safe to run in the current goroutine. 606 bubble := getg().bubble 607 if bubble == nil { 608 throw("fake timer executing with no bubble") 609 } 610 if t.state&timerHeaped == 0 && when <= bubble.now { 611 systemstack(func() { 612 if t.isChan { 613 unlock(&t.sendLock) 614 } 615 t.unlockAndRun(bubble.now, bubble) 616 }) 617 return pending 618 } 619 } 620 621 if t.isChan { 622 // Stop any future sends with stale values. 623 // See timer.unlockAndRun. 624 t.seq++ 625 626 // If there is currently a send in progress, 627 // incrementing seq is going to prevent that 628 // send from actually happening. That means 629 // that we should return true: the timer was 630 // stopped, even though t.when may be zero. 631 if oldPeriod == 0 && t.isSending.Load() > 0 { 632 pending = true 633 } 634 } 635 t.unlock() 636 if t.isChan { 637 if timerchandrain(t.hchan()) { 638 pending = true 639 } 640 unlock(&t.sendLock) 641 } 642 643 if add { 644 t.maybeAdd() 645 } 646 if wake { 647 wakeNetPoller(when) 648 } 649 650 return pending 651 } 652 653 // needsAdd reports whether t needs to be added to a timers heap. 654 // t must be locked. 655 func (t *timer) needsAdd() bool { 656 assertLockHeld(&t.mu) 657 need := t.state&timerHeaped == 0 && t.when > 0 && (!t.isChan || t.blocked > 0) 658 if need { 659 t.trace("needsAdd+") 660 } else { 661 t.trace("needsAdd-") 662 } 663 return need 664 } 665 666 // maybeAdd adds t to the local timers heap if it needs to be in a heap. 667 // The caller must not hold t's lock nor any timers heap lock. 668 // The caller probably just unlocked t, but that lock must be dropped 669 // in order to acquire a ts.lock, to avoid lock inversions. 670 // (timers.adjust holds ts.lock while acquiring each t's lock, 671 // so we cannot hold any t's lock while acquiring ts.lock). 672 // 673 // Strictly speaking it *might* be okay to hold t.lock and 674 // acquire ts.lock at the same time, because we know that 675 // t is not in any ts.heap, so nothing holding a ts.lock would 676 // be acquiring the t.lock at the same time, meaning there 677 // isn't a possible deadlock. But it is easier and safer not to be 678 // too clever and respect the static ordering. 679 // (If we don't, we have to change the static lock checking of t and ts.) 680 // 681 // Concurrent calls to time.Timer.Reset or blockTimerChan 682 // may result in concurrent calls to t.maybeAdd, 683 // so we cannot assume that t is not in a heap on entry to t.maybeAdd. 684 func (t *timer) maybeAdd() { 685 // Note: Not holding any locks on entry to t.maybeAdd, 686 // so the current g can be rescheduled to a different M and P 687 // at any time, including between the ts := assignment and the 688 // call to ts.lock. If a reschedule happened then, we would be 689 // adding t to some other P's timers, perhaps even a P that the scheduler 690 // has marked as idle with no timers, in which case the timer could 691 // go unnoticed until long after t.when. 692 // Calling acquirem instead of using getg().m makes sure that 693 // we end up locking and inserting into the current P's timers. 694 mp := acquirem() 695 var ts *timers 696 if t.isFake { 697 bubble := getg().bubble 698 if bubble == nil { 699 throw("invalid timer: fake time but no syncgroup") 700 } 701 ts = &bubble.timers 702 } else { 703 ts = &mp.p.ptr().timers 704 } 705 ts.lock() 706 ts.cleanHead() 707 t.lock() 708 t.trace("maybeAdd") 709 when := int64(0) 710 wake := false 711 if t.needsAdd() { 712 if t.isFake { 713 // Re-randomize timer order. 714 // We could do this for all timers, but unbubbled timers are highly 715 // unlikely to have the same when. 716 t.rand = cheaprand() 717 } 718 t.state |= timerHeaped 719 when = t.when 720 wakeTime := ts.wakeTime() 721 wake = wakeTime == 0 || when < wakeTime 722 ts.addHeap(t) 723 } 724 t.unlock() 725 ts.unlock() 726 releasem(mp) 727 if wake { 728 wakeNetPoller(when) 729 } 730 } 731 732 // reset resets the time when a timer should fire. 733 // If used for an inactive timer, the timer will become active. 734 // Reports whether the timer was active and was stopped. 735 func (t *timer) reset(when, period int64) bool { 736 return t.modify(when, period, nil, nil, 0) 737 } 738 739 // cleanHead cleans up the head of the timer queue. This speeds up 740 // programs that create and delete timers; leaving them in the heap 741 // slows down heap operations. 742 // The caller must have locked ts. 743 func (ts *timers) cleanHead() { 744 ts.trace("cleanHead") 745 assertLockHeld(&ts.mu) 746 gp := getg() 747 for { 748 if len(ts.heap) == 0 { 749 return 750 } 751 752 // This loop can theoretically run for a while, and because 753 // it is holding timersLock it cannot be preempted. 754 // If someone is trying to preempt us, just return. 755 // We can clean the timers later. 756 if gp.preemptStop { 757 return 758 } 759 760 // Delete zombies from tail of heap. It requires no heap adjustments at all, 761 // and doing so increases the chances that when we swap out a zombie 762 // in heap[0] for the tail of the heap, we'll get a non-zombie timer, 763 // shortening this loop. 764 n := len(ts.heap) 765 if t := ts.heap[n-1].timer; t.astate.Load()&timerZombie != 0 { 766 t.lock() 767 if t.state&timerZombie != 0 { 768 t.state &^= timerHeaped | timerZombie | timerModified 769 t.ts = nil 770 ts.zombies.Add(-1) 771 ts.heap[n-1] = timerWhen{} 772 ts.heap = ts.heap[:n-1] 773 } 774 t.unlock() 775 continue 776 } 777 778 t := ts.heap[0].timer 779 if t.ts != ts { 780 throw("bad ts") 781 } 782 783 if t.astate.Load()&(timerModified|timerZombie) == 0 { 784 // Fast path: head of timers does not need adjustment. 785 return 786 } 787 788 t.lock() 789 updated := t.updateHeap() 790 t.unlock() 791 if !updated { 792 // Head of timers does not need adjustment. 793 return 794 } 795 } 796 } 797 798 // take moves any timers from src into ts 799 // and then clears the timer state from src, 800 // because src is being destroyed. 801 // The caller must not have locked either timers. 802 // For now this is only called when the world is stopped. 803 func (ts *timers) take(src *timers) { 804 ts.trace("take") 805 assertWorldStopped() 806 if len(src.heap) > 0 { 807 // The world is stopped, so we ignore the locking of ts and src here. 808 // That would introduce a sched < timers lock ordering, 809 // which we'd rather avoid in the static ranking. 810 for _, tw := range src.heap { 811 t := tw.timer 812 t.ts = nil 813 if t.state&timerZombie != 0 { 814 t.state &^= timerHeaped | timerZombie | timerModified 815 } else { 816 t.state &^= timerModified 817 ts.addHeap(t) 818 } 819 } 820 src.heap = nil 821 src.zombies.Store(0) 822 src.minWhenHeap.Store(0) 823 src.minWhenModified.Store(0) 824 src.len.Store(0) 825 ts.len.Store(uint32(len(ts.heap))) 826 } 827 } 828 829 // adjust looks through the timers in ts.heap for 830 // any timers that have been modified to run earlier, and puts them in 831 // the correct place in the heap. While looking for those timers, 832 // it also moves timers that have been modified to run later, 833 // and removes deleted timers. The caller must have locked ts. 834 func (ts *timers) adjust(now int64, force bool) { 835 ts.trace("adjust") 836 assertLockHeld(&ts.mu) 837 // If we haven't yet reached the time of the earliest modified 838 // timer, don't do anything. This speeds up programs that adjust 839 // a lot of timers back and forth if the timers rarely expire. 840 // We'll postpone looking through all the adjusted timers until 841 // one would actually expire. 842 if !force { 843 first := ts.minWhenModified.Load() 844 if first == 0 || first > now { 845 if verifyTimers { 846 ts.verify() 847 } 848 return 849 } 850 } 851 852 // minWhenModified is a lower bound on the earliest t.when 853 // among the timerModified timers. We want to make it more precise: 854 // we are going to scan the heap and clean out all the timerModified bits, 855 // at which point minWhenModified can be set to 0 (indicating none at all). 856 // 857 // Other P's can be calling ts.wakeTime concurrently, and we'd like to 858 // keep ts.wakeTime returning an accurate value throughout this entire process. 859 // 860 // Setting minWhenModified = 0 *before* the scan could make wakeTime 861 // return an incorrect value: if minWhenModified < minWhenHeap, then clearing 862 // it to 0 will make wakeTime return minWhenHeap (too late) until the scan finishes. 863 // To avoid that, we want to set minWhenModified to 0 *after* the scan. 864 // 865 // Setting minWhenModified = 0 *after* the scan could result in missing 866 // concurrent timer modifications in other goroutines; those will lock 867 // the specific timer, set the timerModified bit, and set t.when. 868 // To avoid that, we want to set minWhenModified to 0 *before* the scan. 869 // 870 // The way out of this dilemma is to preserve wakeTime a different way. 871 // wakeTime is min(minWhenHeap, minWhenModified), and minWhenHeap 872 // is protected by ts.lock, which we hold, so we can modify it however we like 873 // in service of keeping wakeTime accurate. 874 // 875 // So we can: 876 // 877 // 1. Set minWhenHeap = min(minWhenHeap, minWhenModified) 878 // 2. Set minWhenModified = 0 879 // (Other goroutines may modify timers and update minWhenModified now.) 880 // 3. Scan timers 881 // 4. Set minWhenHeap = heap[0].when 882 // 883 // That order preserves a correct value of wakeTime throughout the entire 884 // operation: 885 // Step 1 “locks in” an accurate wakeTime even with minWhenModified cleared. 886 // Step 2 makes sure concurrent t.when updates are not lost during the scan. 887 // Step 3 processes all modified timer values, justifying minWhenModified = 0. 888 // Step 4 corrects minWhenHeap to a precise value. 889 // 890 // The wakeTime method implementation reads minWhenModified *before* minWhenHeap, 891 // so that if the minWhenModified is observed to be 0, that means the minWhenHeap that 892 // follows will include the information that was zeroed out of it. 893 // 894 // Originally Step 3 locked every timer, which made sure any timer update that was 895 // already in progress during Steps 1+2 completed and was observed by Step 3. 896 // All that locking was too expensive, so now we do an atomic load of t.astate to 897 // decide whether we need to do a full lock. To make sure that we still observe any 898 // timer update already in progress during Steps 1+2, t.modify sets timerModified 899 // in t.astate *before* calling t.updateMinWhenModified. That ensures that the 900 // overwrite in Step 2 cannot lose an update: if it does overwrite an update, Step 3 901 // will see the timerModified and do a full lock. 902 ts.minWhenHeap.Store(ts.wakeTime()) 903 ts.minWhenModified.Store(0) 904 905 changed := false 906 for i := 0; i < len(ts.heap); i++ { 907 tw := &ts.heap[i] 908 t := tw.timer 909 if t.ts != ts { 910 throw("bad ts") 911 } 912 913 if t.astate.Load()&(timerModified|timerZombie) == 0 { 914 // Does not need adjustment. 915 continue 916 } 917 918 t.lock() 919 switch { 920 case t.state&timerHeaped == 0: 921 badTimer() 922 923 case t.state&timerZombie != 0: 924 ts.zombies.Add(-1) 925 t.state &^= timerHeaped | timerZombie | timerModified 926 n := len(ts.heap) 927 ts.heap[i] = ts.heap[n-1] 928 ts.heap[n-1] = timerWhen{} 929 ts.heap = ts.heap[:n-1] 930 t.ts = nil 931 i-- 932 changed = true 933 934 case t.state&timerModified != 0: 935 tw.when = t.when 936 t.state &^= timerModified 937 changed = true 938 } 939 t.unlock() 940 } 941 942 if changed { 943 ts.initHeap() 944 } 945 ts.updateMinWhenHeap() 946 947 if verifyTimers { 948 ts.verify() 949 } 950 } 951 952 // wakeTime looks at ts's timers and returns the time when we 953 // should wake up the netpoller. It returns 0 if there are no timers. 954 // This function is invoked when dropping a P, so it must run without 955 // any write barriers. 956 // 957 //go:nowritebarrierrec 958 func (ts *timers) wakeTime() int64 { 959 // Note that the order of these two loads matters: 960 // adjust updates minWhen to make it safe to clear minNextWhen. 961 // We read minWhen after reading minNextWhen so that 962 // if we see a cleared minNextWhen, we are guaranteed to see 963 // the updated minWhen. 964 nextWhen := ts.minWhenModified.Load() 965 when := ts.minWhenHeap.Load() 966 if when == 0 || (nextWhen != 0 && nextWhen < when) { 967 when = nextWhen 968 } 969 return when 970 } 971 972 // check runs any timers in ts that are ready. 973 // If now is not 0 it is the current time. 974 // It returns the passed time or the current time if now was passed as 0. 975 // and the time when the next timer should run or 0 if there is no next timer, 976 // and reports whether it ran any timers. 977 // If the time when the next timer should run is not 0, 978 // it is always larger than the returned time. 979 // We pass now in and out to avoid extra calls of nanotime. 980 // 981 //go:yeswritebarrierrec 982 func (ts *timers) check(now int64, bubble *synctestBubble) (rnow, pollUntil int64, ran bool) { 983 ts.trace("check") 984 // If it's not yet time for the first timer, or the first adjusted 985 // timer, then there is nothing to do. 986 next := ts.wakeTime() 987 if next == 0 { 988 // No timers to run or adjust. 989 return now, 0, false 990 } 991 992 if now == 0 { 993 now = nanotime() 994 } 995 996 // If this is the local P, and there are a lot of deleted timers, 997 // clear them out. We only do this for the local P to reduce 998 // lock contention on timersLock. 999 zombies := ts.zombies.Load() 1000 if zombies < 0 { 1001 badTimer() 1002 } 1003 force := ts == &getg().m.p.ptr().timers && int(zombies) > int(ts.len.Load())/4 1004 1005 if now < next && !force { 1006 // Next timer is not ready to run, and we don't need to clear deleted timers. 1007 return now, next, false 1008 } 1009 1010 ts.lock() 1011 if len(ts.heap) > 0 { 1012 ts.adjust(now, false) 1013 for len(ts.heap) > 0 { 1014 // Note that runtimer may temporarily unlock ts. 1015 if tw := ts.run(now, bubble); tw != 0 { 1016 if tw > 0 { 1017 pollUntil = tw 1018 } 1019 break 1020 } 1021 ran = true 1022 } 1023 1024 // Note: Delaying the forced adjustment until after the ts.run 1025 // (as opposed to calling ts.adjust(now, force) above) 1026 // is significantly faster under contention, such as in 1027 // package time's BenchmarkTimerAdjust10000, 1028 // though we do not fully understand why. 1029 force = ts == &getg().m.p.ptr().timers && int(ts.zombies.Load()) > int(ts.len.Load())/4 1030 if force { 1031 ts.adjust(now, true) 1032 } 1033 } 1034 ts.unlock() 1035 1036 return now, pollUntil, ran 1037 } 1038 1039 // run examines the first timer in ts. If it is ready based on now, 1040 // it runs the timer and removes or updates it. 1041 // Returns 0 if it ran a timer, -1 if there are no more timers, or the time 1042 // when the first timer should run. 1043 // The caller must have locked ts. 1044 // If a timer is run, this will temporarily unlock ts. 1045 // 1046 //go:systemstack 1047 func (ts *timers) run(now int64, bubble *synctestBubble) int64 { 1048 ts.trace("run") 1049 assertLockHeld(&ts.mu) 1050 Redo: 1051 if len(ts.heap) == 0 { 1052 return -1 1053 } 1054 tw := ts.heap[0] 1055 t := tw.timer 1056 if t.ts != ts { 1057 throw("bad ts") 1058 } 1059 1060 if t.astate.Load()&(timerModified|timerZombie) == 0 && tw.when > now { 1061 // Fast path: not ready to run. 1062 return tw.when 1063 } 1064 1065 t.lock() 1066 if t.updateHeap() { 1067 t.unlock() 1068 goto Redo 1069 } 1070 1071 if t.state&timerHeaped == 0 || t.state&timerModified != 0 { 1072 badTimer() 1073 } 1074 1075 if t.when > now { 1076 // Not ready to run. 1077 t.unlock() 1078 return t.when 1079 } 1080 1081 t.unlockAndRun(now, bubble) 1082 assertLockHeld(&ts.mu) // t is unlocked now, but not ts 1083 return 0 1084 } 1085 1086 // unlockAndRun unlocks and runs the timer t (which must be locked). 1087 // If t is in a timer set (t.ts != nil), the caller must also have locked the timer set, 1088 // and this call will temporarily unlock the timer set while running the timer function. 1089 // unlockAndRun returns with t unlocked and t.ts (re-)locked. 1090 // 1091 //go:systemstack 1092 func (t *timer) unlockAndRun(now int64, bubble *synctestBubble) { 1093 t.trace("unlockAndRun") 1094 assertLockHeld(&t.mu) 1095 if t.ts != nil { 1096 assertLockHeld(&t.ts.mu) 1097 } 1098 if raceenabled { 1099 // Note that we are running on a system stack, 1100 // so there is no chance of getg().m being reassigned 1101 // out from under us while this function executes. 1102 tsLocal := &getg().m.p.ptr().timers 1103 if tsLocal.raceCtx == 0 { 1104 tsLocal.raceCtx = racegostart(abi.FuncPCABIInternal((*timers).run) + sys.PCQuantum) 1105 } 1106 raceacquirectx(tsLocal.raceCtx, unsafe.Pointer(t)) 1107 } 1108 1109 if t.state&(timerModified|timerZombie) != 0 { 1110 badTimer() 1111 } 1112 1113 f := t.f 1114 arg := t.arg 1115 seq := t.seq 1116 var next int64 1117 delay := now - t.when 1118 if t.period > 0 { 1119 // Leave in heap but adjust next time to fire. 1120 next = t.when + t.period*(1+delay/t.period) 1121 if next < 0 { // check for overflow. 1122 next = maxWhen 1123 } 1124 } else { 1125 next = 0 1126 } 1127 if t.isChan && bubble == nil { 1128 // now is read once per timers.run pass and can be stale by 1129 // the time this timer runs. The value sent on the channel is 1130 // derived from delay (see time.sendTime), so recompute it 1131 // with a fresh clock reading. next above deliberately keeps 1132 // the caller's clock so rescheduling is unchanged. 1133 delay = nanotime() - t.when 1134 } 1135 ts := t.ts 1136 t.when = next 1137 if t.state&timerHeaped != 0 { 1138 t.state |= timerModified 1139 if next == 0 { 1140 t.state |= timerZombie 1141 t.ts.zombies.Add(1) 1142 } 1143 t.updateHeap() 1144 } 1145 1146 if t.isChan && t.period == 0 { 1147 // Tell Stop/Reset that we are sending a value. 1148 if t.isSending.Add(1) < 0 { 1149 throw("too many concurrent timer firings") 1150 } 1151 } 1152 1153 t.unlock() 1154 1155 if raceenabled { 1156 // Temporarily use the current P's racectx for g0. 1157 gp := getg() 1158 if gp.racectx != 0 { 1159 throw("unexpected racectx") 1160 } 1161 gp.racectx = gp.m.p.ptr().timers.raceCtx 1162 } 1163 1164 if ts != nil { 1165 ts.unlock() 1166 } 1167 1168 if bubble != nil { 1169 // Temporarily use the timer's synctest group for the G running this timer. 1170 gp := getg() 1171 if gp.bubble != nil { 1172 throw("unexpected syncgroup set") 1173 } 1174 gp.bubble = bubble 1175 bubble.changegstatus(gp, _Gdead, _Grunning) 1176 } 1177 1178 if t.isChan { 1179 // For a timer channel, we want to make sure that no stale sends 1180 // happen after a t.stop or t.modify, but we cannot hold t.mu 1181 // during the actual send (which f does) due to lock ordering. 1182 // It can happen that we are holding t's lock above, we decide 1183 // it's time to send a time value (by calling f), grab the parameters, 1184 // unlock above, and then a t.stop or t.modify changes the timer 1185 // and returns. At that point, the send needs not to happen after all. 1186 // The way we arrange for it not to happen is that t.stop and t.modify 1187 // both increment t.seq while holding both t.mu and t.sendLock. 1188 // We copied the seq value above while holding t.mu. 1189 // Now we can acquire t.sendLock (which will be held across the send) 1190 // and double-check that t.seq is still the seq value we saw above. 1191 // If not, the timer has been updated and we should skip the send. 1192 // We skip the send by reassigning f to a no-op function. 1193 // 1194 // The isSending field tells t.stop or t.modify that we have 1195 // started to send the value. That lets them correctly return 1196 // true meaning that no value was sent. 1197 lock(&t.sendLock) 1198 1199 if t.period == 0 { 1200 // We are committed to possibly sending a value 1201 // based on seq, so no need to keep telling 1202 // stop/modify that we are sending. 1203 if t.isSending.Add(-1) < 0 { 1204 throw("mismatched isSending updates") 1205 } 1206 } 1207 1208 if t.seq != seq { 1209 f = func(any, uintptr, int64) {} 1210 } 1211 } 1212 1213 f(arg, seq, delay) 1214 1215 if t.isChan { 1216 unlock(&t.sendLock) 1217 } 1218 1219 if bubble != nil { 1220 gp := getg() 1221 bubble.changegstatus(gp, _Grunning, _Gdead) 1222 if raceenabled { 1223 // Establish a happens-before between this timer event and 1224 // the next synctest.Wait call. 1225 racereleasemergeg(gp, bubble.raceaddr()) 1226 } 1227 gp.bubble = nil 1228 } 1229 1230 if ts != nil { 1231 ts.lock() 1232 } 1233 1234 if raceenabled { 1235 gp := getg() 1236 gp.racectx = 0 1237 } 1238 } 1239 1240 // verify verifies that the timer heap is in a valid state. 1241 // This is only for debugging, and is only called if verifyTimers is true. 1242 // The caller must have locked ts. 1243 func (ts *timers) verify() { 1244 assertLockHeld(&ts.mu) 1245 for i, tw := range ts.heap { 1246 if i == 0 { 1247 // First timer has no parent. 1248 continue 1249 } 1250 1251 // The heap is timerHeapN-ary. See siftupTimer and siftdownTimer. 1252 p := int(uint(i-1) / timerHeapN) 1253 if tw.less(ts.heap[p]) { 1254 print("bad timer heap at ", i, ": ", p, ": ", ts.heap[p].when, ", ", i, ": ", tw.when, "\n") 1255 throw("bad timer heap") 1256 } 1257 } 1258 if n := int(ts.len.Load()); len(ts.heap) != n { 1259 println("timer heap len", len(ts.heap), "!= atomic len", n) 1260 throw("bad timer heap len") 1261 } 1262 } 1263 1264 // updateMinWhenHeap sets ts.minWhenHeap to ts.heap[0].when. 1265 // The caller must have locked ts or the world must be stopped. 1266 func (ts *timers) updateMinWhenHeap() { 1267 assertWorldStoppedOrLockHeld(&ts.mu) 1268 if len(ts.heap) == 0 { 1269 ts.minWhenHeap.Store(0) 1270 } else { 1271 ts.minWhenHeap.Store(ts.heap[0].when) 1272 } 1273 } 1274 1275 // updateMinWhenModified updates ts.minWhenModified to be <= when. 1276 // ts need not be (and usually is not) locked. 1277 func (ts *timers) updateMinWhenModified(when int64) { 1278 for { 1279 old := ts.minWhenModified.Load() 1280 if old != 0 && old < when { 1281 return 1282 } 1283 if ts.minWhenModified.CompareAndSwap(old, when) { 1284 return 1285 } 1286 } 1287 } 1288 1289 // timeSleepUntil returns the time when the next timer should fire. Returns 1290 // maxWhen if there are no timers. 1291 // This is only called by sysmon and checkdead. 1292 func timeSleepUntil() int64 { 1293 next := int64(maxWhen) 1294 1295 // Prevent allp slice changes. This is like retake. 1296 lock(&allpLock) 1297 for _, pp := range allp { 1298 if pp == nil { 1299 // This can happen if procresize has grown 1300 // allp but not yet created new Ps. 1301 continue 1302 } 1303 1304 if w := pp.timers.wakeTime(); w != 0 { 1305 next = min(next, w) 1306 } 1307 } 1308 unlock(&allpLock) 1309 1310 return next 1311 } 1312 1313 const timerHeapN = 4 1314 1315 // Heap maintenance algorithms. 1316 // These algorithms check for slice index errors manually. 1317 // Slice index error can happen if the program is using racy 1318 // access to timers. We don't want to panic here, because 1319 // it will cause the program to crash with a mysterious 1320 // "panic holding locks" message. Instead, we panic while not 1321 // holding a lock. 1322 1323 // siftUp puts the timer at position i in the right place 1324 // in the heap by moving it up toward the top of the heap. 1325 func (ts *timers) siftUp(i int) { 1326 heap := ts.heap 1327 if i >= len(heap) { 1328 badTimer() 1329 } 1330 tw := heap[i] 1331 if tw.when <= 0 { 1332 badTimer() 1333 } 1334 for i > 0 { 1335 p := int(uint(i-1) / timerHeapN) // parent 1336 if !tw.less(heap[p]) { 1337 break 1338 } 1339 heap[i] = heap[p] 1340 i = p 1341 } 1342 if heap[i].timer != tw.timer { 1343 heap[i] = tw 1344 } 1345 } 1346 1347 // siftDown puts the timer at position i in the right place 1348 // in the heap by moving it down toward the bottom of the heap. 1349 func (ts *timers) siftDown(i int) { 1350 heap := ts.heap 1351 n := len(heap) 1352 if i >= n { 1353 badTimer() 1354 } 1355 if i*timerHeapN+1 >= n { 1356 return 1357 } 1358 tw := heap[i] 1359 if tw.when <= 0 { 1360 badTimer() 1361 } 1362 for { 1363 leftChild := i*timerHeapN + 1 1364 if leftChild >= n { 1365 break 1366 } 1367 w := tw 1368 c := -1 1369 for j, tw := range heap[leftChild:min(leftChild+timerHeapN, n)] { 1370 if tw.less(w) { 1371 w = tw 1372 c = leftChild + j 1373 } 1374 } 1375 if c < 0 { 1376 break 1377 } 1378 heap[i] = heap[c] 1379 i = c 1380 } 1381 if heap[i].timer != tw.timer { 1382 heap[i] = tw 1383 } 1384 } 1385 1386 // initHeap reestablishes the heap order in the slice ts.heap. 1387 // It takes O(n) time for n=len(ts.heap), not the O(n log n) of n repeated add operations. 1388 func (ts *timers) initHeap() { 1389 // Last possible element that needs sifting down is parent of last element; 1390 // last element is len(t)-1; parent of last element is (len(t)-1-1)/timerHeapN. 1391 if len(ts.heap) <= 1 { 1392 return 1393 } 1394 for i := int(uint(len(ts.heap)-1-1) / timerHeapN); i >= 0; i-- { 1395 ts.siftDown(i) 1396 } 1397 } 1398 1399 // badTimer is called if the timer data structures have been corrupted, 1400 // presumably due to racy use by the program. We panic here rather than 1401 // panicking due to invalid slice access while holding locks. 1402 // See issue #25686. 1403 func badTimer() { 1404 throw("timer data corruption") 1405 } 1406 1407 // Timer channels. 1408 1409 // maybeRunChan checks whether the timer needs to run 1410 // to send a value to its associated channel. If so, it does. 1411 // The timer must not be locked. 1412 func (t *timer) maybeRunChan(c *hchan) { 1413 if t.isFake && getg().bubble != c.bubble { 1414 // This should have been checked by the caller, but check just in case. 1415 fatal("synctest timer accessed from outside bubble") 1416 } 1417 if t.astate.Load()&timerHeaped != 0 { 1418 // If the timer is in the heap, the ordinary timer code 1419 // is in charge of sending when appropriate. 1420 return 1421 } 1422 1423 t.lock() 1424 now := nanotime() 1425 if t.isFake { 1426 now = getg().bubble.now 1427 } 1428 if t.state&timerHeaped != 0 || t.when == 0 || t.when > now { 1429 t.trace("maybeRunChan-") 1430 // Timer in the heap, or not running at all, or not triggered. 1431 t.unlock() 1432 return 1433 } 1434 t.trace("maybeRunChan+") 1435 systemstack(func() { 1436 t.unlockAndRun(now, c.bubble) 1437 }) 1438 } 1439 1440 // blockTimerChan is called when a channel op has decided to block on c. 1441 // The caller holds the channel lock for c and possibly other channels. 1442 // blockTimerChan makes sure that c is in a timer heap, 1443 // adding it if needed. 1444 func blockTimerChan(c *hchan) { 1445 t := c.timer 1446 if t.isFake && c.bubble != getg().bubble { 1447 // This should have been checked by the caller, but check just in case. 1448 fatal("synctest timer accessed from outside bubble") 1449 } 1450 1451 t.lock() 1452 t.trace("blockTimerChan") 1453 if !t.isChan { 1454 badTimer() 1455 } 1456 1457 t.blocked++ 1458 1459 // If this is the first enqueue after a recent dequeue, 1460 // the timer may still be in the heap but marked as a zombie. 1461 // Unmark it in this case, if the timer is still pending. 1462 if t.state&timerHeaped != 0 && t.state&timerZombie != 0 && t.when > 0 { 1463 t.state &^= timerZombie 1464 t.ts.zombies.Add(-1) 1465 } 1466 1467 // t.maybeAdd must be called with t unlocked, 1468 // because it needs to lock t.ts before t. 1469 // Then it will do nothing if t.needsAdd(state) is false. 1470 // Check that now before the unlock, 1471 // avoiding the extra lock-lock-unlock-unlock 1472 // inside maybeAdd when t does not need to be added. 1473 add := t.needsAdd() 1474 t.unlock() 1475 if add { 1476 t.maybeAdd() 1477 } 1478 } 1479 1480 // unblockTimerChan is called when a channel op that was blocked on c 1481 // is no longer blocked. Every call to blockTimerChan must be paired with 1482 // a call to unblockTimerChan. 1483 // The caller holds the channel lock for c and possibly other channels. 1484 // unblockTimerChan removes c from the timer heap when nothing is 1485 // blocked on it anymore. 1486 func unblockTimerChan(c *hchan) { 1487 t := c.timer 1488 t.lock() 1489 t.trace("unblockTimerChan") 1490 if !t.isChan || t.blocked == 0 { 1491 badTimer() 1492 } 1493 t.blocked-- 1494 if t.blocked == 0 && t.state&timerHeaped != 0 && t.state&timerZombie == 0 { 1495 // Last goroutine that was blocked on this timer. 1496 // Mark for removal from heap but do not clear t.when, 1497 // so that we know what time it is still meant to trigger. 1498 t.state |= timerZombie 1499 t.ts.zombies.Add(1) 1500 } 1501 t.unlock() 1502 } 1503