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.

121 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_WAITABLEEVENT_H_INCLUDED
  24. #define JUCE_WAITABLEEVENT_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Allows threads to wait for events triggered by other threads.
  28. A thread can call wait() on a WaitableObject, and this will suspend the
  29. calling thread until another thread wakes it up by calling the signal()
  30. method.
  31. */
  32. class JUCE_API WaitableEvent
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a WaitableEvent object.
  37. The object is initially in an unsignalled state.
  38. @param manualReset If this is false, the event will be reset automatically when the wait()
  39. method is called. If manualReset is true, then once the event is signalled,
  40. the only way to reset it will be by calling the reset() method.
  41. */
  42. explicit WaitableEvent (bool manualReset = false) noexcept;
  43. /** Destructor.
  44. If other threads are waiting on this object when it gets deleted, this
  45. can cause nasty errors, so be careful!
  46. */
  47. ~WaitableEvent() noexcept;
  48. //==============================================================================
  49. /** Suspends the calling thread until the event has been signalled.
  50. This will wait until the object's signal() method is called by another thread,
  51. or until the timeout expires.
  52. After the event has been signalled, this method will return true and if manualReset
  53. was set to false in the WaitableEvent's constructor, then the event will be reset.
  54. @param timeOutMilliseconds the maximum time to wait, in milliseconds. A negative
  55. value will cause it to wait forever.
  56. @returns true if the object has been signalled, false if the timeout expires first.
  57. @see signal, reset
  58. */
  59. bool wait (int timeOutMilliseconds = -1) const noexcept;
  60. //==============================================================================
  61. /** Wakes up any threads that are currently waiting on this object.
  62. If signal() is called when nothing is waiting, the next thread to call wait()
  63. will return immediately and reset the signal.
  64. If the WaitableEvent is manual reset, all current and future threads that wait upon this
  65. object will be woken, until reset() is explicitly called.
  66. If the WaitableEvent is automatic reset, and one or more threads is waiting upon the object,
  67. then one of them will be woken up. If no threads are currently waiting, then the next thread
  68. to call wait() will be woken up. As soon as a thread is woken, the signal is automatically
  69. reset.
  70. @see wait, reset
  71. */
  72. void signal() const noexcept;
  73. //==============================================================================
  74. /** Resets the event to an unsignalled state.
  75. If it's not already signalled, this does nothing.
  76. */
  77. void reset() const noexcept;
  78. private:
  79. //==============================================================================
  80. #if JUCE_WINDOWS
  81. void* handle;
  82. #else
  83. mutable pthread_cond_t condition;
  84. mutable pthread_mutex_t mutex;
  85. mutable bool triggered, manualReset;
  86. #endif
  87. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaitableEvent)
  88. };
  89. #endif // JUCE_WAITABLEEVENT_H_INCLUDED