tsqueue.h

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #ifndef _TSQUEUE_H
00021 #define _TSQUEUE_H
00022 #include <queue>
00023 #include <pthread.h>
00024 #include <errno.h>
00025 
00026 namespace freesecs
00027 {
00033     class lock_t
00034     {
00035     public:
00036         lock_t(pthread_mutex_t &mutex)
00037         :_mutex_ref(mutex)
00038         {
00039             pthread_mutex_lock(&_mutex_ref);
00040         }
00041         ~lock_t()
00042         {
00043             pthread_mutex_unlock(&_mutex_ref);
00044         }
00045     private:
00046         lock_t();
00047         pthread_mutex_t &_mutex_ref;
00048     };
00049 
00054     template <class T>
00055     class thread_safe_queue
00056     {
00057     private:
00058         mutable pthread_mutex_t _mutex;
00059         std::queue<T> _queue;
00060      
00061     public:
00062 
00063         typedef typename std::queue<T>::reference reference;
00064         typedef typename std::queue<T>::const_reference const_reference;
00065         typedef typename std::queue<T>::value_type value_type;
00066 
00067         thread_safe_queue()
00068         {
00069             pthread_mutex_init(&_mutex, NULL);
00070         }
00071 
00072         ~thread_safe_queue()
00073         {
00074             pthread_mutex_destroy(&_mutex);
00075         }
00076 
00077         bool empty() const
00078         {
00079             lock_t l(_mutex);
00080             return _queue.empty();
00081         }
00082         
00083         size_t size() const
00084         {
00085             lock_t l(_mutex);
00086             return _queue.size();
00087         }
00088 
00089         reference front()
00090         {
00091             lock_t l(_mutex);
00092             return _queue.front();
00093         }
00094 
00095         const_reference front() const
00096         {
00097             lock_t l(_mutex);
00098             return _queue.front();
00099         }
00100 
00101         reference back()
00102         {
00103             lock_t l(_mutex);
00104             return _queue.back();
00105         }
00106 
00107         const_reference back() const
00108         {
00109             lock_t l(_mutex);
00110             return _queue.back();
00111         }
00112 
00113         void push(const value_type &x)
00114         {
00115             lock_t l(_mutex);
00116             _queue.push(x);
00117         }
00118 
00119         void pop()
00120         {
00121             lock_t l(_mutex);
00122             _queue.pop();
00123         }
00124     };
00125 
00126 }//namespace
00127 
00128 #endif //_TSQUEUE_H

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1