SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DS_ThreadsafeAllocatingQueue.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
19
20 #ifndef __THREADSAFE_ALLOCATING_QUEUE
21 #define __THREADSAFE_ALLOCATING_QUEUE
22
23 #include "DS_Queue.h"
24 #include "SimpleMutex.h"
25 #include "DS_MemoryPool.h"
26
27 // #if defined(new)
28 // #pragma push_macro("new")
29 // #undef new
30 // #define RMO_NEW_UNDEF_ALLOCATING_QUEUE
31 // #endif
32
33 namespace DataStructures
34 {
35
36 template <class structureType>
37 class RAK_DLL_EXPORT ThreadsafeAllocatingQueue
38 {
39 public:
40 // Queue operations
41 void Push(structureType *s);
42 structureType *PopInaccurate(void);
43 structureType *Pop(void);
44 void SetPageSize(int size);
45 bool IsEmpty(void);
46 structureType * operator[] ( unsigned int position );
47 void RemoveAtIndex( unsigned int position );
48 unsigned int Size( void );
49
50 // Memory pool operations
51 structureType *Allocate(const char *file, unsigned int line);
52 void Deallocate(structureType *s, const char *file, unsigned int line);
53 void Clear(const char *file, unsigned int line);
54 protected:
55
56 mutable MemoryPool<structureType> memoryPool;
57 SLNet::SimpleMutex memoryPoolMutex;
58 Queue<structureType*> queue;
59 SLNet::SimpleMutex queueMutex;
60 };
61
62 template <class structureType>
63 void ThreadsafeAllocatingQueue<structureType>::Push(structureType *s)
64 {
65 queueMutex.Lock();
66 queue.Push(s, _FILE_AND_LINE_ );
67 queueMutex.Unlock();
68 }
69
70 template <class structureType>
71 structureType *ThreadsafeAllocatingQueue<structureType>::PopInaccurate(void)
72 {
73 structureType *s;
74 if (queue.IsEmpty())
75 return 0;
76 queueMutex.Lock();
77 if (queue.IsEmpty()==false)
78 s=queue.Pop();
79 else
80 s=0;
81 queueMutex.Unlock();
82 return s;
83 }
84
85 template <class structureType>
86 structureType *ThreadsafeAllocatingQueue<structureType>::Pop(void)
87 {
88 structureType *s;
89 queueMutex.Lock();
90 if (queue.IsEmpty())
91 {
92 queueMutex.Unlock();
93 return 0;
94 }
95 s=queue.Pop();
96 queueMutex.Unlock();
97 return s;
98 }
99
100 template <class structureType>
101 structureType *ThreadsafeAllocatingQueue<structureType>::Allocate(const char *file, unsigned int line)
102 {
103 structureType *s;
104 memoryPoolMutex.Lock();
105 s=memoryPool.Allocate(file, line);
106 memoryPoolMutex.Unlock();
107 // Call new operator, memoryPool doesn't do this
108 s = new ((void*)s) structureType;
109 return s;
110 }
111 template <class structureType>
112 void ThreadsafeAllocatingQueue<structureType>::Deallocate(structureType *s, const char *file, unsigned int line)
113 {
114 // Call delete operator, memory pool doesn't do this
115 s->~structureType();
116 memoryPoolMutex.Lock();
117 memoryPool.Release(s, file, line);
118 memoryPoolMutex.Unlock();
119 }
120
121 template <class structureType>
122 void ThreadsafeAllocatingQueue<structureType>::Clear(const char *file, unsigned int line)
123 {
124 memoryPoolMutex.Lock();
125 for (unsigned int i=0; i < queue.Size(); i++)
126 {
127 queue[i]->~structureType();
128 memoryPool.Release(queue[i], file, line);
129 }
130 queue.Clear(file, line);
131 memoryPoolMutex.Unlock();
132 memoryPoolMutex.Lock();
133 memoryPool.Clear(file, line);
134 memoryPoolMutex.Unlock();
135 }
136
137 template <class structureType>
138 void ThreadsafeAllocatingQueue<structureType>::SetPageSize(int size)
139 {
140 memoryPool.SetPageSize(size);
141 }
142
143 template <class structureType>
144 bool ThreadsafeAllocatingQueue<structureType>::IsEmpty(void)
145 {
146 bool isEmpty;
147 queueMutex.Lock();
148 isEmpty=queue.IsEmpty();
149 queueMutex.Unlock();
150 return isEmpty;
151 }
152
153 template <class structureType>
154 structureType * ThreadsafeAllocatingQueue<structureType>::operator[] ( unsigned int position )
155 {
156 structureType *s;
157 queueMutex.Lock();
158 s=queue[position];
159 queueMutex.Unlock();
160 return s;
161 }
162
163 template <class structureType>
164 void ThreadsafeAllocatingQueue<structureType>::RemoveAtIndex( unsigned int position )
165 {
166 queueMutex.Lock();
167 queue.RemoveAtIndex(position);
168 queueMutex.Unlock();
169 }
170
171 template <class structureType>
172 unsigned int ThreadsafeAllocatingQueue<structureType>::Size( void )
173 {
174 unsigned int s;
175 queueMutex.Lock();
176 s=queue.Size();
177 queueMutex.Unlock();
178 return s;
179 }
180
181 }
182
183
184 // #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE)
185 // #pragma pop_macro("new")
186 // #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE
187 // #endif
188
189 #endif