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.

86 lines
2.8KB

  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& au) : owner (au) {}
  22. void messageCallback()
  23. {
  24. if (shouldDeliver.compareAndSetBool (0, 1))
  25. owner.handleAsyncUpdate();
  26. }
  27. Atomic<int> shouldDeliver;
  28. private:
  29. AsyncUpdater& owner;
  30. JUCE_DECLARE_NON_COPYABLE (AsyncUpdaterMessage);
  31. };
  32. //==============================================================================
  33. AsyncUpdater::AsyncUpdater()
  34. {
  35. message = 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()) || MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  44. message->shouldDeliver.set (0);
  45. }
  46. void AsyncUpdater::triggerAsyncUpdate()
  47. {
  48. if (message->shouldDeliver.compareAndSetBool (1, 0))
  49. message->post();
  50. }
  51. void AsyncUpdater::cancelPendingUpdate() noexcept
  52. {
  53. message->shouldDeliver.set (0);
  54. }
  55. void AsyncUpdater::handleUpdateNowIfNeeded()
  56. {
  57. // This can only be called by the event thread.
  58. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  59. if (message->shouldDeliver.exchange (0) != 0)
  60. handleAsyncUpdate();
  61. }
  62. bool AsyncUpdater::isUpdatePending() const noexcept
  63. {
  64. return message->shouldDeliver.value != 0;
  65. }