OpenShot Library | libopenshot-audio  0.1.9
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  /** Destructor.
54 
55  If other threads are waiting on this object when it gets deleted, this
56  can cause nasty errors, so be careful!
57  */
58  ~WaitableEvent() noexcept;
59 
60  //==============================================================================
61  /** Suspends the calling thread until the event has been signalled.
62 
63  This will wait until the object's signal() method is called by another thread,
64  or until the timeout expires.
65 
66  After the event has been signalled, this method will return true and if manualReset
67  was set to false in the WaitableEvent's constructor, then the event will be reset.
68 
69  @param timeOutMilliseconds the maximum time to wait, in milliseconds. A negative
70  value will cause it to wait forever.
71 
72  @returns true if the object has been signalled, false if the timeout expires first.
73  @see signal, reset
74  */
75  bool wait (int timeOutMilliseconds = -1) const noexcept;
76 
77  //==============================================================================
78  /** Wakes up any threads that are currently waiting on this object.
79 
80  If signal() is called when nothing is waiting, the next thread to call wait()
81  will return immediately and reset the signal.
82 
83  If the WaitableEvent is manual reset, all current and future threads that wait upon this
84  object will be woken, until reset() is explicitly called.
85 
86  If the WaitableEvent is automatic reset, and one or more threads is waiting upon the object,
87  then one of them will be woken up. If no threads are currently waiting, then the next thread
88  to call wait() will be woken up. As soon as a thread is woken, the signal is automatically
89  reset.
90 
91  @see wait, reset
92  */
93  void signal() const noexcept;
94 
95  //==============================================================================
96  /** Resets the event to an unsignalled state.
97  If it's not already signalled, this does nothing.
98  */
99  void reset() const noexcept;
100 
101 
102 private:
103  //==============================================================================
104  #if JUCE_WINDOWS
105  void* handle;
106  #else
107  mutable pthread_cond_t condition;
108  mutable pthread_mutex_t mutex;
109  mutable bool triggered, manualReset;
110  #endif
111 
112  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaitableEvent)
113 };
114 
115 } // namespace juce
116 
117 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
Allows threads to wait for events triggered by other threads.