Files
ForeverCore/src/server/shared/Threading/LockedQueue.h
T

140 lines
3.6 KiB
C++
Raw Normal View History

2009-05-14 13:25:35 +02:00
/*
* Copyright (C) 2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
#ifndef LOCKEDQUEUE_H
#define LOCKEDQUEUE_H
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
#include <ace/Guard_T.h>
#include <ace/Thread_Mutex.h>
#include <deque>
#include <assert.h>
2010-06-07 07:43:20 -06:00
#include "Debugging/Errors.h"
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
namespace ACE_Based
{
template <class T, class LockType, typename StorageType=std::deque<T> >
class LockedQueue
{
//! Lock access to the queue.
2009-05-14 13:25:35 +02:00
LockType _lock;
2009-10-17 15:51:44 -07:00
//! Storage backing the queue.
2009-05-14 13:25:35 +02:00
StorageType _queue;
2009-10-17 15:51:44 -07:00
//! Cancellation flag.
2009-05-14 13:25:35 +02:00
volatile bool _canceled;
2009-10-17 15:51:44 -07:00
2009-05-14 13:25:35 +02:00
public:
2009-10-17 15:51:44 -07:00
//! Create a LockedQueue.
LockedQueue()
: _canceled(false)
{
}
2009-10-17 15:51:44 -07:00
//! Destroy a LockedQueue.
virtual ~LockedQueue()
{
}
2009-10-17 15:51:44 -07:00
//! Adds an item to the queue.
2009-05-14 13:25:35 +02:00
void add(const T& item)
{
lock();
2009-10-17 15:51:44 -07:00
//ASSERT(!this->_canceled);
// throw Cancellation_Exception();
2009-10-17 15:51:44 -07:00
_queue.push_back(item);
unlock();
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
//! Gets the next result in the queue, if any.
bool next(T& result)
2009-05-14 13:25:35 +02:00
{
// ACE_Guard<LockType> g(this->_lock);
ACE_GUARD_RETURN (LockType, g, this->_lock, false);
2009-10-17 15:51:44 -07:00
if (_queue.empty())
return false;
2009-10-17 15:51:44 -07:00
//ASSERT (!_queue.empty() || !this->_canceled);
// throw Cancellation_Exception();
result = _queue.front();
_queue.pop_front();
2009-10-17 15:51:44 -07:00
return true;
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
//! Peeks at the top of the queue. Remember to unlock after use.
T& peek()
{
lock();
T& result = _queue.front();
return result;
}
//! Cancels the queue.
2009-05-14 13:25:35 +02:00
void cancel()
{
lock();
2009-10-17 15:51:44 -07:00
_canceled = true;
unlock();
2009-05-14 13:25:35 +02:00
}
2009-10-17 15:51:44 -07:00
//! Checks if the queue is cancelled.
bool cancelled()
2009-05-14 13:25:35 +02:00
{
ACE_Guard<LockType> g(this->_lock);
return _canceled;
2009-05-14 13:25:35 +02:00
}
//! Locks the queue for access.
void lock()
{
this->_lock.acquire();
}
//! Unlocks the queue.
void unlock()
{
this->_lock.release();
}
///! Calls pop_front of the queue
void pop_front()
{
ACE_GUARD (LockType, g, this->_lock);
_queue.pop_front();
}
///! Checks if we're empty or not with locks held
bool empty()
{
ACE_GUARD_RETURN (LockType, g, this->_lock, false);
return _queue.empty();
}
2009-05-14 13:25:35 +02:00
};
}
#endif