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.

134 lines
3.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. class LockingAsyncUpdater::Impl : public CallbackMessage
  20. {
  21. public:
  22. explicit Impl (std::function<void()> cb)
  23. : callback (std::move (cb)) {}
  24. void clear()
  25. {
  26. const ScopedLock lock (mutex);
  27. deliver = false;
  28. callback = nullptr;
  29. }
  30. void trigger()
  31. {
  32. {
  33. const ScopedLock lock (mutex);
  34. if (deliver)
  35. return;
  36. deliver = true;
  37. }
  38. if (! post())
  39. cancel();
  40. }
  41. void cancel()
  42. {
  43. const ScopedLock lock (mutex);
  44. deliver = false;
  45. }
  46. bool isPending()
  47. {
  48. const ScopedLock lock (mutex);
  49. return deliver;
  50. }
  51. void messageCallback() override
  52. {
  53. const ScopedLock lock (mutex);
  54. if (std::exchange (deliver, false))
  55. NullCheckedInvocation::invoke (callback);
  56. }
  57. private:
  58. CriticalSection mutex;
  59. std::function<void()> callback;
  60. bool deliver = false;
  61. };
  62. //==============================================================================
  63. LockingAsyncUpdater::LockingAsyncUpdater (std::function<void()> callbackToUse)
  64. : impl (new Impl (std::move (callbackToUse))) {}
  65. LockingAsyncUpdater::LockingAsyncUpdater (LockingAsyncUpdater&& other) noexcept
  66. : impl (std::exchange (other.impl, nullptr)) {}
  67. LockingAsyncUpdater& LockingAsyncUpdater::operator= (LockingAsyncUpdater&& other) noexcept
  68. {
  69. LockingAsyncUpdater temp { std::move (other) };
  70. std::swap (temp.impl, impl);
  71. return *this;
  72. }
  73. LockingAsyncUpdater::~LockingAsyncUpdater()
  74. {
  75. if (impl != nullptr)
  76. impl->clear();
  77. }
  78. void LockingAsyncUpdater::triggerAsyncUpdate()
  79. {
  80. if (impl != nullptr)
  81. impl->trigger();
  82. else
  83. jassertfalse; // moved-from!
  84. }
  85. void LockingAsyncUpdater::cancelPendingUpdate() noexcept
  86. {
  87. if (impl != nullptr)
  88. impl->cancel();
  89. else
  90. jassertfalse; // moved-from!
  91. }
  92. void LockingAsyncUpdater::handleUpdateNowIfNeeded()
  93. {
  94. if (impl != nullptr)
  95. impl->messageCallback();
  96. else
  97. jassertfalse; // moved-from!
  98. }
  99. bool LockingAsyncUpdater::isUpdatePending() const noexcept
  100. {
  101. if (impl != nullptr)
  102. return impl->isPending();
  103. jassertfalse; // moved-from!
  104. return false;
  105. }
  106. } // namespace juce