-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_ThreadSafeQueue.h
More file actions
239 lines (207 loc) · 6.8 KB
/
DS_ThreadSafeQueue.h
File metadata and controls
239 lines (207 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef __THREAD_SAFE_QUEUE_H
#define __THREAD_SAFE_QUEUE_H
#include "DS_Queue.h"
#include "SimpleMutex.h"
namespace DataStructures
{
/// \brief A queue implemented as an array with a read and write index.
template <class queue_type>
class ThreadSafeQueue
{
private:
Queue<queue_type> m_queue;
SimpleMutex mutex;
public:
ThreadSafeQueue() {}
ThreadSafeQueue( unsigned int allocation_size );
ThreadSafeQueue( ThreadSafeQueue<queue_type>& original_copy );
ThreadSafeQueue( Queue<queue_type>& original_copy );
Queue<queue_type> *LockQueue();
void ReleaseQueue();
bool operator= ( const Queue<queue_type> &original_copy );
bool operator= ( const ThreadSafeQueue<queue_type> &original_copy );
void Push( const queue_type &input );
void PushAtHead( const queue_type &input, unsigned index=0 );
queue_type& operator[] ( unsigned int position ) const; // Not a normal thing you do with a queue but can be used for efficiency, NEVER this when queue_type is a reference one
void RemoveAtIndex( unsigned int position ); // Not a normal thing you do with a queue but can be used for efficiency
inline queue_type Peek( void ) const;
inline queue_type PeekTail( void ) const;
inline queue_type Pop( void );
inline unsigned int Size( void ) const;
inline bool IsEmpty(void) const;
inline unsigned int AllocationSize( void ) const;
inline void Clear( void );
void Compress( void );
bool Find ( queue_type q );
unsigned int GetItemIndex ( queue_type q );
void ClearAndForceAllocation( int size ); // Force a memory allocation to a certain larger size
unsigned int GetHead(){ unsigned int h = LockQueue()->GetHead(); ReleaseQueue(); return h; };
unsigned int GetTail(){ unsigned int t = LockQueue()->GetTail(); ReleaseQueue(); return t;};
};
template <class queue_type>
Queue<queue_type> *ThreadSafeQueue<queue_type>::LockQueue()
{
mutex.Lock();
return &m_queue;
}
template <class queue_type>
void ThreadSafeQueue<queue_type>::ReleaseQueue()
{
mutex.Unlock();
}
template <class queue_type>
ThreadSafeQueue<queue_type>::ThreadSafeQueue( unsigned int allocation_size )
{
LockQueue()->ClearAndForceAllocation(allocation_size);
ReleaseQueue();
}
template <class queue_type>
ThreadSafeQueue<queue_type>::ThreadSafeQueue( Queue<queue_type>& original_copy )
{
Queue<queue_type> *queue = LockQueue();
*queue = original_copy;
ReleaseQueue();
}
template <class queue_type>
ThreadSafeQueue<queue_type>::ThreadSafeQueue( ThreadSafeQueue<queue_type>& original_copy )
{
Queue<queue_type> *dest = LockQueue();
Queue<queue_type> *source = original_copy.LockQueue();
*dest = *source;
original_copy.ReleaseQueue();
ReleaseQueue();
}
template <class queue_type>
bool ThreadSafeQueue<queue_type>::operator= ( const Queue<queue_type>& original_copy )
{
Queue<queue_type> *queue = LockQueue();
bool res = (*queue = original_copy);
ReleaseQueue();
return res;
}
template <class queue_type>
bool ThreadSafeQueue<queue_type>::operator= ( const ThreadSafeQueue<queue_type>& original_copy )
{
Queue<queue_type> *dest = LockQueue();
Queue<queue_type> *source = original_copy.LockQueue();
bool res = (*dest = *source);
original_copy.ReleaseQueue();
ReleaseQueue();
return res;
}
template <class queue_type>
inline unsigned int ThreadSafeQueue<queue_type>::Size( void ) const
{
Queue<queue_type> *queue = ((ThreadSafeQueue<queue_type> *)(this))->LockQueue();
unsigned int size = queue->Size();
((ThreadSafeQueue<queue_type> *)(this))->ReleaseQueue();
return size;
}
template <class queue_type>
inline bool ThreadSafeQueue<queue_type>::IsEmpty(void) const
{
Queue<queue_type> *queue = ((ThreadSafeQueue<queue_type> *)(this))->LockQueue();
bool isEmpty = queue->IsEmpty();
((ThreadSafeQueue<queue_type> *)(this))->ReleaseQueue();
return isEmpty;
}
template <class queue_type>
inline unsigned int ThreadSafeQueue<queue_type>::AllocationSize( void ) const
{
Queue<queue_type> *queue = ((ThreadSafeQueue<queue_type> *)(this))->LockQueue();
unsigned int size = queue->AllocationSize();
((ThreadSafeQueue<queue_type> *)(this))->ReleaseQueue();
return size;
}
template <class queue_type>
inline queue_type ThreadSafeQueue<queue_type>::Pop( void )
{
Queue<queue_type> *queue = LockQueue();
queue_type result = queue->Pop();
ReleaseQueue();
return result;
}
template <class queue_type>
void ThreadSafeQueue<queue_type>::PushAtHead( const queue_type &input, unsigned index )
{
Queue<queue_type> *queue = LockQueue();
queue->PushAtHead(input, index);
ReleaseQueue();
}
template <class queue_type>
inline queue_type ThreadSafeQueue<queue_type>::Peek( void ) const
{
Queue<queue_type> *queue = ((ThreadSafeQueue<queue_type> *)(this))->LockQueue();
queue_type result = queue->Peek();
((ThreadSafeQueue<queue_type> *)(this))->ReleaseQueue();
return result;
}
template <class queue_type>
inline queue_type ThreadSafeQueue<queue_type>::PeekTail( void ) const
{
Queue<queue_type> *queue = ((ThreadSafeQueue<queue_type> *)(this))->LockQueue();
queue_type result = queue->PeekTail();
((ThreadSafeQueue<queue_type> *)(this))->ReleaseQueue();
return result;
}
template <class queue_type>
void ThreadSafeQueue<queue_type>::Push( const queue_type &input )
{
Queue<queue_type> *queue = LockQueue();
queue->Push(input);
ReleaseQueue();
}
template <class queue_type>
inline void ThreadSafeQueue<queue_type>::Clear ( void )
{
Queue<queue_type> *queue = LockQueue();
queue->Clear();
ReleaseQueue();
}
template <class queue_type>
void ThreadSafeQueue<queue_type>::Compress ( void )
{
Queue<queue_type> *queue = LockQueue();
queue->Compress();
ReleaseQueue();
}
template <class queue_type>
bool ThreadSafeQueue<queue_type>::Find ( queue_type q )
{
Queue<queue_type> *queue = LockQueue();
bool result = queue->Find(q);
ReleaseQueue();
return result;
}
template <class queue_type>
unsigned int ThreadSafeQueue<queue_type>::GetItemIndex ( queue_type q )
{
Queue<queue_type> *queue = LockQueue();
unsigned int result = queue->GetItemIndex(q);
ReleaseQueue();
return result;
}
template <class queue_type>
void ThreadSafeQueue<queue_type>::ClearAndForceAllocation( int size )
{
Queue<queue_type> *queue = LockQueue();
queue->ClearAndForceAllocation(size);
ReleaseQueue();
}
template <class queue_type>
inline queue_type& ThreadSafeQueue<queue_type>::operator[] ( unsigned int position ) const
{
Queue<queue_type> *queue = ((ThreadSafeQueue<queue_type> *)(this))->LockQueue();
queue_type &result = (*queue)[position];
((ThreadSafeQueue<queue_type> *)(this))->ReleaseQueue();
return result;
}
template <class queue_type>
void ThreadSafeQueue<queue_type>::RemoveAtIndex( unsigned int position )
{
Queue<queue_type> *queue = LockQueue();
queue->RemoveAtIndex(position);
ReleaseQueue();
}
} // End namespace
#endif