Audio plugin host https://kx.studio/carla
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.

SchedulerTree.hpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <chrono>
  21. #include <functional>
  22. #include <list>
  23. #include <map>
  24. #include <memory>
  25. namespace ableton
  26. {
  27. namespace test
  28. {
  29. namespace serial_io
  30. {
  31. class SchedulerTree
  32. {
  33. public:
  34. using TimePoint = std::chrono::system_clock::time_point;
  35. using TimerId = std::size_t;
  36. using TimerErrorCode = int;
  37. void run();
  38. std::shared_ptr<SchedulerTree> makeChild();
  39. template <typename Handler>
  40. void async(Handler handler)
  41. {
  42. mPendingHandlers.push_back(std::move(handler));
  43. }
  44. template <typename Handler>
  45. void setTimer(const TimerId timerId, const TimePoint expiration, Handler handler)
  46. {
  47. using namespace std;
  48. mTimers[make_pair(move(expiration), timerId)] = move(handler);
  49. }
  50. void cancelTimer(const TimerId timerId);
  51. // returns the time that the next timer in the subtree expires
  52. TimePoint nextTimerExpiration();
  53. // triggers all timers in the subtree that expire at time t or before
  54. void triggerTimersUntil(const TimePoint t);
  55. private:
  56. // returns true if some work was done, false if there was none to do
  57. bool handlePending();
  58. // returns the time that the next timer from this node expires
  59. TimePoint nextOwnTimerExpiration();
  60. // Traversal function over children that cleans up children that
  61. // have been destroyed.
  62. template <typename Fn>
  63. void withChildren(Fn fn)
  64. {
  65. auto it = begin(mChildren);
  66. while (it != end(mChildren))
  67. {
  68. const auto childIt = it++;
  69. auto pChild = childIt->lock();
  70. if (pChild)
  71. {
  72. fn(*pChild);
  73. }
  74. else
  75. {
  76. mChildren.erase(childIt);
  77. }
  78. }
  79. }
  80. using TimerHandler = std::function<void(TimerErrorCode)>;
  81. using TimerMap = std::map<std::pair<TimePoint, TimerId>, TimerHandler>;
  82. TimerMap mTimers;
  83. std::list<std::function<void()>> mPendingHandlers;
  84. std::list<std::weak_ptr<SchedulerTree>> mChildren;
  85. };
  86. } // namespace serial_io
  87. } // namespace test
  88. } // namespace ableton