| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
SyncQueue.h |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
Declaration/Implementation of cSyncQueue class |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
cSyncQueue |
| 10 |
Synchronized shareable (across multiple threads) queue of |
| 11 |
structures with event notifications |
| 12 |
|
| 13 |
Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 14 |
|
| 15 |
Redistribution and use in source and binary forms, with or without |
| 16 |
modification, are permitted provided that the following conditions are met: |
| 17 |
* Redistributions of source code must retain the above copyright |
| 18 |
notice, this list of conditions and the following disclaimer. |
| 19 |
* Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
* Neither the name of Code Aurora Forum nor |
| 23 |
the names of its contributors may be used to endorse or promote |
| 24 |
products derived from this software without specific prior written |
| 25 |
permission. |
| 26 |
|
| 27 |
|
| 28 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 29 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 32 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 33 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 36 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 37 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 38 |
POSSIBILITY OF SUCH DAMAGE. |
| 39 |
===========================================================================*/ |
| 40 |
|
| 41 |
//--------------------------------------------------------------------------- |
| 42 |
// Pragmas |
| 43 |
//--------------------------------------------------------------------------- |
| 44 |
#pragma once |
| 45 |
|
| 46 |
//--------------------------------------------------------------------------- |
| 47 |
// Include Files |
| 48 |
//--------------------------------------------------------------------------- |
| 49 |
#include <deque> |
| 50 |
#include "Event.h" |
| 51 |
|
| 52 |
//--------------------------------------------------------------------------- |
| 53 |
// Definitions |
| 54 |
//--------------------------------------------------------------------------- |
| 55 |
|
| 56 |
/*=========================================================================*/ |
| 57 |
// Class cSyncQueue |
| 58 |
/*=========================================================================*/ |
| 59 |
template <class tElementType> class cSyncQueue |
| 60 |
{ |
| 61 |
public: |
| 62 |
// (Inline) Constructor |
| 63 |
cSyncQueue( |
| 64 |
ULONG maxElements, |
| 65 |
bool bSignalEvent = false ) |
| 66 |
: mSignature( (ULONG)eSYNC_QUEUE_SIG ), |
| 67 |
mSignalEvent(), |
| 68 |
mbSignalEvent( bSignalEvent ), |
| 69 |
mMaxElements( maxElements ), |
| 70 |
mTotalElements( 0 ) |
| 71 |
{ |
| 72 |
// Create sync CS |
| 73 |
int nRet = pthread_mutex_init( &mSyncSection, NULL ); |
| 74 |
if (nRet != 0) |
| 75 |
{ |
| 76 |
TRACE( "SyncQueue: Unable to init sync mutex. Error %d: %s\n", |
| 77 |
nRet, |
| 78 |
strerror( nRet ) ); |
| 79 |
return; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
// (Inline) Destructor |
| 84 |
~cSyncQueue() |
| 85 |
{ |
| 86 |
if (IsValid() == false) |
| 87 |
{ |
| 88 |
ASSERT( (PVOID)"Double deletion detected in ~cSyncQueue" == 0 ); |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
EmptyQueue(); |
| 93 |
|
| 94 |
mSignature = 0; |
| 95 |
int nRet = pthread_mutex_destroy( &mSyncSection ); |
| 96 |
if (nRet != 0) |
| 97 |
{ |
| 98 |
TRACE( "SyncQueue: Unable to destroy sync mutex." |
| 99 |
" Error %d: %s\n", |
| 100 |
nRet, |
| 101 |
strerror( nRet ) ); |
| 102 |
return; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
}; |
| 107 |
|
| 108 |
// (Inline) Add an element to the queue |
| 109 |
bool AddElement( const tElementType & elem ) |
| 110 |
{ |
| 111 |
// Assume failure |
| 112 |
bool bRC = false; |
| 113 |
if (IsValid() == false) |
| 114 |
{ |
| 115 |
ASSERT( (PVOID)"Bad cSyncQueue object detected" == 0 ); |
| 116 |
return bRC; |
| 117 |
} |
| 118 |
|
| 119 |
int nRet = pthread_mutex_lock( &mSyncSection ); |
| 120 |
if (nRet != 0) |
| 121 |
{ |
| 122 |
TRACE( "SyncQueue: Unable to lock sync mutex. Error %d: %s\n", |
| 123 |
nRet, |
| 124 |
strerror( nRet ) ); |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
// Are we out of space? |
| 129 |
if ((ULONG)mElementDeque.size() >= mMaxElements) |
| 130 |
{ |
| 131 |
// Yes, drop oldest element |
| 132 |
mElementDeque.pop_front(); |
| 133 |
} |
| 134 |
|
| 135 |
// Add new item to the queue |
| 136 |
mElementDeque.push_back( elem ); |
| 137 |
mTotalElements++; |
| 138 |
|
| 139 |
// Set event? |
| 140 |
if (mbSignalEvent == true) |
| 141 |
{ |
| 142 |
// Signal index of event |
| 143 |
nRet = mSignalEvent.Set( mTotalElements - 1 ); |
| 144 |
if (nRet != 0) |
| 145 |
{ |
| 146 |
TRACE( "SyncQueue: Unable to signal. Error %d: %s\n", |
| 147 |
nRet, |
| 148 |
strerror( nRet ) ); |
| 149 |
return false; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Success! |
| 154 |
bRC = true; |
| 155 |
|
| 156 |
nRet = pthread_mutex_unlock( &mSyncSection ); |
| 157 |
if (nRet != 0) |
| 158 |
{ |
| 159 |
TRACE( "SyncQueue: Unable to unlock sync mutex. Error %d: %s\n", |
| 160 |
nRet, |
| 161 |
strerror( nRet ) ); |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
return bRC; |
| 166 |
}; |
| 167 |
|
| 168 |
// (Inline) Add an element to the queue returning the index of |
| 169 |
// the element |
| 170 |
bool AddElement( |
| 171 |
const tElementType & elem, |
| 172 |
ULONG & idx ) |
| 173 |
{ |
| 174 |
// Assume failure |
| 175 |
bool bRC = false; |
| 176 |
if (IsValid() == false) |
| 177 |
{ |
| 178 |
ASSERT( (PVOID)"Bad cSyncQueue object detected" == 0 ); |
| 179 |
return bRC; |
| 180 |
} |
| 181 |
|
| 182 |
int nRet = pthread_mutex_lock( &mSyncSection ); |
| 183 |
if (nRet != 0) |
| 184 |
{ |
| 185 |
TRACE( "SyncQueue: Unable to lock sync mutex. Error %d: %s\n", |
| 186 |
nRet, |
| 187 |
strerror( nRet ) ); |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
// Are we out of space? |
| 192 |
if ((ULONG)mElementDeque.size() >= mMaxElements) |
| 193 |
{ |
| 194 |
mElementDeque.pop_front(); |
| 195 |
} |
| 196 |
|
| 197 |
// Add new item to the queue |
| 198 |
mElementDeque.push_back( elem ); |
| 199 |
idx = mTotalElements++; |
| 200 |
|
| 201 |
// Set event? |
| 202 |
if (mbSignalEvent == true) |
| 203 |
{ |
| 204 |
// Signal index of event |
| 205 |
nRet = mSignalEvent.Set( mTotalElements - 1 ); |
| 206 |
if (nRet != 0) |
| 207 |
{ |
| 208 |
TRACE( "SyncQueue: Unable to signal. Error %d: %s\n", |
| 209 |
nRet, |
| 210 |
strerror( nRet ) ); |
| 211 |
return false; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// Success! |
| 216 |
bRC = true; |
| 217 |
|
| 218 |
nRet = pthread_mutex_unlock( &mSyncSection ); |
| 219 |
if (nRet != 0) |
| 220 |
{ |
| 221 |
TRACE( "SyncQueue: Unable to unlock sync mutex. Error %d: %s\n", |
| 222 |
nRet, |
| 223 |
strerror( nRet ) ); |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
return bRC; |
| 228 |
}; |
| 229 |
|
| 230 |
// (Inline) Return given element in the queue |
| 231 |
bool GetElement( |
| 232 |
ULONG idx, |
| 233 |
tElementType & elem ) const |
| 234 |
{ |
| 235 |
// Assume failure |
| 236 |
bool bRC = false; |
| 237 |
if (IsValid() == false) |
| 238 |
{ |
| 239 |
ASSERT( (PVOID)"Bad cSyncQueue object detected" == 0 ); |
| 240 |
return bRC; |
| 241 |
} |
| 242 |
|
| 243 |
int nRet = pthread_mutex_lock( &mSyncSection ); |
| 244 |
if (nRet != 0) |
| 245 |
{ |
| 246 |
TRACE( "SyncQueue: Unable to lock sync mutex. Error %d: %s\n", |
| 247 |
nRet, |
| 248 |
strerror( nRet ) ); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
// Is this a current element index? |
| 253 |
ULONG expiredIndices = mTotalElements - (ULONG)mElementDeque.size(); |
| 254 |
if (idx >= expiredIndices) |
| 255 |
{ |
| 256 |
// Yes, grab it from the deque |
| 257 |
idx -= expiredIndices; |
| 258 |
if (idx < (ULONG)mElementDeque.size()) |
| 259 |
{ |
| 260 |
elem = mElementDeque[idx]; |
| 261 |
bRC = true; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
nRet = pthread_mutex_unlock( &mSyncSection ); |
| 266 |
if (nRet != 0) |
| 267 |
{ |
| 268 |
TRACE( "SyncQueue: Unable to unlock sync mutex. Error %d: %s\n", |
| 269 |
nRet, |
| 270 |
strerror( nRet ) ); |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
return bRC; |
| 275 |
}; |
| 276 |
|
| 277 |
// (Inline) Empty element queue |
| 278 |
bool EmptyQueue() |
| 279 |
{ |
| 280 |
// Assume failure |
| 281 |
bool bRC = false; |
| 282 |
if (IsValid() == false) |
| 283 |
{ |
| 284 |
ASSERT( (PVOID)"Bad cSyncQueue object detected" == 0 ); |
| 285 |
return bRC; |
| 286 |
} |
| 287 |
|
| 288 |
int nRet = pthread_mutex_lock( &mSyncSection ); |
| 289 |
if (nRet != 0) |
| 290 |
{ |
| 291 |
TRACE( "SyncQueue: Unable to lock sync mutex. Error %d: %s\n", |
| 292 |
nRet, |
| 293 |
strerror( nRet ) ); |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
mElementDeque.clear(); |
| 299 |
mTotalElements = 0; |
| 300 |
|
| 301 |
nRet = pthread_mutex_unlock( &mSyncSection ); |
| 302 |
if (nRet != 0) |
| 303 |
{ |
| 304 |
TRACE( "SyncQueue: Unable to unlock sync mutex. Error %d: %s\n", |
| 305 |
nRet, |
| 306 |
strerror( nRet ) ); |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
bRC = true; |
| 311 |
return bRC; |
| 312 |
}; |
| 313 |
|
| 314 |
// (Inline) Return the number of queued elements |
| 315 |
ULONG GetQueueCount() const |
| 316 |
{ |
| 317 |
ULONG elems = 0; |
| 318 |
if (IsValid() == false) |
| 319 |
{ |
| 320 |
ASSERT( (PVOID)"Bad cSyncQueue object detected" == 0 ); |
| 321 |
return elems; |
| 322 |
} |
| 323 |
|
| 324 |
int nRet = pthread_mutex_lock( &mSyncSection ); |
| 325 |
if (nRet != 0) |
| 326 |
{ |
| 327 |
TRACE( "SyncQueue: Unable to lock sync mutex. Error %d: %s\n", |
| 328 |
nRet, |
| 329 |
strerror( nRet ) ); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
elems = (ULONG)mElementDeque.size(); |
| 334 |
|
| 335 |
nRet = pthread_mutex_unlock( &mSyncSection ); |
| 336 |
if (nRet != 0) |
| 337 |
{ |
| 338 |
TRACE( "SyncQueue: Unable to unlock sync mutex. Error %d: %s\n", |
| 339 |
nRet, |
| 340 |
strerror( nRet ) ); |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
return elems; |
| 345 |
}; |
| 346 |
|
| 347 |
// (Inline) Return the total number of elements added to queue |
| 348 |
ULONG GetTotalCount() const |
| 349 |
{ |
| 350 |
ULONG elems = 0; |
| 351 |
if (IsValid() == false) |
| 352 |
{ |
| 353 |
ASSERT( (PVOID)"Bad cSyncQueue object detected" == 0 ); |
| 354 |
return elems; |
| 355 |
} |
| 356 |
|
| 357 |
int nRet = pthread_mutex_lock( &mSyncSection ); |
| 358 |
if (nRet != 0) |
| 359 |
{ |
| 360 |
TRACE( "SyncQueue: Unable to lock sync mutex. Error %d: %s\n", |
| 361 |
nRet, |
| 362 |
strerror( nRet ) ); |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
elems = mTotalElements; |
| 367 |
|
| 368 |
nRet = pthread_mutex_unlock( &mSyncSection ); |
| 369 |
if (nRet != 0) |
| 370 |
{ |
| 371 |
TRACE( "SyncQueue: Unable to unlock sync mutex. Error %d: %s\n", |
| 372 |
nRet, |
| 373 |
strerror( nRet ) ); |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
return elems; |
| 378 |
}; |
| 379 |
|
| 380 |
// (Inline) Return the signal event |
| 381 |
cEvent & GetSignalEvent() const |
| 382 |
{ |
| 383 |
return mSignalEvent; |
| 384 |
}; |
| 385 |
|
| 386 |
// (Inline) Is this sync queue valid? |
| 387 |
bool IsValid() const |
| 388 |
{ |
| 389 |
return (mSignature == (ULONG)eSYNC_QUEUE_SIG); |
| 390 |
}; |
| 391 |
|
| 392 |
protected: |
| 393 |
// Object signature |
| 394 |
enum eClassConstants |
| 395 |
{ |
| 396 |
eSYNC_QUEUE_SIG = 0x1799A2BC |
| 397 |
}; |
| 398 |
|
| 399 |
/* Object signature */ |
| 400 |
ULONG mSignature; |
| 401 |
|
| 402 |
/* Multithreaded mutex type */ |
| 403 |
mutable pthread_mutex_t mSyncSection; |
| 404 |
|
| 405 |
/* Signal event, set everytime an element is added (if configured) */ |
| 406 |
mutable cEvent mSignalEvent; |
| 407 |
|
| 408 |
/* Use above signal event? */ |
| 409 |
bool mbSignalEvent; |
| 410 |
|
| 411 |
/* Maximum number of elements to add to the deque */ |
| 412 |
ULONG mMaxElements; |
| 413 |
|
| 414 |
/* Total number of elements added to the deque */ |
| 415 |
ULONG mTotalElements; |
| 416 |
|
| 417 |
/* Element queue */ |
| 418 |
std::deque <tElementType> mElementDeque; |
| 419 |
}; |