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.

85 lines
2.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. Atomic<int> shouldDeliver;
  27. private:
  28. AsyncUpdater& owner;
  29. JUCE_DECLARE_NON_COPYABLE (AsyncUpdaterMessage)
  30. };
  31. //==============================================================================
  32. AsyncUpdater::AsyncUpdater()
  33. {
  34. activeMessage = new AsyncUpdaterMessage (*this);
  35. }
  36. AsyncUpdater::~AsyncUpdater()
  37. {
  38. // You're deleting this object with a background thread while there's an update
  39. // pending on the main event thread - that's pretty dodgy threading, as the callback could
  40. // happen after this destructor has finished. You should either use a MessageManagerLock while
  41. // deleting this object, or find some other way to avoid such a race condition.
  42. jassert ((! isUpdatePending()) || MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  43. activeMessage->shouldDeliver.set (0);
  44. }
  45. void AsyncUpdater::triggerAsyncUpdate()
  46. {
  47. if (activeMessage->shouldDeliver.compareAndSetBool (1, 0))
  48. activeMessage->post();
  49. }
  50. void AsyncUpdater::cancelPendingUpdate() noexcept
  51. {
  52. activeMessage->shouldDeliver.set (0);
  53. }
  54. void AsyncUpdater::handleUpdateNowIfNeeded()
  55. {
  56. // This can only be called by the event thread.
  57. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  58. if (activeMessage->shouldDeliver.exchange (0) != 0)
  59. handleAsyncUpdate();
  60. }
  61. bool AsyncUpdater::isUpdatePending() const noexcept
  62. {
  63. return activeMessage->shouldDeliver.value != 0;
  64. }