[8463] Fixed race conditions in LockedQueue.

Signed-off-by: ApoC <apoc@nymfe.net>
This commit is contained in:
XTZGZoReX 2009-08-30 23:23:46 +02:00 committed by ApoC
parent 7f444189c6
commit 66ffd80ed2
6 changed files with 42 additions and 86 deletions

View file

@ -26,7 +26,6 @@ SqlDelayThread::SqlDelayThread(Database* db) : m_dbEngine(db), m_running(true)
void SqlDelayThread::run()
{
SqlOperation* s;
#ifndef DO_POSTGRESQL
mysql_thread_init();
#endif
@ -36,9 +35,9 @@ void SqlDelayThread::run()
// if the running state gets turned off while sleeping
// empty the queue before exiting
ACE_Based::Thread::Sleep(10);
while (!m_sqlQueue.empty())
SqlOperation* s;
while (m_sqlQueue.next(s))
{
s = m_sqlQueue.next();
s->Execute(m_dbEngine);
delete s;
}

View file

@ -71,9 +71,9 @@ void SqlQuery::Execute(Database *db)
void SqlResultQueue::Update()
{
/// execute the callbacks waiting in the synchronization queue
while(!empty())
MaNGOS::IQueryCallback* callback;
while (next(callback))
{
MaNGOS::IQueryCallback * callback = next();
callback->Execute();
delete callback;
}

View file

@ -30,99 +30,65 @@ namespace ACE_Based
template <class T, class LockType, typename StorageType=std::deque<T> >
class LockedQueue
{
//! Serialize access to the Queue
//! Lock access to the queue.
LockType _lock;
//! Storage backing the queue
//! Storage backing the queue.
StorageType _queue;
//! Cancellation flag
volatile bool _canceled;
//! Cancellation flag.
/*volatile*/ bool _canceled;
public:
//! Create a LockedQueue
//! Create a LockedQueue.
LockedQueue() : _canceled(false) {}
//! Destroy a LockedQueue
//! Destroy a LockedQueue.
virtual ~LockedQueue() { }
/**
* @see Queue::add(const T& item)
*/
//! Adds an item to the queue.
void add(const T& item)
{
ACE_Guard<LockType> g(this->_lock);
ASSERT(!this->_canceled);
//ASSERT(!this->_canceled);
// throw Cancellation_Exception();
this->_queue.push_back(item);
_queue.push_back(item);
}
/**
* @see Queue::next()
*/
T next()
//! Gets the next result in the queue, if any.
bool next(T& result)
{
ACE_Guard<LockType> g(this->_lock);
ASSERT (!_queue.empty() || !this->_canceled);
if (_queue.empty())
return false;
//ASSERT (!_queue.empty() || !this->_canceled);
// throw Cancellation_Exception();
T item = this->_queue.front();
this->_queue.pop_front();
result = _queue.front();
_queue.pop_front();
return item;
return true;
}
T front()
{
ACE_Guard<LockType> g(this->_lock);
ASSERT (!this->_queue.empty());
// throw NoSuchElement_Exception();
return this->_queue.front();
}
/**
* @see Queue::cancel()
*/
//! Cancels the queue.
void cancel()
{
ACE_Guard<LockType> g(this->_lock);
this->_canceled = true;
_canceled = true;
}
/**
* @see Queue::isCanceled()
*/
bool isCanceled()
{
// Faster check since the queue will not become un-canceled
if(this->_canceled)
return true;
ACE_Guard<LockType> g(this->_lock);
return this->_canceled;
}
/**
* @see Queue::size()
*/
size_t size()
//! Checks if the queue is cancelled.
bool cancelled()
{
ACE_Guard<LockType> g(this->_lock);
return this->_queue.size();
}
bool empty()
{
ACE_Guard<LockType> g(this->_lock);
return this->_queue.empty();
return _canceled;
}
};
}

View file

@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "8462"
#define REVISION_NR "8463"
#endif // __REVISION_NR_H__