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