OpenShot Library | OpenShotAudio  0.2.2
juce_WaitableEvent.h
1 
2 /** @weakgroup juce_core-threads
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Allows threads to wait for events triggered by other threads.
33 
34  A thread can call WaitableEvent::wait() to suspend the calling thread until
35  another thread wakes it up by calling the WaitableEvent::signal() method.
36 
37  @tags{Core}
38 */
40 {
41 public:
42  //==============================================================================
43  /** Creates a WaitableEvent object.
44 
45  The object is initially in an unsignalled state.
46 
47  @param manualReset If this is false, the event will be reset automatically when the wait()
48  method is called. If manualReset is true, then once the event is signalled,
49  the only way to reset it will be by calling the reset() method.
50  */
51  explicit WaitableEvent (bool manualReset = false) noexcept;
52 
53  //==============================================================================
54  /** Suspends the calling thread until the event has been signalled.
55 
56  This will wait until the object's signal() method is called by another thread,
57  or until the timeout expires.
58 
59  After the event has been signalled, this method will return true and if manualReset
60  was set to false in the WaitableEvent's constructor, then the event will be reset.
61 
62  @param timeOutMilliseconds the maximum time to wait, in milliseconds. A negative
63  value will cause it to wait forever.
64 
65  @returns true if the object has been signalled, false if the timeout expires first.
66  @see signal, reset
67  */
68  bool wait (int timeOutMilliseconds = -1) const;
69 
70  /** Wakes up any threads that are currently waiting on this object.
71 
72  If signal() is called when nothing is waiting, the next thread to call wait()
73  will return immediately and reset the signal.
74 
75  If the WaitableEvent is manual reset, all current and future threads that wait upon this
76  object will be woken, until reset() is explicitly called.
77 
78  If the WaitableEvent is automatic reset, and one or more threads is waiting upon the object,
79  then one of them will be woken up. If no threads are currently waiting, then the next thread
80  to call wait() will be woken up. As soon as a thread is woken, the signal is automatically
81  reset.
82 
83  @see wait, reset
84  */
85  void signal() const;
86 
87  /** Resets the event to an unsignalled state.
88  If it's not already signalled, this does nothing.
89  */
90  void reset() const;
91 
92 private:
93  //==============================================================================
94  bool useManualReset;
95 
96  mutable std::mutex mutex;
97  mutable std::condition_variable condition;
98  mutable std::atomic<bool> triggered { false };
99 
100  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaitableEvent)
101 };
102 
103 } // namespace juce
104 
105 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
Allows threads to wait for events triggered by other threads.