ngscopeclient v0.2.2
Loading...
Searching...
No Matches
Event.h
1/***********************************************************************************************************************
2* *
3* ngscopeclient *
4* *
5* Copyright (c) 2012-2026 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 {}
43
47 void Signal()
48 {
49 m_ready = true;
50 m_cond.notify_one();
51 }
52
59 {
60 //Existing event pending? We did nothing
61 if(m_ready.exchange(true) == true)
62 return false;
63
64 //No event was already pending so we submitted one.
65 else
66 {
67 m_cond.notify_one();
68 return true;
69 }
70 }
71
72
81 {
82 while(true)
83 {
84 //Existing event pending? Block until it's completed
85 if(m_ready.exchange(true) == true)
86 processedEvent.Block();
87
88 //No event was already pending so we submitted one.
89 else
90 {
91 m_cond.notify_one();
92 break;
93 }
94 }
95 }
96
100 void Block()
101 {
102 std::unique_lock<std::mutex> lock(m_mutex);
103 m_cond.wait(lock, [&]{ return m_ready.load(); });
104 m_ready = false;
105 }
106
112 bool Peek(bool clearReady = true)
113 {
114 if(m_ready)
115 {
116 if(clearReady)
117 m_ready = false;
118 return true;
119 }
120
121 return false;
122 }
123
127 void Clear()
128 {
129 m_ready = false;
130 }
131
132protected:
133 std::mutex m_mutex;
134 std::condition_variable m_cond;
135 std::atomic_bool m_ready;
136};
137
138#endif
Definition AcceleratorBuffer.h:204
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:80
bool SignalIfNotAlreadySignaled()
Sends an event to the receiving thread.
Definition Event.h:58
bool Peek(bool clearReady=true)
Checks if the event is signaled, and returns immediately without blocking regardless of event state.
Definition Event.h:112
void Signal()
Sends an event to the receiving thread.
Definition Event.h:47
void Clear()
Clears the event state if it's currently signaled.
Definition Event.h:127
void Block()
Blocks until the event is signaled.
Definition Event.h:100