Wolframe, 0.0.3

objectPool.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
35 
36 #ifndef _OBJECT_POOL_HPP_INCLUDED
37 #define _OBJECT_POOL_HPP_INCLUDED
38 
39 #include <vector>
40 #include <stdexcept>
41 
42 #include <boost/thread/mutex.hpp>
43 #include <boost/thread/condition_variable.hpp>
44 
45 namespace _Wolframe {
46 
48 class ObjectPoolTimeout : public std::runtime_error
49 {
50 public:
52  :std::runtime_error( "object pool exception"){}
53 };
54 
58 template < typename objectType >
59 class ObjectPool {
60 public:
61  ObjectPool( const unsigned to ) { m_timeout = to; }
62  ObjectPool() { m_timeout = 0; }
64  boost::lock_guard<boost::mutex> lock( m_mutex );
65  if ( !m_availList.empty() )
66  throw std::runtime_error( "ObjectPool not empty at destruction" );
67  }
68 
69  std::size_t available() const { return m_availList.size(); }
70 
71  objectType get() {
72  while( true ) {
73  boost::unique_lock<boost::mutex> lock( m_mutex );
74  if ( !m_availList.empty()) {
75  objectType obj = m_availList.back();
76  m_availList.pop_back();
77  lock.unlock();
78  return obj;
79  }
80  else {
81  if ( m_timeout == 0 ) {
82  while( m_availList.empty() )
83  m_cond.wait( lock );
84  }
85  else {
86  boost::system_time absTime = boost::get_system_time()
87  + boost::posix_time::seconds( m_timeout );
88  while( m_availList.empty() )
89  if ( ! m_cond.timed_wait( lock, absTime ))
90  throw ObjectPoolTimeout();
91  }
92  }
93  }
94  throw std::logic_error( "Logic error #1 in ObjectPool" );
95  }
96 
97  void add( objectType obj ) {
98  boost::lock_guard<boost::mutex> lock( m_mutex );
99  m_availList.push_back( obj );
100  m_cond.notify_one();
101  }
102 
103  static void static_add( ObjectPool* pool, objectType obj)
104  {
105  pool->add( obj);
106  }
107 
108  unsigned timeout() const { return m_timeout; }
109  void timeout( unsigned to ) { m_timeout = to; }
110 private:
111  std::vector< objectType > m_availList;
112  boost::mutex m_mutex;
113  boost::condition_variable m_cond;
114  unsigned m_timeout;
115 };
116 
117 } // namespace _Wolframe
118 
119 #endif // _OBJECT_POOL_HPP_INCLUDED
static void static_add(ObjectPool *pool, objectType obj)
Definition: objectPool.hpp:103
std::vector< objectType > m_availList
list (vector really) of available objects
Definition: objectPool.hpp:111
ObjectPool(const unsigned to)
Definition: objectPool.hpp:61
ObjectPool()
Definition: objectPool.hpp:62
Timeout exception for object pool.
Definition: objectPool.hpp:48
boost::condition_variable m_cond
the condition variable
Definition: objectPool.hpp:113
void add(objectType obj)
Definition: objectPool.hpp:97
unsigned timeout() const
Definition: objectPool.hpp:108
~ObjectPool()
Definition: objectPool.hpp:63
unsigned m_timeout
acquire timeout
Definition: objectPool.hpp:114
boost::mutex m_mutex
condition variable associated mutex
Definition: objectPool.hpp:112
void timeout(unsigned to)
Definition: objectPool.hpp:109
std::size_t available() const
Definition: objectPool.hpp:69
ObjectPoolTimeout()
Definition: objectPool.hpp:51
Class represening a pool of objects.
Definition: objectPool.hpp:59