TDME2 1.9.121
RealtimeQueue.h
Go to the documentation of this file.
1#pragma once
2
3#include <tdme/tdme.h>
5
6#include <queue>
7
9
10using std::queue;
12
13namespace tdme {
14namespace os {
15namespace threading {
16
17/**
18 * Realtime consumer/producer queue.
19 * @author Andreas Drewke
20 */
21template <typename T>
23public:
24 /**
25 * @brief Public constructor
26 */
27 RealtimeQueue(const unsigned int maxElements) :
29 m("queue"),
30 stopRequested(false) {
31 //
32 }
33
34 /**
35 * @brief Destructor, removes remaining elements from queue
36 */
37 virtual ~RealtimeQueue() {
38 while (data.size() > 0) {
39 T* element = data.front();
40 delete element;
41 data.pop();
42 }
43 }
44
45 /**
46 * @brief Requests this queue to be stopped
47 */
48 void stop() {
49 stopRequested = true;
50 }
51
52 /**
53 * @brief Gets an element from this queue, if no element exists it will immediately return NULL
54 * @return T*
55 */
57 m.lock();
58 if (stopRequested == true) {
59 m.unlock();
60 return NULL;
61 } else
62 if (data.size() == 0) {
63 m.unlock();
64 return NULL;
65 } else {
66 T* element = data.front();
67 data.pop();
68 m.unlock();
69 return element;
70 }
71 }
72
73 /**
74 * @brief Adds an element to this queue
75 * @param element T* element
76 * @param declinable bool if element is declinable
77 * @return if element was added
78 */
79 bool addElement(T* element, const bool declinable) {
80 m.lock();
81 if (declinable == true && data.size() > maxElements) {
82 m.unlock();
83 return false;
84 }
85 data.push(element);
86 m.unlock();
87 return true;
88 }
89
90protected:
91 typedef queue<T*> RealtimeQueueType;
93 unsigned int maxElements;
94
95private:
97 volatile bool stopRequested;
98
99};
100
101};
102};
103};
Realtime consumer/producer queue.
Definition: RealtimeQueue.h:22
RealtimeQueue(const unsigned int maxElements)
Public constructor.
Definition: RealtimeQueue.h:27
bool addElement(T *element, const bool declinable)
Adds an element to this queue.
Definition: RealtimeQueue.h:79
T * getElement()
Gets an element from this queue, if no element exists it will immediately return NULL.
Definition: RealtimeQueue.h:56
void stop()
Requests this queue to be stopped.
Definition: RealtimeQueue.h:48
virtual ~RealtimeQueue()
Destructor, removes remaining elements from queue.
Definition: RealtimeQueue.h:37
Spin Lock implementation.
Definition: SpinLock.h:16
void unlock()
Unlocks this spin lock.
Definition: SpinLock.h:47
void lock()
Locks the spin lock, additionally spin lock locks will block until other locks have been unlocked.
Definition: SpinLock.h:40
Definition: fwd-tdme.h:4