Skip to content
Newer
Older
100644 114 lines (93 sloc) 2.6 KB
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
1 #ifndef CQUEUE_H
2 #define CQUEUE_H
3
4 #include <spitfire/util/thread.h>
5
6 namespace spitfire
7 {
8 namespace util
9 {
10 // ** cQueue
11 // A general purpose queue typically used between a producer and consumer
12 template <class T>
13 class cQueue
14 {
15 public:
16 ~cQueue();
17
18 bool IsEmpty() const { return items.empty(); }
19 size_t GetSize() const { return items.size(); }
20
21 void AddItemToBack(T* pItem); // The queue takes ownership of pItem
22 T* RemoveItemFromFront(); // The caller takes ownership of the item that is returned
23
24 private:
25 std::list<T*> items;
26 };
27
28 template <class T>
29 inline cQueue<T>::~cQueue()
30 {
31 // NOTE: All items must be removed before the queue is destroyed
32 ASSERT(items.empty());
33 }
34
35 template <class T>
36 inline void cQueue<T>::AddItemToBack(T* pItem)
37 {
38 items.push_back(pItem);
39 }
40
41 template <class T>
42 inline T* cQueue<T>::RemoveItemFromFront()
43 {
44 ASSERT(!items.empty());
45 return items.pop_front();
46 }
47
48
49 // ** cThreadSafeQueue
50 // Exactly the same as cQueue except that the items are protected with a mutex
51 template <class T>
52 class cThreadSafeQueue
53 {
54 public:
5f42d39 @pilkch spitfire thread Introduced soAction which can be triggered by multipl…
authored Mar 13, 2012
55 explicit cThreadSafeQueue(cSignalObject& soAction);
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
56 ~cThreadSafeQueue();
57
58 // NOTE: No IsEmpty or GetSize functions because by the time you call the next function this may have changed
59
60 void AddItemToBack(T* pItem); // The queue takes ownership of pItem
61 T* RemoveItemFromFront(); // The caller takes ownership of the item that is returned
62
63 private:
5f42d39 @pilkch spitfire thread Introduced soAction which can be triggered by multipl…
authored Mar 13, 2012
64 cSignalObject& soAction;
65
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
66 cMutex mutex;
67 std::list<T*> items;
68 };
69
70 template <class T>
5f42d39 @pilkch spitfire thread Introduced soAction which can be triggered by multipl…
authored Mar 13, 2012
71 inline cThreadSafeQueue<T>::cThreadSafeQueue(cSignalObject& _soAction) :
72 soAction(_soAction),
e65b448 @pilkch spitfire Compiled.
authored Feb 27, 2012
73 mutex("cThreadSafeQueue<T>::mutex")
74 {
75 }
76
77 template <class T>
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
78 inline cThreadSafeQueue<T>::~cThreadSafeQueue()
79 {
e65b448 @pilkch spitfire Compiled.
authored Feb 27, 2012
80 cLockObject lock(mutex);
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
81 // NOTE: All items must be removed before the queue is destroyed
82 ASSERT(items.empty());
83 }
84
85 template <class T>
86 inline void cThreadSafeQueue<T>::AddItemToBack(T* pItem)
87 {
5f42d39 @pilkch spitfire thread Introduced soAction which can be triggered by multipl…
authored Mar 13, 2012
88 {
89 cLockObject lock(mutex);
90 items.push_back(pItem);
91 }
92
93 // Tell anyone listening that something happened
94 soAction.Signal();
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
95 }
96
97 template <class T>
98 inline T* cThreadSafeQueue<T>::RemoveItemFromFront()
99 {
100 T* pItem = nullptr;
101 {
e65b448 @pilkch spitfire Compiled.
authored Feb 27, 2012
102 cLockObject lock(mutex);
5f42d39 @pilkch spitfire thread Introduced soAction which can be triggered by multipl…
authored Mar 13, 2012
103 if (!items.empty()) {
104 pItem = items.front();
105 items.pop_front();
106 }
83f34d2 @pilkch spitfire Added cQueue and cThreadSafeQueue.
authored Jan 10, 2012
107 }
108 return pItem;
109 }
110 }
111 }
112
113 #endif // CQUEUE_H
Something went wrong with that request. Please try again.