diff options
Diffstat (limited to 'src/stable/synchroqueue.h')
| -rw-r--r-- | src/stable/synchroqueue.h | 100 |
1 files changed, 96 insertions, 4 deletions
diff --git a/src/stable/synchroqueue.h b/src/stable/synchroqueue.h index 1c39c2c..b10ec33 100644 --- a/src/stable/synchroqueue.h +++ b/src/stable/synchroqueue.h | |||
| @@ -44,7 +44,8 @@ namespace Bu | |||
| 44 | SynchroQueue() : | 44 | SynchroQueue() : |
| 45 | pStart( NULL ), | 45 | pStart( NULL ), |
| 46 | pEnd( NULL ), | 46 | pEnd( NULL ), |
| 47 | nSize( 0 ) | 47 | nSize( 0 ), |
| 48 | bRunning( true ) | ||
| 48 | { | 49 | { |
| 49 | } | 50 | } |
| 50 | 51 | ||
| @@ -76,6 +77,14 @@ namespace Bu | |||
| 76 | */ | 77 | */ |
| 77 | void enqueue( T pData ) | 78 | void enqueue( T pData ) |
| 78 | { | 79 | { |
| 80 | mRunning.lock(); | ||
| 81 | if( !bRunning ) | ||
| 82 | { | ||
| 83 | mRunning.unlock(); | ||
| 84 | throw Bu::ExceptionBase("SynchoQueue is stopped."); | ||
| 85 | } | ||
| 86 | mRunning.unlock(); | ||
| 87 | |||
| 79 | cBlock.lock(); | 88 | cBlock.lock(); |
| 80 | 89 | ||
| 81 | if( pStart == NULL ) | 90 | if( pStart == NULL ) |
| @@ -119,6 +128,14 @@ namespace Bu | |||
| 119 | */ | 128 | */ |
| 120 | T dequeue( bool bBlock=false ) | 129 | T dequeue( bool bBlock=false ) |
| 121 | { | 130 | { |
| 131 | mRunning.lock(); | ||
| 132 | if( !bRunning ) | ||
| 133 | { | ||
| 134 | mRunning.unlock(); | ||
| 135 | return T(); | ||
| 136 | } | ||
| 137 | mRunning.unlock(); | ||
| 138 | |||
| 122 | cBlock.lock(); | 139 | cBlock.lock(); |
| 123 | if( pStart == NULL ) | 140 | if( pStart == NULL ) |
| 124 | { | 141 | { |
| @@ -129,8 +146,18 @@ namespace Bu | |||
| 129 | if( pStart == NULL ) | 146 | if( pStart == NULL ) |
| 130 | { | 147 | { |
| 131 | cBlock.unlock(); | 148 | cBlock.unlock(); |
| 132 | return NULL; | 149 | return T(); |
| 133 | } | 150 | } |
| 151 | |||
| 152 | mRunning.lock(); | ||
| 153 | if( !bRunning ) | ||
| 154 | { | ||
| 155 | mRunning.unlock(); | ||
| 156 | cBlock.unlock(); | ||
| 157 | return T(); | ||
| 158 | } | ||
| 159 | mRunning.unlock(); | ||
| 160 | |||
| 134 | T pTmp = pStart->pData; | 161 | T pTmp = pStart->pData; |
| 135 | Item *pDel = pStart; | 162 | Item *pDel = pStart; |
| 136 | pStart = pStart->pNext; | 163 | pStart = pStart->pNext; |
| @@ -143,7 +170,7 @@ namespace Bu | |||
| 143 | } | 170 | } |
| 144 | 171 | ||
| 145 | cBlock.unlock(); | 172 | cBlock.unlock(); |
| 146 | return NULL; | 173 | return T(); |
| 147 | } | 174 | } |
| 148 | else | 175 | else |
| 149 | { | 176 | { |
| @@ -171,6 +198,14 @@ namespace Bu | |||
| 171 | */ | 198 | */ |
| 172 | T dequeue( int nSec, int nUSec ) | 199 | T dequeue( int nSec, int nUSec ) |
| 173 | { | 200 | { |
| 201 | mRunning.lock(); | ||
| 202 | if( !bRunning ) | ||
| 203 | { | ||
| 204 | mRunning.unlock(); | ||
| 205 | return T(); | ||
| 206 | } | ||
| 207 | mRunning.unlock(); | ||
| 208 | |||
| 174 | cBlock.lock(); | 209 | cBlock.lock(); |
| 175 | if( pStart == NULL ) | 210 | if( pStart == NULL ) |
| 176 | { | 211 | { |
| @@ -179,8 +214,17 @@ namespace Bu | |||
| 179 | if( pStart == NULL ) | 214 | if( pStart == NULL ) |
| 180 | { | 215 | { |
| 181 | cBlock.unlock(); | 216 | cBlock.unlock(); |
| 182 | return NULL; | 217 | return T(); |
| 218 | } | ||
| 219 | |||
| 220 | mRunning.lock(); | ||
| 221 | if( !bRunning ) | ||
| 222 | { | ||
| 223 | mRunning.unlock(); | ||
| 224 | cBlock.unlock(); | ||
| 225 | return T(); | ||
| 183 | } | 226 | } |
| 227 | mRunning.unlock(); | ||
| 184 | 228 | ||
| 185 | T pTmp = pStart->pData; | 229 | T pTmp = pStart->pData; |
| 186 | Item *pDel = pStart; | 230 | Item *pDel = pStart; |
| @@ -203,6 +247,35 @@ namespace Bu | |||
| 203 | return pTmp; | 247 | return pTmp; |
| 204 | } | 248 | } |
| 205 | } | 249 | } |
| 250 | |||
| 251 | T drain() | ||
| 252 | { | ||
| 253 | mRunning.lock(); | ||
| 254 | if( bRunning ) | ||
| 255 | { | ||
| 256 | mRunning.unlock(); | ||
| 257 | return NULL; | ||
| 258 | } | ||
| 259 | mRunning.unlock(); | ||
| 260 | |||
| 261 | cBlock.lock(); | ||
| 262 | if( pStart == NULL ) | ||
| 263 | { | ||
| 264 | cBlock.unlock(); | ||
| 265 | return T(); | ||
| 266 | } | ||
| 267 | else | ||
| 268 | { | ||
| 269 | T pTmp = pStart->pData; | ||
| 270 | Item *pDel = pStart; | ||
| 271 | pStart = pStart->pNext; | ||
| 272 | delete pDel; | ||
| 273 | nSize--; | ||
| 274 | |||
| 275 | cBlock.unlock(); | ||
| 276 | return pTmp; | ||
| 277 | } | ||
| 278 | } | ||
| 206 | 279 | ||
| 207 | /** | 280 | /** |
| 208 | * Checks to see if the queue has data in it or not. Note that there | 281 | * Checks to see if the queue has data in it or not. Note that there |
| @@ -235,12 +308,31 @@ namespace Bu | |||
| 235 | cBlock.unlock(); | 308 | cBlock.unlock(); |
| 236 | } | 309 | } |
| 237 | 310 | ||
| 311 | void stop() | ||
| 312 | { | ||
| 313 | mRunning.lock(); | ||
| 314 | bRunning = false; | ||
| 315 | mRunning.unlock(); | ||
| 316 | unblockAll(); | ||
| 317 | } | ||
| 318 | |||
| 319 | bool isRunning() const | ||
| 320 | { | ||
| 321 | bool bRet; | ||
| 322 | mRunning.lock(); | ||
| 323 | bRet = bRunning; | ||
| 324 | mRunning.unlock(); | ||
| 325 | return bRet; | ||
| 326 | } | ||
| 327 | |||
| 238 | private: | 328 | private: |
| 239 | Item *pStart; /**< The start of the queue, the next element to dequeue. */ | 329 | Item *pStart; /**< The start of the queue, the next element to dequeue. */ |
| 240 | Item *pEnd; /**< The end of the queue, the last element to dequeue. */ | 330 | Item *pEnd; /**< The end of the queue, the last element to dequeue. */ |
| 241 | long nSize; /**< The number of items in the queue. */ | 331 | long nSize; /**< The number of items in the queue. */ |
| 242 | 332 | ||
| 243 | Condition cBlock; /**< The condition for blocking dequeues. */ | 333 | Condition cBlock; /**< The condition for blocking dequeues. */ |
| 334 | mutable Mutex mRunning; | ||
| 335 | bool bRunning; | ||
| 244 | }; | 336 | }; |
| 245 | } | 337 | } |
| 246 | 338 | ||
