/* * Ascent MMORPG Server * Copyright (C) 2005-2007 Ascent Team * * 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 3 of the License, or * 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, see . * */ #ifndef _THREADING_LOCKED_QUEUE_H #define _THREADING_LOCKED_QUEUE_H #include "Mutex.h" #include template class LockedQueue { public: ~LockedQueue() { } inline void add(const TYPE& element) { mutex.Acquire(); queue.push_back(element); mutex.Release(); } inline TYPE next() { mutex.Acquire(); assert(queue.size() > 0); TYPE t = queue.front(); queue.pop_front(); mutex.Release(); return t; } inline uint32 size() { mutex.Acquire(); uint32 c = queue.size(); mutex.Release(); return c; } inline TYPE get_first_element() { mutex.Acquire(); TYPE t; if(queue.size() == 0) t = reinterpret_cast(0); else t = queue.front(); mutex.Release(); return t; } inline void pop() { mutex.Acquire(); ASSERT(queue.size() > 0); queue.pop_front(); mutex.Release(); } protected: std::deque queue; Mutex mutex; }; #endif