timeoutcond.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package syncutil
  7. import (
  8. "sync"
  9. "time"
  10. )
  11. // TimeoutCond is a variant on Cond. It has roughly the same semantics regarding 'L' - it must be held
  12. // both when broadcasting and when calling TimeoutCondWaiter.Wait()
  13. // Call Broadcast() to broadcast to all waiters on the TimeoutCond. Call SetupWait to create a
  14. // TimeoutCondWaiter configured with the given timeout, which can then be used to listen for
  15. // broadcasts.
  16. type TimeoutCond struct {
  17. L sync.Locker
  18. ch chan struct{}
  19. }
  20. // TimeoutCondWaiter is a type allowing a consumer to wait on a TimeoutCond with a timeout. Wait() may be called multiple times,
  21. // and will return true every time that the TimeoutCond is broadcast to. Once the configured timeout
  22. // expires, Wait() will return false.
  23. // Call Stop() to release resources once this TimeoutCondWaiter is no longer needed.
  24. type TimeoutCondWaiter struct {
  25. c *TimeoutCond
  26. timer *time.Timer
  27. }
  28. func NewTimeoutCond(l sync.Locker) *TimeoutCond {
  29. return &TimeoutCond{
  30. L: l,
  31. }
  32. }
  33. func (c *TimeoutCond) Broadcast() {
  34. // ch.L must be locked when calling this function
  35. if c.ch != nil {
  36. close(c.ch)
  37. c.ch = nil
  38. }
  39. }
  40. func (c *TimeoutCond) SetupWait(timeout time.Duration) *TimeoutCondWaiter {
  41. timer := time.NewTimer(timeout)
  42. return &TimeoutCondWaiter{
  43. c: c,
  44. timer: timer,
  45. }
  46. }
  47. func (w *TimeoutCondWaiter) Wait() bool {
  48. // ch.L must be locked when calling this function
  49. // Ensure that the channel exists, since we're going to be waiting on it
  50. if w.c.ch == nil {
  51. w.c.ch = make(chan struct{})
  52. }
  53. ch := w.c.ch
  54. w.c.L.Unlock()
  55. defer w.c.L.Lock()
  56. select {
  57. case <-w.timer.C:
  58. return false
  59. case <-ch:
  60. return true
  61. }
  62. }
  63. func (w *TimeoutCondWaiter) Stop() {
  64. w.timer.Stop()
  65. }