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.

89 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. 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. }