ngscopeclient 0.1-dev+51fbda87c
Event.h
1/***********************************************************************************************************************
2* *
3* glscopeclient *
4* *
5* Copyright (c) 2012-2022 Andrew D. Zonenberg *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29#ifndef Event_h
30#define Event_h
31
37class Event
38{
39public:
40 Event()
41 { m_ready = false; }
42
46 void Signal()
47 {
48 m_ready = true;
49 m_cond.notify_one();
50 }
51
58 {
59 //Existing event pending? We did nothing
60 if(m_ready.exchange(true) == true)
61 return false;
62
63 //No event was already pending so we submitted one.
64 else
65 {
66 m_cond.notify_one();
67 return true;
68 }
69 }
70
71
79 void SignalExactlyOnce(Event& processedEvent)
80 {
81 while(true)
82 {
83 //Existing event pending? Block until it's completed
84 if(m_ready.exchange(true) == true)
85 processedEvent.Block();
86
87 //No event was already pending so we submitted one.
88 else
89 {
90 m_cond.notify_one();
91 break;
92 }
93 }
94 }
95
99 void Block()
100 {
101 std::unique_lock<std::mutex> lock(m_mutex);
102 m_cond.wait(lock, [&]{ return m_ready.load(); });
103 m_ready = false;
104 }
105
111 bool Peek(bool clearReady = true)
112 {
113 if(m_ready)
114 {
115 if(clearReady)
116 m_ready = false;
117 return true;
118 }
119
120 return false;
121 }
122
126 void Clear()
127 {
128 m_ready = false;
129 }
130
131protected:
132 std::mutex m_mutex;
133 std::condition_variable m_cond;
134 std::atomic_bool m_ready;
135};
136
137#endif
Synchronization primitive for sending a "something is ready" notification to a thread.
Definition: Event.h:38
void SignalExactlyOnce(Event &processedEvent)
Sends an event to the receiving thread.
Definition: Event.h:79
bool SignalIfNotAlreadySignaled()
Sends an event to the receiving thread.
Definition: Event.h:57
bool Peek(bool clearReady=true)
Checks if the event is signaled, and returns immediately without blocking regardless of event state.
Definition: Event.h:111
void Signal()
Sends an event to the receiving thread.
Definition: Event.h:46
void Clear()
Clears the event state if it's currently signaled.
Definition: Event.h:126
void Block()
Blocks until the event is signaled.
Definition: Event.h:99