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.

109 lines
4.2KB

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