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.

91 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class AsyncUpdater::AsyncUpdaterMessage : public CallbackMessage
  18. {
  19. public:
  20. AsyncUpdaterMessage (AsyncUpdater& au) : owner (au) {}
  21. void messageCallback() override
  22. {
  23. if (shouldDeliver.compareAndSetBool (0, 1))
  24. owner.handleAsyncUpdate();
  25. }
  26. AsyncUpdater& owner;
  27. Atomic<int> shouldDeliver;
  28. JUCE_DECLARE_NON_COPYABLE (AsyncUpdaterMessage)
  29. };
  30. //==============================================================================
  31. AsyncUpdater::AsyncUpdater()
  32. {
  33. activeMessage = new AsyncUpdaterMessage (*this);
  34. }
  35. AsyncUpdater::~AsyncUpdater()
  36. {
  37. // You're deleting this object with a background thread while there's an update
  38. // pending on the main event thread - that's pretty dodgy threading, as the callback could
  39. // happen after this destructor has finished. You should either use a MessageManagerLock while
  40. // deleting this object, or find some other way to avoid such a race condition.
  41. jassert ((! isUpdatePending())
  42. || MessageManager::getInstanceWithoutCreating() == nullptr
  43. || MessageManager::getInstanceWithoutCreating()->currentThreadHasLockedMessageManager());
  44. activeMessage->shouldDeliver.set (0);
  45. }
  46. void AsyncUpdater::triggerAsyncUpdate()
  47. {
  48. // If you're calling this before (or after) the MessageManager is
  49. // running, then you're not going to get any callbacks!
  50. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  51. if (activeMessage->shouldDeliver.compareAndSetBool (1, 0))
  52. if (! activeMessage->post())
  53. cancelPendingUpdate(); // if the message queue fails, this avoids getting
  54. // trapped waiting for the message to arrive
  55. }
  56. void AsyncUpdater::cancelPendingUpdate() noexcept
  57. {
  58. activeMessage->shouldDeliver.set (0);
  59. }
  60. void AsyncUpdater::handleUpdateNowIfNeeded()
  61. {
  62. // This can only be called by the event thread.
  63. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  64. if (activeMessage->shouldDeliver.exchange (0) != 0)
  65. handleAsyncUpdate();
  66. }
  67. bool AsyncUpdater::isUpdatePending() const noexcept
  68. {
  69. return activeMessage->shouldDeliver.value != 0;
  70. }