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.

93 lines
2.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class AsyncUpdater::AsyncUpdaterMessage : public CallbackMessage
  21. {
  22. public:
  23. AsyncUpdaterMessage (AsyncUpdater& owner_)
  24. : owner (owner_)
  25. {
  26. }
  27. void messageCallback()
  28. {
  29. if (shouldDeliver.compareAndSetBool (0, 1))
  30. owner.handleAsyncUpdate();
  31. }
  32. Atomic<int> shouldDeliver;
  33. private:
  34. AsyncUpdater& owner;
  35. };
  36. //==============================================================================
  37. AsyncUpdater::AsyncUpdater()
  38. {
  39. message = new AsyncUpdaterMessage (*this);
  40. }
  41. AsyncUpdater::~AsyncUpdater()
  42. {
  43. // You're deleting this object with a background thread while there's an update
  44. // pending on the main event thread - that's pretty dodgy threading, as the callback could
  45. // happen after this destructor has finished. You should either use a MessageManagerLock while
  46. // deleting this object, or find some other way to avoid such a race condition.
  47. jassert ((! isUpdatePending()) || MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  48. message->shouldDeliver.set (0);
  49. }
  50. void AsyncUpdater::triggerAsyncUpdate()
  51. {
  52. if (message->shouldDeliver.compareAndSetBool (1, 0))
  53. message->post();
  54. }
  55. void AsyncUpdater::cancelPendingUpdate() noexcept
  56. {
  57. message->shouldDeliver.set (0);
  58. }
  59. void AsyncUpdater::handleUpdateNowIfNeeded()
  60. {
  61. // This can only be called by the event thread.
  62. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  63. if (message->shouldDeliver.exchange (0) != 0)
  64. handleAsyncUpdate();
  65. }
  66. bool AsyncUpdater::isUpdatePending() const noexcept
  67. {
  68. return message->shouldDeliver.value != 0;
  69. }
  70. END_JUCE_NAMESPACE