MLIB
Loading...
Searching...
No Matches
syncque.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#if __has_include("defs.h")
11#include "defs.h"
12#endif
13
14#include <queue>
15#include "semaphore.h"
16#include "critsect.h"
17#include "stopwatch.h"
18
19namespace mlib {
34template <class M>
36{
37public:
40 : message (nullptr)
41 {}
42
44 void produce (const M& obj)
45 {
46 update.enter ();
47 while (message)
48 {
49 update.leave ();
50 prod_sema.wait ();
51 update.enter ();
52 }
53 cons_sema.signal ();
54 message = &obj;
55 update.leave ();
56 }
57
58 void consume (M& obj)
59 {
60 update.enter ();
61 while (!message)
62 {
63 update.leave ();
64 cons_sema.wait ();
65 update.enter ();
66 }
67 prod_sema.signal ();
68 obj = *message;
69 message = nullptr;
70 update.leave ();
71 }
72
73private:
74 criticalsection update;
75 semaphore prod_sema;
76 semaphore cons_sema;
77 const M* message;
78};
79
89template <class M, class C = std::queue<M>>
90class async_queue : protected C
91{
92public:
94 async_queue (size_t limit_ = INFINITE)
95 : limit (limit_)
96 {
97 if (limit > 0 && limit < INFINITE)
98 prod_sema.signal ((int)limit);
99 }
100
103 virtual bool produce (const M& obj, DWORD timeout = INFINITE)
104 {
105 update.enter ();
106 if (limit < INFINITE)
107 {
108 // Bounded queue. See if there is enough space to produce
109 if (timeout == 0)
110 {
111 //Don't wait if there is no space
112 if (C::size () >= limit)
113 {
114 update.leave ();
115 return false;
116 }
117 }
118 else if (timeout == INFINITE)
119 {
120 //wait forever to get space
121 while (C::size () >= limit)
122 {
123 update.leave ();
124 prod_sema.wait ();
125 update.enter ();
126 }
127 }
128 else
129 {
130 stopwatch t; // need a stopwatch to count cumulated wait time
131 t.start ();
132 while (C::size () >= limit)
133 {
134 update.leave ();
135 int t_wait = timeout - (int)t.lap_msec ();
136 if (t_wait < 0 || prod_sema.wait (t_wait) == WAIT_TIMEOUT)
137 return false;
138 update.enter ();
139 }
140 }
141 }
142 C::push (obj); // put a copy of the object at the end
143 cons_sema.signal (); // and signal the semaphore
144 update.leave ();
145
146 return true;
147 }
148
151 virtual bool consume (M& result, int timeout = INFINITE)
152 {
153 update.enter ();
154 if (timeout == 0)
155 {
156 //no wait
157 if (C::empty ())
158 {
159 update.leave ();
160 return false;
161 }
162 }
163 else if (timeout == INFINITE)
164 {
165 //wait forever
166 while (C::empty ())
167 {
168 update.leave ();
169 cons_sema.wait (); // wait for a producer
170 update.enter ();
171 }
172 }
173 else
174 {
175 stopwatch t;
176 int t_wait;
177 t.start ();
178 while (C::empty ())
179 {
180 update.leave ();
181 t_wait = timeout - (int)t.lap_msec ();
182 // give up or wait for a producer
183 if (t_wait <= 0 || cons_sema.wait (t_wait) == WAIT_TIMEOUT)
184 return false;
185 update.enter ();
186 }
187 }
188 result = std::move(C::front ()); // get the message
189 C::pop ();
190 if (limit != INFINITE)
191 prod_sema.signal (); // signal producers there is space available
192
193 update.leave ();
194 return true;
195 }
196
198 bool empty ()
199 {
200 lock l (update);
201 return C::empty ();
202 }
203
205 bool full ()
206 {
207 lock l (this->update);
208 return (C::size () == limit);
209 }
210
212 size_t size ()
213 {
214 lock l (update);
215 return C::size ();
216 }
217
218protected:
219 size_t limit;
223};
224
232template <class M>
233class async_buffer : protected mlib::ring_buffer<M>
234{
235public:
237 async_buffer (size_t limit_)
238 : ring_buffer<M> (limit_)
239 {
240 }
241
243 virtual void produce (const M& obj)
244 {
245 lock l (update);
246 auto prev_sz = ring_buffer<M>::size ();
248 if (ring_buffer<M>::size () != prev_sz)
249 sema.signal ();
250 }
251
254 virtual bool consume (M& result, int timeout = INFINITE)
255 {
256 bool ret = false;
257 if (timeout == 0)
258 {
259 update.enter ();
260 {
261 if (!ring_buffer<M>::empty ())
262 {
263 result = ring_buffer<M>::front ();
265 ret = true;
266 }
267 update.leave ();
268 }
269 return ret;
270 }
271 if (timeout != INFINITE)
272 {
273 stopwatch t;
274 int t_wait;
275 t.start ();
276 update.enter ();
277 while (ring_buffer<M>::empty ())
278 {
279 update.leave ();
280 t_wait = timeout - (int)t.lap_msec ();
281 // give up or wait for a producer
282 if (t_wait <= 0 || sema.wait (t_wait) == WAIT_TIMEOUT)
283 return false;
284 update.enter ();
285 }
286 }
287 else
288 {
289 update.enter ();
291 {
292 while (1)
293 {
294 update.leave ();
295 sema.wait (); // wait for a producer
296 update.enter ();
297 if (!ring_buffer<M>::empty ())
298 break;
299 }
300 }
301 }
302 result = std::move (ring_buffer<M>::front ()); // get the message
304
305 update.leave ();
306 return true;
307 }
308
310 bool empty ()
311 {
312 return ring_buffer<M>::empty ();
313 }
314
316 bool full () const
317 {
318 return ring_buffer<M>::full();
319 }
320
322 size_t size () const
323 {
324 return ring_buffer<M>::size ();
325 }
326
328 void clear ()
329 {
330 lock l (update);
331 if (!ring_buffer<M>::empty ())
332 {
334 while (sema.is_signaled ()) //drain semaphore
335 ;
336 }
337 }
338
339protected:
342};
343
344
345} // namespace mlib
size_t size() const
Return buffer size.
Definition syncque.h:322
async_buffer(size_t limit_)
Creates a buffer with the given maximum size.
Definition syncque.h:237
bool full() const
Return true if buffer is at capacity.
Definition syncque.h:316
criticalsection update
critical section protects buffer's integrity
Definition syncque.h:341
void clear()
Remove all elements from buffer.
Definition syncque.h:328
semaphore sema
semaphore counts buffer size
Definition syncque.h:340
virtual void produce(const M &obj)
Append an element to buffer.
Definition syncque.h:243
virtual bool consume(M &result, int timeout=INFINITE)
Definition syncque.h:254
bool empty()
Return true if buffer is empty.
Definition syncque.h:310
bool empty()
Return true if queue is empty.
Definition syncque.h:198
virtual bool consume(M &result, int timeout=INFINITE)
Definition syncque.h:151
bool full()
Return true if queue is at capacity.
Definition syncque.h:205
semaphore prod_sema
producers' semaphore counts down until queue is full
Definition syncque.h:220
semaphore cons_sema
consumers' semaphore counts down until queue is empty
Definition syncque.h:221
size_t size()
Return queue size.
Definition syncque.h:212
criticalsection update
critical section protects queue's integrity
Definition syncque.h:222
size_t limit
maximum queue size
Definition syncque.h:219
virtual bool produce(const M &obj, DWORD timeout=INFINITE)
Definition syncque.h:103
async_queue(size_t limit_=INFINITE)
Crates a queue with the given maximum size.
Definition syncque.h:94
Lightweight inter-thread synchronization.
Definition critsect.h:27
virtual void enter()
Enter critical section.
Definition critsect.h:91
virtual void leave()
Leave critical section.
Definition critsect.h:103
Automatic wrapper for critical sections.
Definition critsect.h:67
Circular buffer.
Definition ringbuf.h:24
void pop_front()
Remove oldest element from buffer.
Definition ringbuf.h:452
void clear(void)
Remove all elements from buffer.
Definition ringbuf.h:533
size_t size() const
Return number of elements in buffer.
Definition ringbuf.h:577
T & front()
Return a reference to first (oldest) element in buffer.
Definition ringbuf.h:463
void push_back(const T &item)
Inserts new element in buffer.
Definition ringbuf.h:402
bool empty(void) const
Return true if buffer is empty.
Definition ringbuf.h:540
bool full(void) const
Return true if buffer is full.
Definition ringbuf.h:546
ring_buffer(size_t size)
Definition ringbuf.h:297
Wrapper for Windows semaphore objects.
Definition semaphore.h:16
int signal(int count=1)
Signal a semaphore object.
Definition semaphore.cpp:40
Simple stopwatch timer (yet another one!)
Definition stopwatch.h:20
double lap_msec()
Return number of milliseconds elapsed from start.
Definition stopwatch.h:82
void start()
Start the stopwatch.
Definition stopwatch.h:43
sync_queue()
Create a synchronous queue.
Definition syncque.h:39
void produce(const M &obj)
Put new element in queue.
Definition syncque.h:44
virtual void wait()
Wait for object to become signaled.
Definition syncbase.h:85
Definitions of mlib::criticalsection and mlib::lock classes.
Definition of mlib::semaphore class.
Definition of mlib::stopwatch class.