[9478] Reimplement Aura::IsNeedVisibleSlot
[getmangos.git] / src / shared / LockedQueue.h
blobb3adb1372f8915fd46f0042b72dc812654acdde3
1 /*
2 * Copyright (C) 2009-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef LOCKEDQUEUE_H
20 #define LOCKEDQUEUE_H
22 #include <ace/Guard_T.h>
23 #include <ace/Thread_Mutex.h>
24 #include <deque>
25 #include <assert.h>
26 #include "Errors.h"
28 namespace ACE_Based
30 template <class T, class LockType, typename StorageType=std::deque<T> >
31 class LockedQueue
33 //! Lock access to the queue.
34 LockType _lock;
36 //! Storage backing the queue.
37 StorageType _queue;
39 //! Cancellation flag.
40 /*volatile*/ bool _canceled;
42 public:
44 //! Create a LockedQueue.
45 LockedQueue()
46 : _canceled(false)
50 //! Destroy a LockedQueue.
51 virtual ~LockedQueue()
55 //! Adds an item to the queue.
56 void add(const T& item)
58 lock();
60 //ASSERT(!this->_canceled);
61 // throw Cancellation_Exception();
63 _queue.push_back(item);
65 unlock();
68 //! Gets the next result in the queue, if any.
69 bool next(T& result)
71 ACE_Guard<LockType> g(this->_lock);
73 if (_queue.empty())
74 return false;
76 //ASSERT (!_queue.empty() || !this->_canceled);
77 // throw Cancellation_Exception();
79 result = _queue.front();
80 _queue.pop_front();
82 return true;
85 //! Peeks at the top of the queue. Remember to unlock after use.
86 T& peek()
88 lock();
90 T& result = _queue.front();
92 return result;
95 //! Cancels the queue.
96 void cancel()
98 lock();
100 _canceled = true;
102 unlock();
105 //! Checks if the queue is cancelled.
106 bool cancelled()
108 ACE_Guard<LockType> g(this->_lock);
110 return _canceled;
113 //! Locks the queue for access.
114 void lock()
116 this->_lock.acquire();
119 //! Unlocks the queue.
120 void unlock()
122 this->_lock.release();
126 #endif