The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. /**
  20. A high-resolution periodic timer.
  21. This provides accurately-timed regular callbacks. Unlike the normal Timer
  22. class, this one uses a dedicated thread, not the message thread, so is
  23. far more stable and precise.
  24. You should only use this class in situations where you really need accuracy,
  25. because unlike the normal Timer class, which is very lightweight and cheap
  26. the HighResolutionTimer will use far more resources and require thread
  27. safety considerations.
  28. @see Timer
  29. @tags{Core}
  30. */
  31. class JUCE_API HighResolutionTimer
  32. {
  33. protected:
  34. /** Creates a HighResolutionTimer.
  35. When created, the timer is stopped, so use startTimer() to get it going.
  36. */
  37. HighResolutionTimer();
  38. public:
  39. /** Destructor. */
  40. virtual ~HighResolutionTimer();
  41. //==============================================================================
  42. /** The user-defined callback routine that actually gets called periodically.
  43. This will be called on a dedicated timer thread, so make sure your
  44. implementation is thread-safe!
  45. On some platforms the dedicated timer thread may be shared with
  46. other HighResolutionTimer's so aim to complete any work in this
  47. callback as fast as possible.
  48. It's perfectly ok to call startTimer() or stopTimer() from within this
  49. callback to change the subsequent intervals. However, if you call
  50. stopTimer() in the callback it's still best practice to call stopTimer()
  51. from the destructor in order to avoid data races.
  52. */
  53. virtual void hiResTimerCallback() = 0;
  54. //==============================================================================
  55. /** Starts the timer and sets the length of interval required.
  56. If the timer has already started, this will reset the timer, so the
  57. time between calling this method and the next timer callback
  58. will not be less than the interval length passed in.
  59. In exceptional circumstances the dedicated timer thread may not start,
  60. if this is a potential concern for your use case, you can call isTimerRunning()
  61. to confirm if the timer actually started.
  62. @param intervalInMilliseconds the interval to use (a value of zero or less will stop the timer)
  63. */
  64. void startTimer (int intervalInMilliseconds);
  65. /** Stops the timer.
  66. This method may block while it waits for pending callbacks to complete. Once it
  67. returns, no more callbacks will be made. If it is called from the timer's own thread,
  68. it will cancel the timer after the current callback returns.
  69. To prevent data races it's normally best practice to call this in the derived classes
  70. destructor, even if stopTimer() was called in the hiResTimerCallback().
  71. */
  72. void stopTimer();
  73. /** Checks if the timer has been started.
  74. @returns true if the timer is running.
  75. */
  76. bool isTimerRunning() const noexcept;
  77. /** Returns the timer's interval.
  78. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  79. */
  80. int getTimerInterval() const noexcept;
  81. private:
  82. class Impl;
  83. std::unique_ptr<Impl> impl;
  84. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HighResolutionTimer)
  85. };
  86. } // namespace juce