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.

100 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Allows threads to wait for events triggered by other threads.
  22. A thread can call WaitableEvent::wait() to suspend the calling thread until
  23. another thread wakes it up by calling the WaitableEvent::signal() method.
  24. @tags{Core}
  25. */
  26. class JUCE_API WaitableEvent
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates a WaitableEvent object.
  31. The object is initially in an unsignalled state.
  32. @param manualReset If this is false, the event will be reset automatically when the wait()
  33. method is called. If manualReset is true, then once the event is signalled,
  34. the only way to reset it will be by calling the reset() method.
  35. */
  36. explicit WaitableEvent (bool manualReset = false) noexcept;
  37. //==============================================================================
  38. /** Suspends the calling thread until the event has been signalled.
  39. This will wait until the object's signal() method is called by another thread,
  40. or until the timeout expires.
  41. After the event has been signalled, this method will return true and if manualReset
  42. was set to false in the WaitableEvent's constructor, then the event will be reset.
  43. @param timeOutMilliseconds the maximum time to wait, in milliseconds. A negative
  44. value will cause it to wait forever.
  45. @returns true if the object has been signalled, false if the timeout expires first.
  46. @see signal, reset
  47. */
  48. bool wait (double timeOutMilliseconds = -1.0) const;
  49. /** Wakes up any threads that are currently waiting on this object.
  50. If signal() is called when nothing is waiting, the next thread to call wait()
  51. will return immediately and reset the signal.
  52. If the WaitableEvent is manual reset, all current and future threads that wait upon this
  53. object will be woken, until reset() is explicitly called.
  54. If the WaitableEvent is automatic reset, and one or more threads is waiting upon the object,
  55. then one of them will be woken up. If no threads are currently waiting, then the next thread
  56. to call wait() will be woken up. As soon as a thread is woken, the signal is automatically
  57. reset.
  58. @see wait, reset
  59. */
  60. void signal() const;
  61. /** Resets the event to an unsignalled state.
  62. If it's not already signalled, this does nothing.
  63. */
  64. void reset() const;
  65. private:
  66. //==============================================================================
  67. bool useManualReset;
  68. mutable std::mutex mutex;
  69. mutable std::condition_variable condition;
  70. mutable std::atomic<bool> triggered { false };
  71. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaitableEvent)
  72. };
  73. } // namespace juce