juce_WaitableEvent.h 4.9 KB

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