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.

121 lines
3.1KB

  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. class HighResolutionTimer::Pimpl : private Thread
  20. {
  21. using steady_clock = std::chrono::steady_clock;
  22. using milliseconds = std::chrono::milliseconds;
  23. public:
  24. explicit Pimpl (HighResolutionTimer& ownerRef)
  25. : Thread ("HighResolutionTimerThread"),
  26. owner (ownerRef)
  27. {
  28. }
  29. using Thread::isThreadRunning;
  30. void start (int periodMs)
  31. {
  32. {
  33. const std::scoped_lock lk { mutex };
  34. periodMillis = periodMs;
  35. nextTickTime = steady_clock::now() + milliseconds (periodMillis);
  36. }
  37. waitEvent.notify_one();
  38. if (! isThreadRunning())
  39. startThread (Thread::Priority::high);
  40. }
  41. void stop()
  42. {
  43. {
  44. const std::scoped_lock lk { mutex };
  45. periodMillis = 0;
  46. }
  47. waitEvent.notify_one();
  48. if (Thread::getCurrentThreadId() != getThreadId())
  49. stopThread (-1);
  50. }
  51. int getPeriod() const
  52. {
  53. return periodMillis;
  54. }
  55. private:
  56. void run() override
  57. {
  58. for (;;)
  59. {
  60. {
  61. std::unique_lock lk { mutex };
  62. if (waitEvent.wait_until (lk, nextTickTime, [this] { return periodMillis == 0; }))
  63. break;
  64. nextTickTime = steady_clock::now() + milliseconds (periodMillis);
  65. }
  66. owner.hiResTimerCallback();
  67. }
  68. }
  69. HighResolutionTimer& owner;
  70. std::atomic<int> periodMillis { 0 };
  71. steady_clock::time_point nextTickTime;
  72. std::mutex mutex;
  73. std::condition_variable waitEvent;
  74. };
  75. HighResolutionTimer::HighResolutionTimer()
  76. : pimpl (new Pimpl (*this))
  77. {
  78. }
  79. HighResolutionTimer::~HighResolutionTimer()
  80. {
  81. stopTimer();
  82. }
  83. void HighResolutionTimer::startTimer (int periodMs)
  84. {
  85. pimpl->start (jmax (1, periodMs));
  86. }
  87. void HighResolutionTimer::stopTimer()
  88. {
  89. pimpl->stop();
  90. }
  91. bool HighResolutionTimer::isTimerRunning() const noexcept { return getTimerInterval() != 0; }
  92. int HighResolutionTimer::getTimerInterval() const noexcept { return pimpl->getPeriod(); }
  93. } // namespace juce