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.

94 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 AsyncUpdater::AsyncUpdaterMessage : public CallbackMessage
  20. {
  21. public:
  22. AsyncUpdaterMessage (AsyncUpdater& au) : owner (au) {}
  23. void messageCallback() override
  24. {
  25. if (shouldDeliver.compareAndSetBool (0, 1))
  26. owner.handleAsyncUpdate();
  27. }
  28. AsyncUpdater& owner;
  29. Atomic<int> shouldDeliver;
  30. JUCE_DECLARE_NON_COPYABLE (AsyncUpdaterMessage)
  31. };
  32. //==============================================================================
  33. AsyncUpdater::AsyncUpdater()
  34. {
  35. activeMessage = *new AsyncUpdaterMessage (*this);
  36. }
  37. AsyncUpdater::~AsyncUpdater()
  38. {
  39. // You're deleting this object with a background thread while there's an update
  40. // pending on the main event thread - that's pretty dodgy threading, as the callback could
  41. // happen after this destructor has finished. You should either use a MessageManagerLock while
  42. // deleting this object, or find some other way to avoid such a race condition.
  43. jassert ((! isUpdatePending())
  44. || MessageManager::getInstanceWithoutCreating() == nullptr
  45. || MessageManager::getInstanceWithoutCreating()->currentThreadHasLockedMessageManager());
  46. activeMessage->shouldDeliver.set (0);
  47. }
  48. void AsyncUpdater::triggerAsyncUpdate()
  49. {
  50. // If you're calling this before (or after) the MessageManager is
  51. // running, then you're not going to get any callbacks!
  52. JUCE_ASSERT_MESSAGE_MANAGER_EXISTS
  53. if (activeMessage->shouldDeliver.compareAndSetBool (1, 0))
  54. if (! activeMessage->post())
  55. cancelPendingUpdate(); // if the message queue fails, this avoids getting
  56. // trapped waiting for the message to arrive
  57. }
  58. void AsyncUpdater::cancelPendingUpdate() noexcept
  59. {
  60. activeMessage->shouldDeliver.set (0);
  61. }
  62. void AsyncUpdater::handleUpdateNowIfNeeded()
  63. {
  64. // This can only be called by the event thread.
  65. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  66. if (activeMessage->shouldDeliver.exchange (0) != 0)
  67. handleAsyncUpdate();
  68. }
  69. bool AsyncUpdater::isUpdatePending() const noexcept
  70. {
  71. return activeMessage->shouldDeliver.value != 0;
  72. }
  73. } // namespace juce